How can i draw lines on a image using mouse in opencv and c++?

asked 2014-08-27 17:40:34 -0600

updated 2017-12-19 10:40:54 -0600

My goal is to draw a line in a image using mouse in opencv and c++. I looked at different codes online but i wanted to try something myself after having some ideas in my mind. I may be completely wrong but i am just a beginner.So, i thought of trying but Its still not working so i thought of posting it here. Please correct my mistakes if possible.

POINT p;
  GetCursorPos(&p);
 bool drawing;
  int x;
  int y;
  int startx,starty;
  int finishx,finishy;

  int z;
  int l;
Mat a;
a = imread("a.JPG");

 if(GetKeyState(VK_LBUTTON) & 0x80 != 0)
 {
     drawing = true;
     x = p.x;
    y = p.y;
    startx = x;//the starting position to start the drawing
     starty = y;


 }
if(GetKeyState(VK_RBUTTON) &0x80 != 0)
 {
     drawing = false;
    z= p.x;
    l = p.y;

    finishx = z;
    finishy = l;

 }
if(drawing==true)
  {
      line(a,Point(startx,starty),Point(finishx,finishy),Scalar(0,0,255),1);
          imshow("test image",a);
  }
edit retag flag offensive close merge delete

Comments

" Its still not working" is no description of your error! What happens and what did you expect? Is this your on-click callback? Why do you have these variables: x,y,z,l? Do you read the image within the callback?

FooBar gravatar imageFooBar ( 2014-08-28 01:21:57 -0600 )edit