Ask Your Question

ChazZ's profile - activity

2018-11-12 06:04:53 -0600 received badge  Taxonomist
2012-11-29 00:21:14 -0600 received badge  Student (source)
2012-11-05 05:36:22 -0600 received badge  Scholar (source)
2012-11-05 05:16:15 -0600 commented answer Rectangle missing top line

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

2012-11-05 05:15:45 -0600 received badge  Supporter (source)
2012-10-29 13:25:13 -0600 received badge  Editor (source)
2012-10-29 13:22:41 -0600 received badge  Autobiographer
2012-10-29 13:11:39 -0600 asked a question 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;
    }
}