Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在OpenCV中删除鼠标回调并关闭窗口_C++_Opencv - Fatal编程技术网

C++ 如何在OpenCV中删除鼠标回调并关闭窗口

C++ 如何在OpenCV中删除鼠标回调并关闭窗口,c++,opencv,C++,Opencv,我有一个设置窗口,用户可以按一个按钮来创建一个新窗口,您可以在其中选择图像的一个区域。选择区域后,用户可以按鼠标右键将其坐标保存在xml文件中。保存xml文件后,此“选择区域”窗口应自动关闭 我的问题是,我无法在新窗口上调用cv::destroyWindow,而不会使整个程序崩溃。我想先用setMouseCallback(“imageWindow”,NULL,NULL)删除鼠标事件但这并不能阻止崩溃 以下是我的代码的重要部分: void Settings::on_buttonXML_clicke

我有一个设置窗口,用户可以按一个按钮来创建一个新窗口,您可以在其中选择图像的一个区域。选择区域后,用户可以按鼠标右键将其坐标保存在xml文件中。保存xml文件后,此“选择区域”窗口应自动关闭

我的问题是,我无法在新窗口上调用cv::destroyWindow,而不会使整个程序崩溃。我想先用setMouseCallback(“imageWindow”,NULL,NULL)删除鼠标事件但这并不能阻止崩溃

以下是我的代码的重要部分:

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 );
    cv::waitKey(0);
}

void Settings::onMouse(int event, int x, int y, int, void* userdata) {
    Settings* settings = reinterpret_cast<Settings*>(userdata);
    settings->onMouse(event, x, y);
}
void Settings::onMouse(int event, int x, int y) {
    if (event == CV_EVENT_LBUTTONUP) {
        cv::Point2f p(x, y);
        coor.push_back(p);
        cv::line(src,p,p,color);
        if(coor.size()>1)
            cv::line(src, p, coor[coor.size()-2], color);
        cv::imshow("imageWindow", src);
    }
    else if (event == CV_EVENT_RBUTTONUP && coor.size()>2){
        cv::line(src, coor[0], coor[coor.size()-1], color);
        getPointsInContour(coor);
        cv::imshow("imageWindow", src);
        cv::waitKey(500);
        cv::setMouseCallback( "imageWindow", NULL, NULL );
        cv::destroyWindow("imageWindow");
    }
}
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(pointPolygonTest(contour,p,false) >= 0) // yes inside
                insideContour.push_back(p);
        }
    }
    cout << "# points inside contour: " << insideContour.size() << endl;
    savePointsAsXML(insideContour);
}
void Settings::on_buttonXML_clicked(){
视频捕捉网络摄像机;
网络摄像头。打开(索引);
网络摄像头读取(src);
颜色=标量(0,0255);
coor_num=0;
xmlPath=“C:/myregion.xml”;
cv::namedWindow(“imageWindow”,cv\u WINDOW\u AUTOSIZE);
cv::imshow(“图像窗口”,src);
cv::setMouseCallback(“imageWindow”,onMouse,this);
cv::waitKey(0);
}
void设置::onMouse(int事件、int x、int y、int、void*userdata){
设置*Settings=reinterpret\u cast(用户数据);
设置->onMouse(事件,x,y);
}
无效设置::onMouse(int事件、int x、int y){
if(event==CV\u event\u LBUTTONUP){
cv::点2f p(x,y);
合作推回(p);
cv::线条(src、p、p、颜色);
如果(coor.size()>1)
cv::line(src,p,coor[coor.size()-2],颜色);
cv::imshow(“图像窗口”,src);
}
else if(event==CV_event_RBUTTONUP&&coor.size()>2){
cv::line(src,coor[0],coor[coor.size()-1],color);
getPointsInContour(coor);
cv::imshow(“图像窗口”,src);
cv::waitKey(500);
cv::setMouseCallback(“imageWindow”,NULL,NULL);
cv::destroyWindow(“imageWindow”);
}
}
void设置::savePointsAsXML(向量和轮廓){
TiXmlDocument文档;
TIXmlDecl(“1.0”、“1.0”、“1.0”);
插入子文档(decl);
对于(int i=0;i