问题描述
我正在尝试在我的c ++项目中使用cv :: setMouseCallback.我就是不明白.让我成为一个类Stuff的人,该如何告诉该类您有一个框架并在此框架上运行cv :: setMouseCallback,这是我要尝试做的一个示例:
I'm trying to use cv::setMouseCallback in my c++ project. I just don't get it.let that I habe a class Stuff how can tell this class you got a frame and run the cv::setMouseCallback on this frame here is an example of what I'm trying to do :
class Stuff{
public:
Stuff();
void setFrame(cv::Mat);
void mouse (int,int, int, int,void*);
private :
cv::Mat frame;
int key;
};
Stuff::Stuff(){}
void Stuff::setFrame(cv::Mat framex){
frame = framex;
}
int main (){
Stuff obj;
cv::Mat frame = cv::imread ("examople.jpg");
char* name;
cv::imshow(name,frame);
cv::setMouseCallback(name,obj.mouse,&frame) // I' stop here because that's exactlly what just don't work
}
这得到的错误消息:
Stuff::mouse : function call missing argument list; use '&Stuff::mouse ' to create a pointer to member
真正的程序太大了,无法在此处放置其代码,这就是为什么我要简化问题
the real program is too big to put its code here that why I'm trying to simplify the question
推荐答案
您必须在类内部将鼠标处理程序声明为静态的.例如,我有一个dragger
,成员为mouser
,我想被调用.我声明一个帮助器static void mouser
,该帮助器将接收到的void *转换为空并调用该成员:
you must declare a mouse handler as static inside your class. For instance, I have a dragger
with a member mouser
, that I want to be called. I declare an helper static void mouser
, that cast the void* received and calls the member:
class dragger {
void mouser(int event, int x, int y) {
current_img = original_img.clone();
Point P(x, y);
...
}
static void mouser(int event, int x, int y, int, void* this_) {
static_cast<dragger*>(this_)->mouser(event, x, y);
}
和dragger
构造函数中的实例
dragger(string w, Mat m) :
window_id(w), status(0), original_img(m), /*black(0, 0, 0),*/ K(5, 5)
{
...
setMouseCallback(w, mouser, this);
}
...
}
这篇关于如何使用cv :: setMouseCallback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!