roi的opencv错误断言

opencv error assertion with roi

本文关键字:断言 错误 opencv roi      更新时间:2023-10-16

我正在尝试使用HOG分类器检测人员和其他对象。我开始使用以下代码检测人员:

    capt >> frame_capture;
    capt1 >> frame_capture1;
    cv::cvtColor(frame_capture1,gray, CV_RGB2GRAY);
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(gray,contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    vector <Rect> listOfRectangles;
    // detecting objects 
    listOfRectangles = drawBoundingBox(contours);
if(!frame_capture.empty()){
    for (int i =0; i<listOfRectangles.size();++i)
    {
        //rectangle (frame_capture, listOfRectangles[i],Scalar(255,255,0),1,8,0); //! display detections
        cv::Mat roi;
        roi.create(frame_capture.size(),CV_8UC3);
        cv::Mat image=imread("");   
         roi = image(listOfRectangles[i]);
            cv::Mat window;
            cv::resize(roi, window, cv::Size(64, 128));
            hog.detect(window, foundLocations);
            if (!foundLocations.empty())
            {
            cout << "person .." << endl;
            }   
    }
//oVideoWriter.write(frame_capture);
    imshow("video",frame_capture);
    waitKey(25);
}    

我得到了这个错误:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat

我用过这个链接:OpenCV:如何使用HOGDescriptionr::detect方法?

当您使用imread()时,您正在传递一个空路径,因此找不到图像,cv::Mat image也没有数据。在下一行中,你试图获得一个空图像的子图像(ROI),这就是为什么你会得到错误。

您需要正确初始化cv::Mat image。您可以通过在该行之后添加一个简单的验证来检查是否一切正常,例如

if(! image.data )                              // Check for invalid input
{
    std::cout <<  "Could not open or find the image" << std::endl ;
    return;
}