title: How to make cv::setMouseCallback function work
body: <p>I've seen a few questions that are probably the same. I am still unable to make my code work after reading the answers. So I am sorry in advance if I am repeating the posts.</p>
  
  <p>I managed to write this code :</p>
  
  <pre><code>        #include &lt;opencv2/core/core.hpp&gt;
          #include &lt;opencv2/highgui/highgui.hpp&gt;
          #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  
          #include &lt;iostream&gt;
          #include &lt;string&gt;
  
          bool leftButtonDown = false, leftButtonUp = false;
          cv::Mat img;
          cv::Point cor1, cor2;
          cv::Rect rect;
  
      void mouseCall(int event, int x, int y, int, void*) {
          if (event == cv::EVENT_LBUTTONDOWN)                                                                     //finding first corner
          {
              leftButtonDown = true; cor1.x = x; cor1.y = y; std::cout &lt;&lt; "Corner 1: " &lt;&lt; cor1 &lt;&lt; std::endl;
          }
          if (event == cv::EVENT_LBUTTONUP) {
              if (abs(x - cor1.x)&gt;20 &amp;&amp; abs(y - cor1.y)&gt;5)                                                        //finding second corner and checking whether the region is too small 
              {
                  leftButtonUp = true; cor2.x = x; cor2.y = y; std::cout &lt;&lt; "Corner 2: " &lt;&lt; cor2 &lt;&lt; std::endl;
              }
              else { std::cout &lt;&lt; "Select more than 5 pixels" &lt;&lt; std::endl; }
          }
  
          if (leftButtonDown == true &amp;&amp; leftButtonUp == false)                                                    //when the left button is clicked and let off 
          {                                                                                                       //draw a rectangle continuously
              cv::Point pt; pt.x = x; pt.y = y;
              cv::Mat temp_img = img.clone();
              rectangle(temp_img, cor1, pt, cv::Scalar(0, 0, 255));                                            
              cv::imshow("Original", temp_img);
          }
          else if (event == cv::EVENT_MOUSEMOVE)                                                                  //tracking mouse movement
          {
              std::cout &lt;&lt; "Mouse moving over the window - position (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")" &lt;&lt; std::endl;
  
          }
          if (leftButtonDown == true &amp;&amp; leftButtonUp == true)                                                     //when the selection is done 
          {
              rect.width = abs(cor1.x - cor2.x);
              rect.height = abs(cor1.y - cor2.y);
              rect.x = cv::min(cor1.x, cor2.x);
              rect.y = cv::min(cor1.y, cor2.y);
              cv::Mat cutTempImg(img, rect);                                                                      //Selecting a ROI(region of interest) from the original img 
              cv::namedWindow("Cut Temporary Image");
              cv::imshow("Cut Temporary Image", cutTempImg);                                                      //showing the cropped image 
              leftButtonDown = false;
              leftButtonUp = false;
          }
      }
  int main(){
      img = cv::imread("image.jpg");
      cv::namedWindow("Original");
      cv::imshow("Original", img);
      cv::setMouseCallback("Original", mouseCall); //setting the mouse callback for selecting the region with mouse  
      while (char(cv::waitKey(1) != 'q')) //waiting for the 'q' key to finish the execution 
      {
      }
      return 0;
  }
  </code></pre>
  
  <p>And it is working fine. Now I want to make same code, with using class.(OOP)</p>
  
  <p>But <code>cv::setMouseCallback</code> function is not letting me do that.
  Can any one help me fix this?</p>
  
  <p>My second code :</p>
  
  <pre><code>    #include &lt;opencv2/core/core.hpp&gt;
      #include &lt;opencv2/highgui/highgui.hpp&gt;
      #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  
      #include &lt;iostream&gt;
      #include &lt;string&gt;
  
  class ResizeImage {
  
      cv::Mat img;
      cv::Point cor1, cor2;
      cv::Rect rect;
      std::string name;
  
  public:
  
      void setImg(cv::Mat img) { this-&gt;img = img; };
      cv::Mat getImg() { return img; };
      void setRect();
      int getCoordinates1X() { return cor1.x; };
      int getCoordinates1Y() { return cor1.y; };
      int getCoordinates2X() { return cor2.x; };
      int getCoordinates2Y() { return cor2.y; };
      void setCoordinates1(int x, int y) { this-&gt;cor1.x = x; this-&gt;cor1.y = y; };
      void setCoordinates2(int x, int y) { this-&gt;cor2.x = x; this-&gt;cor2.y = y; };
  
      void mouseCall(int event, int x, int y, int flags, void* param);
  
      void showImgOriginal();
      void setImgName(std::string name) { this-&gt;name = name; };
      std::string getImgName() { return name; };
  
  };
  
  void ResizeImage :: showImgOriginal()  {
      cv::namedWindow(name, CV_WINDOW_AUTOSIZE);
      cv::imshow(name, img);
  };
  
  void ResizeImage::setRect() {
      rect.width = abs(cor1.x - cor2.x);
      rect.height = abs(cor1.y - cor2.y);
      rect.x = cv::min(cor1.x, cor2.x);
      rect.y = cv::min(cor1.y, cor2.y);
  }
  
  void ResizeImage::mouseCall(int event, int x, int y, int flags, void* param) {
  
  
      if (event == cv::EVENT_LBUTTONDOWN)                                                                 //finding first corner
      {
          leftButtonDown = true; setCoordinates1(x,y); std::cout &lt;&lt; "Corner 1: " &lt;&lt; getCoordinates1X()&lt;&lt;" "&lt;&lt;getCoordinates1Y() &lt;&lt; std::endl;
      } 
      if (event == cv::EVENT_LBUTTONUP) {
          if (abs(x - cor1.x)&gt;20 &amp;&amp; abs(y - cor1.y)&gt;5)                                                //finding second corner and checking whether the region is too small 
          {
              leftButtonUp = true; setCoordinates2(x, y); std::cout &lt;&lt; "Corner 2: " &lt;&lt; getCoordinates2X() &lt;&lt; " " &lt;&lt; getCoordinates2Y() &lt;&lt; std::endl;
          }
          else { std::cout &lt;&lt; "Select more than 5 pixels" &lt;&lt; std::endl; }
      }  
  
      if (leftButtonDown == true &amp;&amp; leftButtonUp == false)                                            //when the left button is down 
      {
          cv::Point pt; pt.x = x; pt.y = y; 
          cv::Mat temp_img = img.clone(); 
          rectangle(temp_img, cor1, pt, cv::Scalar(0, 0, 255));                                           //drawing a rectangle continuously 
          cv::imshow("Original", temp_img);
      }
      else if (event == cv::EVENT_MOUSEMOVE)                                                              //tracking mouse movement
      {
          std::cout &lt;&lt; "Mouse moving over the window - position (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")" &lt;&lt; std::endl;
  
      }
      if (leftButtonDown == true &amp;&amp; leftButtonUp == true)                                             //when the selection is done 
      {
          setRect();
          cv::Mat cutTempImg(img, rect);                                                                          //Selecting a ROI(region of interest) from the original img 
          cv::namedWindow("Cut Temporary Image");
          cv::imshow("Cut Temporary Image", cutTempImg);                                                              //showing the cropped image 
          leftButtonDown = false;
          leftButtonUp = false;
      }
  }
  
  int main(){
      cv::Mat img = cv::imread("image.jpg");
  
      ResizeImage img_;
  
      img_.setImg(img);
      img_.setImgName("original");
      img_.showImgOriginal();
  
      cv::setMouseCallback(img_.getImgName(),img_.mouseCall());
  
      while (char(cv::waitKey(1) != 'q')) //waiting for the 'q' key to finish the execution 
      {
      }
      return 0;
  }
  </code></pre>
  
  <p>Code after changes :</p>
  
  <pre><code>//Program is loading image, and showing it to user. 
  //User can use mouse to make a rectangle and cut the loaded image.
  //Command line is tracking mouse movements and the coordinates of the rectangle.
  //User can end the program using 'q'.
  
  #include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  bool leftButtonDown = false, leftButtonUp = false;          //flags for mouse clicks
  
  
  class ResizeImage {
  
      cv::Mat img;            //image to process
      cv::Point cor1, cor2;   //coordinates of selected rectangle
      cv::Rect rect;          //rectangle
      std::string name;       //windows name
  
  public:
      //////////////////////////////////////////////////////////////////////////////
      ResizeImage() { std::cout &lt;&lt; "Starting..."&lt;&lt;std::endl; };                                   //Constructor/Destructor
      ~ResizeImage() { std::cout &lt;&lt; "Ending..." &lt;&lt; std::endl; };
      //////////////////////////////////////////////////////////////////////////////
      void setImg(cv::Mat img) { this-&gt;img = img; };
      void setImgName(std::string name) { this-&gt;name = name; };                                   //set functions
      void setRect();                                             
      void setCoordinates1(int x, int y) { this-&gt;cor1.x = x; this-&gt;cor1.y = y; };
      void setCoordinates2(int x, int y) { this-&gt;cor2.x = x; this-&gt;cor2.y = y; };
      //////////////////////////////////////////////////////////////////////////////
      int getCoordinates1X() { return cor1.x; };                                                  //getfunctions
      int getCoordinates1Y() { return cor1.y; };
      int getCoordinates2X() { return cor2.x; };
      int getCoordinates2Y() { return cor2.y; };
      cv::Mat getImg() { return img; };
      std::string getImgName() { return name; };
      //////////////////////////////////////////////////////////////////////////////
      static void mouseCall(int event, int x, int y, int flags, void* param);                     //static function
      //////////////////////////////////////////////////////////////////////////////
      void showImgOriginal();                                                                     //show function (priting image)
      //////////////////////////////////////////////////////////////////////////////
  
  };
  
  void ResizeImage :: showImgOriginal()  {            //showing image
      cv::namedWindow(name, CV_WINDOW_AUTOSIZE);
      cv::imshow(name, img);
  };
  
  void ResizeImage::setRect() {                       //calculating selected rectangle
      rect.width = abs(cor1.x - cor2.x);
      rect.height = abs(cor1.y - cor2.y);
      rect.x = cv::min(cor1.x, cor2.x);
      rect.y = cv::min(cor1.y, cor2.y);
  }
  
  void ResizeImage::mouseCall(int event, int x, int y, int flags, void* param) {
  
  
      if (event == cv::EVENT_LBUTTONDOWN)                                                                                         //finding first corner
      {
          leftButtonDown = true; ((ResizeImage*)param)-&gt;cor1.x = x; ((ResizeImage*)param)-&gt;cor1.y = y;                            //saving coordinates
          std::cout &lt;&lt; "Corner 1: " &lt;&lt; ((ResizeImage*)param)-&gt;cor1.x &lt;&lt; " " &lt;&lt; ((ResizeImage*)param)-&gt;cor1.y &lt;&lt; std::endl;        //printing coordinates
      } 
      if (event == cv::EVENT_LBUTTONUP) {
          if (abs(x - ((ResizeImage*)param)-&gt;cor1.x)&gt;20 &amp;&amp; abs(y - ((ResizeImage*)param)-&gt;cor1.y)&gt;10)                             //finding second corner and checking whether the region is too small 
          {
              leftButtonUp = true; ((ResizeImage*)param)-&gt;cor2.x = x; ((ResizeImage*)param)-&gt;cor2.y = y;                          //saving coordinates
              std::cout &lt;&lt; "Corner 2: " &lt;&lt; ((ResizeImage*)param)-&gt;cor2.x &lt;&lt; " " &lt;&lt; ((ResizeImage*)param)-&gt;cor2.y &lt;&lt; std::endl;    //printing coordinates
          }
          else { std::cout &lt;&lt; "Select more than 10 pixels" &lt;&lt; std::endl; }                                                        //warning if region is too small
      }  
  
      if (leftButtonDown == true &amp;&amp; leftButtonUp == false)                                                                        //when the left button is down 
      {
          cv::Point pt; pt.x = x; pt.y = y; 
          cv::Mat temp_img = ((ResizeImage*)param)-&gt;img.clone();
          rectangle(temp_img, ((ResizeImage*)param)-&gt;cor1, pt, cv::Scalar(0, 0, 255));                                            //drawing a rectangle continuously 
      }
      else if (event == cv::EVENT_MOUSEMOVE)                                                                                      //tracking mouse movement
      {
          std::cout &lt;&lt; "Mouse moving over the window - position (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")" &lt;&lt; std::endl;
  
      }
      if (leftButtonDown == true &amp;&amp; leftButtonUp == true)                                                                         //when the selection is done 
      {
          ((ResizeImage*)param)-&gt;setRect();
          cv::Mat cutTempImg(((ResizeImage*)param)-&gt;img, ((ResizeImage*)param)-&gt;rect);                                            //Selecting a ROI(region of interest) from the original img 
          cv::namedWindow("Cut Temporary Image");
          cv::imshow("Cut Temporary Image", cutTempImg);                                                                          //showing the cropped image 
          leftButtonDown = false;
          leftButtonUp = false;
      }
  }
  
  int main() {
  
      cv::Mat img = cv::imread("image.jpg");
  
      ResizeImage img_;
  
      img_.setImg(img);
      img_.setImgName("Original");
      img_.showImgOriginal();
  
      cv::setMouseCallback(img_.getImgName(),ResizeImage::mouseCall,&amp;img_);
  
      while (char(cv::waitKey(1) != 'q')) //waiting for the 'q' key to finish the execution 
      {
      }
      return 0;
  }
  </code></pre>