Ask Your Question
0

Getting error for set mouse callback

asked 2017-10-26 08:57:25 -0600

vps gravatar image

updated 2017-10-26 09:19:12 -0600

Hi, I am trying to use mouse callback function to crop out the rectangular area by clicking twice on the image. Below is code and error message. It will great, if anyone help here. Thanks

#include "opencv2/opencv.hpp"
 using namespace std;
 using namespace cv;

 vector<Point>pt;
   Mat RectifiedImage;
     int main()
      {
         // read the video frame.......
        Mat RectifiedImage= frame;
    imshow("Rectified Image", RectifiedImage);
    setMouseCallback("Rectified Image", onMouse, &pt);
    Mat ROI;
    while (true)
    {
        if (pt.size() <= 2)
        {
            int x = pt[0].x;
            int y = pt[0].y;
            int x1 = pt[1].x;
            int y1 = pt[1].y;
            ROI = RectifiedImage(Rect(x, y, x1 - x, y1 - y));
            return false;
        }
       }            
    imshow("image", ROI);   
  }
                waitKey(0);
                return 0;
  }

          static void onMouse(int event, int i, int j, int flags, void* param)
                 {
              Mat &RectifiedImage = *((Mat*)param);
                    Point p;
                    if (event == EVENT_LBUTTONDOWN)
                     {
                       p = { i,j };
                       pt.push_back(p);
                       cout << i << j << endl;
                    }
             }

image description

edit retag flag offensive close merge delete

Comments

please try to format your code properly.

berak gravatar imageberak ( 2017-10-26 09:09:12 -0600 )edit

Hi berak, done

vps gravatar imagevps ( 2017-10-26 09:15:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-10-26 09:41:19 -0600

berak gravatar image

updated 2017-10-26 09:51:02 -0600

tbh, almost all of it is wrong here ;(

first, you pass in a vector<Point> to the callback, then, inside of that, you cast it into a Mat (won't work, it's also never used), whatever you click, it never gets back to your main(), if (pt.size() <= 2) should be >= (that's the actual exception) , -- i probably missed more problems.

you made a quite a mess there, throw it all away, and use selectROI() instead !

    Mat frame = ... // whatever
    Rect r = selectROI("Rectified Image", frame);
    Mat ROI = frame(r);   // done !
edit flag offensive delete link more

Comments

Yeah the error was (pt.size() <= 2) :) ..thanks

vps gravatar imagevps ( 2017-10-26 15:47:14 -0600 )edit

still, please do not reinvent the "flat tyre".

read docs, and use whatever "high-level" method is avaible in opencv already

berak gravatar imageberak ( 2017-10-26 15:50:13 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-26 08:57:25 -0600

Seen: 455 times

Last updated: Oct 26 '17