Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Assertion Failed _pFirstBlock == pHead using Opencv

I am using following code for background subtraction. I am giving it path of video, video runs successfully but at the end it gives Debug Assertion Failed error.

enter image description here

I am using following code in Microsoft Visual Studio to solve a problem of Computer Vision with opencv.

#include<opencv2/opencv.hpp>
#include<iostream>
#include<string>
#include<vector>    
#include "opencv2/video/background_segm.hpp"
using namespace cv;
using namespace std;

int main()
{   
    Mat frame;
    Mat back;
    Mat fore;

    VideoCapture cap;
    cap.open("H:/competition.avi");

    BackgroundSubtractorMOG2 bg(100,16,true);    
    bg.set("nmixtures",3);

    vector<vector<Point> > contours;

    namedWindow("Frame");
    namedWindow("Background");

    for(;;)
    {
        cap >> frame;
        if(!frame.empty())
        {
            bg.operator ()(frame,fore);
            bg.getBackgroundImage(back);
            erode(fore,fore,Mat());
            dilate(fore,fore,Mat());
            findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
            drawContours(frame,contours,-1,Scalar(0,0,255),2);
            imshow("Frame",frame);
            imshow("Background",back);
            if(waitKey(30) >= 0) break;
        }
        else
            break;
    }
    return 0;
}
like image 959
Hammad Hassan Avatar asked Mar 26 '26 09:03

Hammad Hassan


2 Answers

I just came across this issue and after serious web trawling, found the answer, at least it worked in my case...

you need to go to your visual studio project settings, to c/c++, to code generation and change the runtime library to Multi-threaded Debug DLL (/MDd).

It seems this error is from a single threaded DLL trying to run in a multi thread project, or something of that nature.

good luck!

like image 128
anti Avatar answered Mar 27 '26 21:03

anti


For unknown reasons, some versions of opencv (2.x at least) have a CMake variable "BUILD_WITH_STATIC_CRT" that by default gets set to on, thus causing issues like that. Disable that flag, then the solution should get generated with /MDd defined.

Secondarily, open your exe file in dependency walker. Look for multiple versions of MS C++ runtime libraries. For example, you may have a version of QT built against msvcp110.dll (visual studio 2012) but your current project uses msvcp120.dll (visual studio 2013).

like image 20
peter karasev Avatar answered Mar 27 '26 23:03

peter karasev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!