How to draw a circle on an image using mouse click/events?
Hi, My task is to draw a circle on an image using mouse events. I know how to use mouse events, I use for example CV_EVENT_LBUTTONDOWN and thanks to the cvSetMouseCallback and function that i wrote i get mouse click coordinates. I know even how to draw normally a circle, I use function "circle". But I don't know how to connect together theese two action- mouse events and drawing circle. I think that I have to get variables x and y from "void funkcja" but i don't know how to do that...any help?
My code:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
void function(int event, int x, int y, int flags, void* param)
{
switch(event){
case CV_EVENT_LBUTTONDOWN:
if(flags & CV_EVENT_FLAG_CTRLKEY)
{
printf("Left button clicked in coordinates %d, %d with CTRL\n",x,y);
}
else if(flags==CV_EVENT_LBUTTONDOWN)
{
printf("Left button clicked in coordinates %d, %d\n",x,y);
}
break;
}
}
int main( int argc, char** argv )
{
int key,parameter=5;
Mat image, dst;
/// Read image ( same size, same type )
image = imread("lena.jpg");
if( !image.data )
{
printf("Error \n"); return -1;
}
else
{
printf("Image has been loaded with size %dx%d.\n",image.size);
}
circle(image,cvPoint(100,100),50,Scalar(0,255,0),1);//draw circle in point 100,100
namedWindow("My window", 1);
imshow("My window",image);
cvSetMouseCallback("My window",function,¶meter);
std::cout << "what do you want to do?: \n";
while(1)
{
key=cvWaitKey(10);//waits
switch(key)
{
case 'X':
printf("Pressed X \n");
imwrite("image_circle.jpg",image);
return 1;
break;
case 27:
return 1;
break;
}
}
return 0;
}