Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 funkcja(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,parametr=5;

Mat obraz, dst;

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

if( !obraz.data )
{
printf("Error \n"); return -1;
}
else
{
printf("Image has been loaded with size %dx%d.\n",obraz.size);
}


circle(obraz,cvPoint(100,100),50,Scalar(0,255,0),1);//draw circle in point 100,100

namedWindow("My window", 1); 

imshow("My window",obraz);

cvSetMouseCallback("My window",funkcja,&parametr);

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",obraz);
return 1;
break;

case 27:
return 1;
break;
}

}
return 0;
}
click to hide/show revision 2
retagged

updated 2014-03-25 04:42:03 -0600

berak gravatar image

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 funkcja(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,parametr=5;

Mat obraz, dst;

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

if( !obraz.data )
{
printf("Error \n"); return -1;
}
else
{
printf("Image has been loaded with size %dx%d.\n",obraz.size);
}


circle(obraz,cvPoint(100,100),50,Scalar(0,255,0),1);//draw circle in point 100,100

namedWindow("My window", 1); 

imshow("My window",obraz);

cvSetMouseCallback("My window",funkcja,&parametr);

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",obraz);
return 1;
break;

case 27:
return 1;
break;
}

}
return 0;
}

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 funkcja(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,parametr=5;

Mat obraz, dst;

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

if( !obraz.data )
{
printf("Error \n"); return -1;
}
else
{
printf("Image has been loaded with size %dx%d.\n",obraz.size);
}


circle(obraz,cvPoint(100,100),50,Scalar(0,255,0),1);//draw circle in point 100,100

namedWindow("My window", 1); 

imshow("My window",obraz);

cvSetMouseCallback("My window",funkcja,&parametr);

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",obraz);
return 1;
break;

case 27:
return 1;
break;
}

}
return 0;
}

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 funkcja(int 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,parametr=5;
key,parameter=5;

Mat obraz, image, dst;

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

if( !obraz.data !image.data )
{
printf("Error \n"); return -1;
}
else
{
printf("Image has been loaded with size %dx%d.\n",obraz.size);
}


circle(obraz,cvPoint(100,100),50,Scalar(0,255,0),1);//draw %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",obraz);
window",image);

cvSetMouseCallback("My window",funkcja,&parametr);
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",obraz);
imwrite("image_circle.jpg",image);
return 1;
break;

case 27:
return 1;
break;
}

}
return 0;
}

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,&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;
}