我试图使用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
答案 0 :(得分:1)
当您使用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;
}