C++ 如何使cv::setMouseCallback函数工作

C++ 如何使cv::setMouseCallback函数工作,c++,opencv,C++,Opencv,我看到一些问题可能是相同的。阅读答案后,我仍然无法使我的代码正常工作。因此,如果我重复这些帖子,我很抱歉 我成功地编写了以下代码: #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream&

我看到一些问题可能是相同的。阅读答案后,我仍然无法使我的代码正常工作。因此,如果我重复这些帖子,我很抱歉

我成功地编写了以下代码:

        #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;
}
更改后的代码:

//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;
}
//程序正在加载图像,并将其显示给用户。
//用户可以使用鼠标制作矩形并剪切加载的图像。
//命令行跟踪鼠标移动和矩形的坐标。
//用户可以使用“q”结束程序。
#包括
#包括
#包括
#包括
#包括
bool leftButtonDown=false,leftButtonUp=false//鼠标点击的标志
类大小图像{
cv::Mat img;//要处理的图像
cv::点cor1,cor2;//选定矩形的坐标
cv::Rect Rect;//矩形
std::string name;//windows名称
公众:
//////////////////////////////////////////////////////////////////////////////
ResizeImage(){std::cout cor2.x=x;this->cor2.y=y;};
//////////////////////////////////////////////////////////////////////////////
int getCoordinates 1X(){return cor1.x;};//getfunctions
int getCoordinates 1y(){return cor1.y;};
int getCoordinates 2x(){return cor2.x;};
int getCoordinates 2y(){return cor2.y;};
cv::Mat getImg(){return img;};
std::string getImgName(){return name;};
//////////////////////////////////////////////////////////////////////////////
静态void mouseCall(int事件、int x、int y、int标志、void*param);//静态函数
//////////////////////////////////////////////////////////////////////////////
void showImgOriginal();//显示函数(翻印图像)
//////////////////////////////////////////////////////////////////////////////
};
void ResizeImage::showImgOriginal(){//显示图像
cv::namedWindow(名称、cv\u窗口\u自动调整大小);
cv::imshow(名称,img);
};
void ResizeImage::setRect(){//计算选定矩形
rect.width=abs(cor1.x-cor2.x);
垂直高度=绝对高度(cor1.y-cor2.y);
rect.x=cv::min(cor1.x,cor2.x);
rect.y=cv::min(cor1.y,cor2.y);
}
void ResizeImage::mouseCall(int事件、int x、int y、int标志、void*param){
if(event==cv::event_LBUTTONDOWN)//查找第一个角点
{
leftButtonDown=true;((ResizeImage*)参数)->cor1.x=x;((ResizeImage*)参数)->cor1.y=y;//保存坐标
std::cout cor1.y)>10)//查找第二个角点并检查区域是否太小
{
leftButtonUp=true;((ResizeImage*)参数)->cor2.x=x;((ResizeImage*)参数)->cor2.y=y;//保存坐标

std::cout您应该将函数设置为静态

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

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

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

以下是如何实现这一目标的一个简单示例:

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();
类调用{
公众:
调用(inti):a(i){};
INTA;
静态无效鼠标(int事件、int x、int y、int标志、void*param){
标准::cout a
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();