Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Iam trying to draw circles on points where the user clicks on the image..below is my code..it doesn't draws the circle when clicked on the image.But when i add
cvCircle(shape,cvPoint(100,100),10,CV_RGB(0,255,0),-1);
inside main function it just draws the circle...(here i have given the x,y coordinates manually)

C++
IplImage* shape;

void mouseHandler(int event, int x, int y, int flags, void* param)
  {
    switch(event){
      case CV_EVENT_LBUTTONDOWN:
          printf("Left button clicked %i\ %i \n",x,y);
           cvCircle(shape,cvPoint(x,y),10,CV_RGB(0,255,0),-1);

          break;

      case CV_EVENT_RBUTTONDOWN:
        printf("Right BUTTON Clicked\n");
        break;

    }
}

int _tmain(int argc, _TCHAR* argv[])
{
   
    cvNamedWindow("Shapes",CV_WINDOW_AUTOSIZE);
     
    shape=cvLoadImage("bb.jpg");
    
      cvSetMouseCallback("Shapes",mouseHandler,0);

     
    cvShowImage("Shapes",shape);
    
    cvWaitKey(0);
   
    cvReleaseImage(&shape);
    cvDestroyAllWindows();

    return 0;
}


Does anyone know the reason?
Thankyou
Posted

1 solution

i have found the solution myself..Hope this will help....

C#
int xC,yC;
int *Xvalues = new int[15];
int *Yvalues = new int[15];
int count = 0;
bool drawing_circle = false;


<pr>


C++
void draw_circle(IplImage* img, int x,int y)
  {
         cvCircle(img,cvPoint(x,y),1,CV_RGB(0,255,0),-1);
	 Xvalues[count] = x;
	 Yvalues[count] = y;
	 count++;

    }
}


void my_mouse_callback( int event, int x, int y, int flags, void* param ){
	IplImage* image = (IplImage*) param;

	switch( event ){

		case CV_EVENT_LBUTTONDOWN:
			drawing_circle = true;
			xC = x;
			yC = y;
			break;
	}
}


C++
int _tmain(int argc, _TCHAR* argv[])
{

    cvNamedWindow("Shapes",CV_WINDOW_AUTOSIZE);

    shape=cvLoadImage("bb.jpg");

    cvSetMouseCallback( name, my_mouse_callback, (void*) image);


    cvShowImage("Shapes",shape);
    
      while( 1 ){
		
		if( drawing_box ) 
		{
			draw_circle( image, xC,yC );
			
		}
    cvWaitKey(0);

    cvReleaseImage(&shape);
    cvDestroyAllWindows();

    return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900