Ask Your Question
2

Rectangle missing top line

asked 2012-10-29 13:11:39 -0600

ChazZ gravatar image

updated 2012-10-29 13:25:13 -0600

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;
    }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2012-10-30 09:26:42 -0600

Michael Burdinov gravatar image

My guess is that the problem is not in function that draws rectangle in image, but in displaying this image on screen. If your image is bigger than your screen it should be downsized. There many ways to reduce size, and some of them involve removing part of rows/columns. So it can be that top line of your rectangle belongs to one of those removed rows. Happend to me number of times when I opened my images with IrfanView (not necessary top line).

To check this assumption store image with the rectangle in file.

edit flag offensive delete link more

Comments

I try to store my image in a file and the rectangle is complete in my file, thank you !

ChazZ gravatar imageChazZ ( 2012-11-05 05:16:15 -0600 )edit

You are welcome

Michael Burdinov gravatar imageMichael Burdinov ( 2012-11-05 06:29:46 -0600 )edit

Question Tools

Stats

Asked: 2012-10-29 13:11:39 -0600

Seen: 1,867 times

Last updated: Oct 30 '12