weixin_41866266
weixin_41866266
2021-04-20 20:44
采纳率: 100%
浏览 50

opencv对roi区域执行hog.detectmultiscale()

这里用到的roi是在原视频上读取每帧图片,对前景进行轮廓检测,将检测到的行人目标用矩形标注出来。

利用roi=frame(rect(x0,y0,w0,z0))进行的roi标定。

希望利用detectanddraw(hog,img)对img区域进行行人检测

代码运行到hog.detectmultiscale(img)

这个时候运行到这里出现

OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0])) in cv::Mat::ptr

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • qq_34124780
    爱晚乏客游 2021-04-20 21:23
    已采纳

    这报错应该是你哪个参数为空或者维度不够的问题。打个断点看看函数里面的参数哪个是不对的。

    vector<Mat> roi(contours.size());这句话,你怎么能用一个整数来初始化一个mat数组。。。。。

    点赞 评论
  • software7503
    点赞 评论
  • weixin_41866266
    weixin_41866266 2021-04-20 21:52
    //复原原轮廓
    vector<Mat> roi(contours.size());
    for (int i = 0; i < contours.size(); i++) {
    				
    //计算凸包和轮廓面积
    double cntarea = contourArea(contours[i]);
    double hullarea = contourArea(hull[i]);
    Scalar color_contours = Scalar(0, 255, 0);// green - color for contours
    Scalar color = Scalar(255, 0, 0); // blue - color for convex hull
    
    //跳过轮廓面积远小于轮廓面积的目标轮廓
    if (cntarea < hullarea / 5.0)
        continue;
    				
    //将轮廓*K,复原轮廓
    for (int j = 0; j < contours[i].size(); j++)
     {
        //cout << "contour:" << i << "==" << contours[i] << endl;
        contours[i][j].x *= K;
        contours[i][j].y *= K;
     }
    				
    vector<Rect>boundRect(contours.size());
    int x0 = 0, y0 = 0, w0 = 0, h0 = 0;
    //画原视频轮廓
    				
    boundRect[i] = boundingRect(contours[i]);
    x0 = boundRect[i].x;
    y0 = boundRect[i].y;
    w0 = boundRect[i].width;
    h0 = boundRect[i].height;
    					
    if (w0 < 64 && h0 < 128)
    continue;
    
    			
    					
    rectangle(frame, Point(x0, y0), Point(x0 + w0, y0 + h0), Scalar(255, 0, 0), 1, 8);
    /*这里就是我要圈定的ROI,是希望用前景检测的方法先圈定粘连的行人再对这部分区域进行检测*/	
    roi[i] = frame(boundRect[i]);
    imageROI[framenum] = roi;
    
    //imageROI[framenum][i] = frame(boundRect[i]);
    					detectanddraw(hog, imageROI[framenum][i]);
    
    					imshow("mask", mask);
    					imshow("imageROI", imageROI[framenum][i]);
    					
    					//保存ROI为图片
    					//char save_image_name[128];
    					////CvArr* s = (CvArr*)&imageROI[i];
    					//sprintf_s(save_image_name, "rect_%d.jpg", i);
    					//imwrite(save_image_name, imageROI[framenum][i]);
    					
    					//imageROI[i]=frame(Range(x0, w0), Range(y0, h0));
    				//imshow("o1", Vibe_Bgs.getMask());
    				//imshow("drawing", drawing);
    			}
    点赞 评论
  • weixin_41866266
    weixin_41866266 2021-04-20 21:55
    void detectanddraw(HOGDescriptor&hog, Mat&img)
    {
    	//矩形框数组
    	vector<Rect> found, found_filtered;
    	//调用gettickcount()
    	//double t = (double)getTickCount();
    	
    	Mat gray_pic,binary_pic;
    	cvtColor(img, gray_pic,CV_BGR2GRAY);
    	//threshold(gray_pic, binary_pic, 10, 1, CV_THRESH_BINARY);
    	//报错的地方
    	hog.detectMultiScale(img, found, 0, Size(8, 8), Size(32, 32), 1.05, 2);
    	
    	/*t = (double)getTickCount() - t;
    	cout << "dection time=" << (t * 1000 / cvGetTickFrequency()) <<" ms" << endl;
    	cout << "dection result=" << found.size() << "rects" << endl;*/
    
    	for (int i = 0; i < found.size(); i++)
    	{
    		Rect r = found[i];
    		int j = 0;
    		for (; j < found.size(); j++)
    		{
    			if (j != i && ((r&found[j]) == r))
    				break;
    		}
    		if (j == found.size())
    			found_filtered.push_back(r);
    
    		for (int i = 0; i < found_filtered.size(); i++)
    		{
    			Rect r = found[i];
    			rectangle(img, r.tl(), r.br(), Scalar(0, 255, 0), 3);
    		}
    	}
    	//cout << "real detection result:" << found_filtered.size() << "rects" << endl;
    }
    点赞 评论
  • QA_Assistant
    有问必答小助手 2021-04-21 10:17

    您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论
  • QA_Assistant
    有问必答小助手 2021-04-21 17:48

    非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

    速戳参与调研>>>https://t.csdnimg.cn/Kf0y

    点赞 评论

相关推荐