Ask Your Question
2

How to draw a circle on an image using mouse click/events?

asked 2014-03-25 04:13:00 -0600

robcio_5 gravatar image

updated 2015-11-06 05:47:09 -0600

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,&parameter);

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

2 answers

Sort by ยป oldest newest most voted
4

answered 2014-03-25 04:56:14 -0600

berak gravatar image

you can just pass your image to the callback-function, and draw your circle there:

void onmouse(int event, int x, int y, int flags, void* param)
{
    if(event==CV_EVENT_LBUTTONDOWN)
    {
        Mat &img = *((Mat*)(param)); // 1st cast it back, then deref
        circle(img,Point(x,y),50,Scalar(0,255,0),1);
    }
}

int main()
{
    Mat img;
    namedWindow("My window", 1); 
    setMouseCallback("My window", onmouse, &img); // pass address of img here
    // ...
}

(some sidenotes:)

  • please don't use polish var-names, or comments, as people in the rest of the world can't read it.
  • please don't mix the c++ api with older c-functions ( cvSetMousCallback, etc )
edit flag offensive delete link more

Comments

1

Nice answer :)

StevenPuttemans gravatar imageStevenPuttemans ( 2014-03-25 05:32:29 -0600 )edit
0

answered 2014-03-30 08:52:54 -0600

robcio_5 gravatar image

updated 2014-03-30 08:54:28 -0600

O.K. I have changed my program like "berak" said but it still doesn't work. Where I make a mistake?

#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;
}
}

void onmouse(int event, int x, int y, int flags, void* param)
{
    if(event==CV_EVENT_LBUTTONDOWN)
    {
        Mat &image = *((Mat*)(param));
        circle(image,Point(x,y),50,Scalar(0,255,0),1);
    }
}

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);
}

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

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

setMouseCallback("My window",function,&parameter);

printf("what do you want to do?: \n");

while(1)
{
key=waitKey(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;
}
edit flag offensive delete link more

Comments

else if(flags==CV_EVENT_LBUTTONDOWN) // checks the wrong flag -> (flags & CV_EVENT_FLAG_**)

also you can only set one callback. (so onmouse is ignored)

berak gravatar imageberak ( 2014-03-30 09:05:15 -0600 )edit

OK, so I changed else if(flags==CV_EVENT_LBUTTONDOWN) into else if(flags & CV_EVENT_LBUTTONDOWN) and I left only one callback, setMouseCallback("My window",onmouse,&image); but it still doesn't work ;/ any other ideas?

robcio_5 gravatar imagerobcio_5 ( 2014-03-30 11:41:54 -0600 )edit

Question Tools

Stats

Asked: 2014-03-25 04:13:00 -0600

Seen: 14,229 times

Last updated: Mar 30 '14