Ask Your Question

Revision history [back]

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

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

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

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