gpt4 book ai didi

c++ - 如何在类里面使用 cv::setMouseCallback?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:59:32 27 4
gpt4 key购买 nike

我想在设置类中使用 cv::setMouseCallback 来选择图片区域。这是我的代码:

void Settings::on_buttonXML_clicked(){
cv::VideoCapture webcam;
webcam.open(INDEX);
webcam.read(src);
color = Scalar(0,0,255);
coor_num = 0;
xmlPath="C:/myregion.xml";
cv::namedWindow("imageWindow", CV_WINDOW_AUTOSIZE );
cv::imshow("imageWindow", src);
cv::setMouseCallback( "imageWindow", onMouse, 0 );
cv::waitKey(0);

}

void Settings::onMouse(int event, int x, int y, int, void* ) {
if (event == CV_EVENT_LBUTTONUP) {
Point2f p(x, y);
coor.push_back(p);
line(src,p,p,color);
if(coor.size()>1)
line(src, p, coor[coor.size()-2], color);
imshow("imageWindow", src);
}
else if (event == CV_EVENT_RBUTTONUP && coor.size()>2){
line(src, coor[0], coor[coor.size()-1], color);
getPointsInContour(coor);
imshow("imageWindow", src);
waitKey(2000);
exit(0);
}
}
void Settings::savePointsAsXML(vector<Point2f> & contour){
TiXmlDocument doc;
TiXmlDeclaration decl("1.0", "", "");
doc.InsertEndChild(decl);
for(int i = 0; i < contour.size(); i++)
{
TiXmlElement point("point");
point.SetAttribute("x",contour[i].x);
point.SetAttribute("y",contour[i].y);
doc.InsertEndChild(point);
}
if(doc.SaveFile(xmlPath.c_str()))
cout << "file saved succesfully.\n";
else
cout << "file not saved, something went wrong!\n";
}

void Settings::getPointsInContour(vector<Point2f> & contour){
vector<Point2f> insideContour;
for(int j = 0; j < src.rows; j++){
for(int i = 0; i < src.cols; i++){
Point2f p(i,j);
if(cv::pointPolygonTest(contour,p,false) >= 0) // yes inside
insideContour.push_back(p);
}
}
cout << "# points inside contour: " << insideContour.size() << endl;
savePointsAsXML(insideContour);
}

我收到了大量对 Settings:coor、Settings:src、Settings:color 的 undefined reference 。我无法理解什么需要静态才能工作。这是我的标题:

class Settings
{
private:
static void onMouse(int event, int x, int y, int, void* );
static void savePointsAsXML(std::vector<cv::Point2f> & contour);
static void getPointsInContour(std::vector<cv::Point2f> & contour);
static cv::Scalar color;
static std::vector<cv::Point2f> coor;
static int coor_num;
static std::string xmlPath;
static cv::Mat src;

我的代码中缺少什么?

最佳答案

由于 OpenCV 有一个类似 C 的接口(interface),它不采用成员函数作为回调,但您可以使用标准方法来克服这个问题,并将类实例作为 userdata 参数传递,然后将其转换回去到实例并调用成员方法。这是一个片段:

void Settings::on_buttonXML_clicked(){
cv::VideoCapture webcam;
webcam.open(INDEX);
webcam.read(src);
color = Scalar(0,0,255);
coor_num = 0;
xmlPath="C:/myregion.xml";
cv::namedWindow("imageWindow", CV_WINDOW_AUTOSIZE );
cv::imshow("imageWindow", src);
cv::setMouseCallback( "imageWindow", onMouse, this ); // Pass the class instance pointer here
cv::waitKey(0);
}

// In you header make a static and a member version of onMouse
void onMouse(int event, int x, int y);
static void onMouse(int event, int x, int y, int, void* userdata);

// Implement it to call the member function
void Settings::onMouse(int event, int x, int y, int, void* userdata)
{
// Check for null pointer in userdata and handle the error
...
Settings* settings = reinterpret_cast<Settings*>(userdata);
settings->onMouse(event, x, y);
}

希望这能解释这个想法,我是内联写的,所以对于任何拼写错误我深表歉意。

关于c++ - 如何在类里面使用 cv::setMouseCallback?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25748404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com