Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I'm trying to draw a rectangle on my mouse click coordinates?

I am very new to opencv. So basically it compiles and runs well but I am unable to see the rectangle on my image. Can someone help me with this? Thank you so much :)

----------
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
Point point1, point2;


void onmouse(int event, int x, int y, int flags, void* param)
{
    if(event==CV_EVENT_LBUTTONDOWN)
    {
            Mat image = imread("mozart.jpg");
        point1 = Point(x, y);
        point2 = Point(x + 20, y + 20);
        rectangle(image, point1, point2,CV_RGB(0,255,0),1);
        std::cout << "(" << x << ", " << y << ")" << "\n";

    }
}

int main( int argc, char** argv )
{


Mat image, dst;

/// Read image ( same size, same type )
image = imread("mozart.jpg");


namedWindow("My window", 1);
imshow("My window",image);

setMouseCallback("My window", onmouse, &image);

waitKey();
return 0;
}


----------