Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Rectangle missing top line

Hi everyone!

I have an issue with cv::rectangle, I'm trying to draw a rectangle with a cv::Rect fill my a mouse callback. And the result is:

image description

I'm running under windows 8 with the opencv 2.4.3 and I use VS2010.

Here is my code: Function to draw rectangle.

cv::Rect myBox(-1, -1, 0, 0);
bool drawRect = false;

void
drawBox(cv::Mat image, cv::Rect myRect) {
    std::cout << myRect.width << " " << myRect.height << std::endl;
        // To draw rectangle from point of myBox
    cv::rectangle(image, cv::Point(myBox.x, myBox.y), cv::Point(myBox.x + myBox.width, myBox.y + myBox.height), cv::Scalar(0x0, 0, 0xFF));
        // To draw rectangle direct from myBox
    cv::rectangle(image, myBox, cv::Scalar(0x0, 0, 0xFF));
        // To draw a fixed size rectangle because I have some issue with the other call of this function
    cv::rectangle(image, cv::Point(150, 150), cv::Point(300, 300), cv::Scalar(0, 0, 0xFF));
}

Call back function:

void
onMouse(int myEvent, int x, int y, int flags, void *param) {
    IplImage *image = (IplImage*)param;

    switch (myEvent) {
        case CV_EVENT_LBUTTONDOWN:
            drawRect = true;
            myBox = cv::Rect(x, y, 0, 0);
            break;
        case CV_EVENT_MOUSEMOVE:
            if (drawRect) {
                myBox.width = x - myBox.x;
                myBox.height = y - myBox.y;
            }
            break;
        case CV_EVENT_LBUTTONUP:
            drawRect = false;
            if (myBox.width < 0) {
                myBox.x += myBox.width;
                myBox.width *= -1;
            } if (myBox.height < 0) {
                myBox.y += myBox.height;
                myBox.height *= -1;
            }
            drawBox((cv::Mat(image)), myBox);
            break;
    }
}

Rectangle missing top line

Hi everyone!

I have an issue with cv::rectangle, I'm trying to draw a rectangle with a cv::Rect fill my a mouse callback. And the result is:

image description

I'm running under windows 8 with the opencv 2.4.3 and I use VS2010.

Here is my code: Function to draw rectangle.

cv::Rect myBox(-1, -1, 0, 0);
bool drawRect = false;

void
drawBox(cv::Mat image, cv::Rect myRect) {
    std::cout << myRect.width << " " << myRect.height << std::endl;
        // To draw rectangle from point of myBox
    cv::rectangle(image, cv::Point(myBox.x, myBox.y), cv::Point(myBox.x + myBox.width, myBox.y + myBox.height), cv::Scalar(0x0, 0, 0xFF));
        // To draw rectangle direct from myBox
    cv::rectangle(image, myBox, cv::Scalar(0x0, 0, 0xFF));
        // To draw a fixed size rectangle because I have some issue with the other call of this function
    cv::rectangle(image, cv::Point(150, 150), cv::Point(300, 300), cv::Scalar(0, 0, 0xFF));
}

Call back function:

void
onMouse(int myEvent, int x, int y, int flags, void *param) {
    IplImage *image = (IplImage*)param;

    switch (myEvent) {
        case CV_EVENT_LBUTTONDOWN:
            drawRect = true;
            myBox = cv::Rect(x, y, 0, 0);
            break;
        case CV_EVENT_MOUSEMOVE:
            if (drawRect) {
                myBox.width = x - myBox.x;
                myBox.height = y - myBox.y;
            }
            break;
        case CV_EVENT_LBUTTONUP:
            drawRect = false;
            if (myBox.width < 0) {
                myBox.x += myBox.width;
                myBox.width *= -1;
            } if (myBox.height < 0) {
                myBox.y += myBox.height;
                myBox.height *= -1;
            }
            drawBox((cv::Mat(image)), myBox);
            break;
    }
}