OpenCV Error: Assertion failed (dsize.area() || (inv_scale_x > 0 && inv_scale_y > 0)) in unknown fun

本文解决了使用HOGDescriptor进行行人检测时出现的 Assertion Failed 错误,并提供了一种通过检查区域大小确保检测过程顺利的方法。
[RESOLVED] HOGDescriptor Error: Assertion Failed!
HOGDescriptor
assertion
error
asked Jun 13 '13
Shaban gravatar image
Shaban flag of Indonesia 
187 ●2 ●8 ●17
updated Jun 13 '13
Hi, I've just added HOG Descriptor to my object detection code to tracking the pedestrian. Here we go my code:

#include"stdafx.h"
#include<vector>
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/objdetect/objdetect.hpp>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;     
    cv::Mat blurred;
    cv::Mat thresholded;
    cv::Mat gray;
    cv::Mat blob;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");   
    cv::namedWindow("Background Model");
    cv::namedWindow("Blob");
    cv::VideoCapture cap("campus3.avi");    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 3;
        bgs.history = 1000;
        bgs.varThresholdGen = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

    std::vector<std::vector<cv::Point>> contours;               

    cv::HOGDescriptor human;
    assert(human.load("hogcascade_pedestrians.xml"));

    for(;;)
    {
        cap >> frame;                                           

        cv::GaussianBlur(frame,blurred,cv::Size(3,3),0,0,cv::BORDER_DEFAULT);

        bgs.operator()(blurred,fg);                         
        bgs.getBackgroundImage(bgmodel);                                

        cv::threshold(fg,thresholded,70.0f,255,CV_THRESH_BINARY);

        cv::Mat elementCLOSE(5,5,CV_8U,cv::Scalar(255,255,255));
        cv::morphologyEx(thresholded,thresholded,cv::MORPH_CLOSE,elementCLOSE);

        cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
        cv::cvtColor(thresholded,blob,CV_GRAY2RGB);
        cv::drawContours(blob,contours,-1,cv::Scalar(1),CV_FILLED,8);

        cv::cvtColor(frame,gray,CV_RGB2GRAY);
        cv::equalizeHist(gray, gray);

        int cmin = 50; 
        int cmax = 1000;
        std::vector<cv::Rect> rects;
        std::vector<std::vector<cv::Point>>::iterator itc=contours.begin();

        while (itc!=contours.end()) {   

                if (itc->size() > cmin && itc->size() < cmax){ 

                        human.detectMultiScale(gray, rects);
                        for (unsigned int i=0;i<rects.size();i++) {
                            cv::rectangle(frame, cv::Point(rects[i].x, rects[i].y),
                            cv::Point(rects[i].x+rects[i].width, rects[i].y+rects[i].height),
                            cv::Scalar(0, 255, 0));
                         }

                        ++itc;
                    }else{++itc;}
        }

        cv::imshow("Frame",frame);
        cv::imshow("Background Model",bgmodel);
        cv::imshow("Blob",blob);
        if(cv::waitKey(30) >= 0) break;
    }
    return 0;
}
On my code, I minimize the area using findcontours and make the area bigger than the object using morphologyEX (Close) and then I'm using HOGDescriptors.detectMultiScale to detect human in the contours area. But I've got the error message when I run the program. This is my error message:

"OpenCV Error: Assertion failed (dsize.area() || (inv_scale_x > 0 && inv_scale_y > 0)) in unknown function, file C:\OpenCV\modules\imgproc\src\imgwarp.cpp, line 1726"

I've tried to detectMultiScale directly without findcontours but the same error message happened to me! So how to resolve this problems?

I'll appreciate any help here. Thanks! :)

==============

edited: resolved!

I've changed...

cv::HOGDescriptor body;
to...

cv::CascadeClassifier body;
it works like a charm! it can detect the pedestrian! :)

but there's another problem, this program run slowly! SO LAGGY! :))
add a comment
1 answer Sort by » oldestnewestmost voted
1
answered Jun 13 '13
Siegfried gravatar image
Siegfried 
1333 ●8 ●25
Hi Shaban,

if you want to detect persons in ROI`s (given by each contour) you have to check that the ROI is larger than the used HoG detector size. cv::HOGDescriptor.detectMultiScale will crash if the ROI is smaller than the detector size. In your case you load a detector with height 96 and width 48. But you check if the number of the points of a contour is in a certain range (min = 50, max 1000).

if (itc->size() > cmin && itc->size() < cmax)
To get the bounding rectangle of the ROI use the points of each contour.

cv::Rect object_bounding_box = cv::boundingRect(*itc);
Now, check if the height and width of the object_bounding_box is larger than the HoG detector.

if(object_bounding_box.height > human.winSize.height && object_bounding_box.width > human.winSize.width)
After the check you can take the ROI and run detectMultiScale on it.

cv::Mat object_roi = gray(object_bounding_box);
human.detectMultiScale(object_roi, rects);
link Comments
Wow thanks! but I've got another error message on human.detectMultiscale(object_roi_ rects): "Error 1 error C2065: 'object_roi' : undeclared identifier C:\Users\MuhamadRizky\Documents\Visual Studio 2012\Projects\Shaban\Shaban\Shaban.cpp 82 1 Shaban" odd... T_T
Shaban gravatar imageShaban (Jun 13 '13)
I've tried your suggestion but still got the same message. omg, I'm so curious about this case but til' now I can't resolve it.. T_T
Shaban gravatar imageShaban (Jun 13 '13)
I've used your code but i'm having errors like IntelliSense: member "cv::BackgroundSubtractorMOG2::nmixtures" (declared at line 145 of "D:\drivers\opencv\build\include\opencv2/video/background_segm.hpp") is inaccessible c:\Users\Maryum\Documents\Visual Studio 2012\Projects\ObstacleDetection\ObstacleDetection\Task3ConsoleApp.cpp 30 13 ObstacleDetection... and to every variable like history and varThresholdGen and others..what would be the reason? Could you plz assist me?
Hejab gravatar imageHejab (Apr 10 '14)
add a comment
在使用 OpenCV 的 `cv.resize` 函数时,出现断言错误 `Assertion failed (dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0))` 通常表明目标图像尺寸或缩放因子设置不当,导致函数无法执行有效的图像缩放操作。以下是可能的原因及解决方案: ### 原因分析 1. **目标尺寸无效** 如果指定了目标图像尺寸 `dsize`,但其宽度或高度小于等于零,则会触发该错误。例如: ```python dsize = (0, 0) resized_img = cv2.resize(img, dsize) ``` 这种情况下,OpenCV 无法计算有效的输出图像尺寸[^1]。 2. **缩放因子非法** 如果未指定 `dsize`,而是通过 `fx` 和 `fy` 参数指定缩放因子,但缩放因子小于等于零,也会导致错误。例如: ```python resized_img = cv2.resize(img, None, fx=0, fy=0.5) ``` 此时 `inv_scale_x` 或 `inv_scale_y` 会小于等于零,从而导致断言失败[^1]。 3. **输入图像为空** 如果输入图像本身为空(例如路径错误或图像读取失败),调用 `resize` 函数时也可能触发此错误。 --- ### 解决方案 1. **确保目标尺寸有效** 在使用 `dsize` 参数时,应确保其宽度和高度均大于零。例如: ```python dsize = (new_width, new_height) if dsize[0] > 0 and dsize[1] > 0: resized_img = cv2.resize(img, dsize) ``` 2. **确保缩放因子合法** 在使用 `fx` 和 `fy` 缩放图像时,应确保缩放因子大于零: ```python if fx > 0 and fy > 0: resized_img = cv2.resize(img, None, fx=fx, fy=fy) ``` 3. **检查输入图像是否有效** 在调用 `resize` 之前,先验证图像是否成功加载: ```python if img is not None: resized_img = cv2.resize(img, dsize) else: print("Error: Input image is empty.") ``` 4. **结合使用 `dsize` 和缩放因子** 如果需要动态计算缩放比例,可以基于原始图像尺寸计算目标尺寸。例如,将图像缩小为原来的一半: ```python height, width = img.shape[:2] dsize = (width // 2, height // 2) resized_img = cv2.resize(img, dsize) ``` 5. **异常处理机制** 在实际应用中,建议加入异常处理逻辑以增强程序的健壮性: ```python try: resized_img = cv2.resize(img, dsize) except cv2.error as e: print(f"OpenCV error: {e}") ``` --- ### 总结 OpenCV 的 `resize` 函数对输入参数有严格的限制,特别是在目标尺寸和缩放因子方面。通过确保参数的有效性、检查输入图像状态以及合理使用异常处理,可以有效避免此类断言错误。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值