如何使 cv::setMouseCallback 函数工作

How to make cv::setMouseCallback function work

本文关键字:函数 工作 setMouseCallback cv 何使      更新时间:2023-10-16

我见过几个可能相同的问题。阅读答案后,我仍然无法使我的代码工作。因此,如果我重复这些帖子,我提前表示歉意。

我设法编写了这段代码:

        #include <opencv2/core/core.hpp>
        #include <opencv2/highgui/highgui.hpp>
        #include <opencv2/imgproc/imgproc.hpp>
        #include <iostream>
        #include <string>
        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 << "Corner 1: " << cor1 << std::endl;
        }
        if (event == cv::EVENT_LBUTTONUP) {
            if (abs(x - cor1.x)>20 && abs(y - cor1.y)>5)                                                        //finding second corner and checking whether the region is too small 
            {
                leftButtonUp = true; cor2.x = x; cor2.y = y; std::cout << "Corner 2: " << cor2 << std::endl;
            }
            else { std::cout << "Select more than 5 pixels" << std::endl; }
        }
        if (leftButtonDown == true && 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 << "Mouse moving over the window - position (" << x << ", " << y << ")" << std::endl;
        }
        if (leftButtonDown == true && 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;
}

而且它工作正常。现在我想使用class.(OOP)制作相同的代码。

cv::setMouseCallback功能是不允许我这样做。任何人都可以帮我解决这个问题吗?

我的第二个代码:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <iostream>
    #include <string>
class ResizeImage {
    cv::Mat img;
    cv::Point cor1, cor2;
    cv::Rect rect;
    std::string name;
public:
    void setImg(cv::Mat img) { this->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->cor1.x = x; this->cor1.y = y; };
    void setCoordinates2(int x, int y) { this->cor2.x = x; this->cor2.y = y; };
    void mouseCall(int event, int x, int y, int flags, void* param);
    void showImgOriginal();
    void setImgName(std::string name) { this->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 << "Corner 1: " << getCoordinates1X()<<" "<<getCoordinates1Y() << std::endl;
    } 
    if (event == cv::EVENT_LBUTTONUP) {
        if (abs(x - cor1.x)>20 && abs(y - cor1.y)>5)                                                //finding second corner and checking whether the region is too small 
        {
            leftButtonUp = true; setCoordinates2(x, y); std::cout << "Corner 2: " << getCoordinates2X() << " " << getCoordinates2Y() << std::endl;
        }
        else { std::cout << "Select more than 5 pixels" << std::endl; }
    }  
    if (leftButtonDown == true && 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 << "Mouse moving over the window - position (" << x << ", " << y << ")" << std::endl;
    }
    if (leftButtonDown == true && 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;
}

更改后的代码:

//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 <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
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 << "Starting..."<<std::endl; };                                   //Constructor/Destructor
    ~ResizeImage() { std::cout << "Ending..." << std::endl; };
    //////////////////////////////////////////////////////////////////////////////
    void setImg(cv::Mat img) { this->img = img; };
    void setImgName(std::string name) { this->name = name; };                                   //set functions
    void setRect();                                             
    void setCoordinates1(int x, int y) { this->cor1.x = x; this->cor1.y = y; };
    void setCoordinates2(int x, int y) { this->cor2.x = x; this->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)->cor1.x = x; ((ResizeImage*)param)->cor1.y = y;                            //saving coordinates
        std::cout << "Corner 1: " << ((ResizeImage*)param)->cor1.x << " " << ((ResizeImage*)param)->cor1.y << std::endl;        //printing coordinates
    } 
    if (event == cv::EVENT_LBUTTONUP) {
        if (abs(x - ((ResizeImage*)param)->cor1.x)>20 && abs(y - ((ResizeImage*)param)->cor1.y)>10)                             //finding second corner and checking whether the region is too small 
        {
            leftButtonUp = true; ((ResizeImage*)param)->cor2.x = x; ((ResizeImage*)param)->cor2.y = y;                          //saving coordinates
            std::cout << "Corner 2: " << ((ResizeImage*)param)->cor2.x << " " << ((ResizeImage*)param)->cor2.y << std::endl;    //printing coordinates
        }
        else { std::cout << "Select more than 10 pixels" << std::endl; }                                                        //warning if region is too small
    }  
    if (leftButtonDown == true && leftButtonUp == false)                                                                        //when the left button is down 
    {
        cv::Point pt; pt.x = x; pt.y = y; 
        cv::Mat temp_img = ((ResizeImage*)param)->img.clone();
        rectangle(temp_img, ((ResizeImage*)param)->cor1, pt, cv::Scalar(0, 0, 255));                                            //drawing a rectangle continuously 
    }
    else if (event == cv::EVENT_MOUSEMOVE)                                                                                      //tracking mouse movement
    {
        std::cout << "Mouse moving over the window - position (" << x << ", " << y << ")" << std::endl;
    }
    if (leftButtonDown == true && leftButtonUp == true)                                                                         //when the selection is done 
    {
        ((ResizeImage*)param)->setRect();
        cv::Mat cutTempImg(((ResizeImage*)param)->img, ((ResizeImage*)param)->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,&img_);
    while (char(cv::waitKey(1) != 'q')) //waiting for the 'q' key to finish the execution 
    {
    }
    return 0;
}

如果要使用类方法作为回调,确实应该将该方法声明为static。但是,您还需要将对象传递给回调,以便能够访问类的非静态成员,例如 cor1cor2

下面是如何实现此目的的最小示例:

class Call {
public:
    Call(int i) : a(i){};
    int a;
    static void mouse(int event, int x, int y, int flags, void* param) {
        std::cout << ((Call*)param)->a << std::endl;
    }
};

cv::namedWindow("Call");
Call call(10);
cv::setMouseCallback("Call", Call::mouse, &call);
cv::imshow("Call", cv::Mat(100, 100, CV_8U, cv::Scalar(0)));
cv::waitKey();

我创建了一个Call对象,并使用其mouse方法作为窗口回调,同时仍然将对象传递给回调。

你应该使函数static

static void mouseCall(int event, int x, int y, int flags, void* param);

然后:

cv::setMouseCallback(img_.getImgName(),ResizeImage::mouseCall);