Siddhant
Siddhant

Reputation: 76

Debug Assertion Error in OpenCV

I was doing line detection in OpenCV. Everything was going fine until i get this Debug assertion error:

Debug Assertion Failed! Expression: _pFirstBlock == pHead

Screenshot of the assertion

I spent days working on it but cannot debug it. This is my code.

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main() {
VideoCapture stream1(0);  
while (true) {
    Mat cameraFrame;
    stream1.read(cameraFrame);
    imshow("cam", cameraFrame);

    if (waitKey(30) >= 0)
        break;

    Mat src = cameraFrame;


    Mat dst, cdst;
    Canny(src, dst, 50, 200, 3);
    cvtColor(dst, cdst, CV_GRAY2BGR);

   vector<Vec4i> lines;

    HoughLinesP(dst, lines, 1, CV_PI / 180, 50, 50, 10);
    for (size_t i = 0; i < lines.size(); i++)
    {
    Vec4i l = lines[i];
    line(cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, CV_AA);
    }

    imshow("processed", cdst);


}
return 0;
}

Upvotes: 1

Views: 2248

Answers (2)

karlphillip
karlphillip

Reputation: 93410

This happens because OpenCV libraries are traditionally compiled with the following runtime library:

  • MD (Release)
  • MDd (Debug)

while my IDE Qt Creator, which uses MSVC 2013 with it's default configuration, builds stuff with:

  • MT (Release)
  • MTd (Debug)

Surprisingly, the error only manifested when HOG's compute() was invoked.

To fully understand the MT vs MD (runtime library) dilemma, read this.

There are 2 different ways to handle this issue. The easy way out is to adjust your projects setting to also use MD/MDd as runtime library and match OpenCV's!

On Qt Creator, this can be done in the .pro file by adding:

QMAKE_CXXFLAGS_DEBUG += /MDd
QMAKE_CXXFLAGS_RELEASE += /MD

On the other hand, on some versions of Visual Studio this can be done through your Project Properties >> Configuration Properties >> C/C++ >> Code Generation and changing the Runtime Library to:

  • Multi-threaded Debug DLL (/MDd), if you are building the Debug version of your code.
  • Multi-threaded DLL(/MD), if you are building the Release version of your code.

The other way to handle this issue is to rebuild/recompile OpenCV with BUILD_WITH_STATIC_CRT enabled. This will compile OpenCV libraries with MT/MTd support.

Upvotes: 2

Niklas S
Niklas S

Reputation: 1

Maybe its the solution like the one, which helped for me.

You try using a Winforms with /clr specified for the GUI side calling into unmanaged code that at some point referenced a ATL header.

You need to bind the opencv Libarys in the linker dependencies ( opencv_calib2411d, ... care for Debug/Release and the opencv version you use)

additional:

add __DllMainCRTStartup@12 in Force Symbols Reference section in the Linker section of the project settings.

source https://social.msdn.microsoft.com/Forums/vstudio/en-US/62db4002-4ebc-4a3a-91ec-9fc702db821e/crtisvalidheappointerpuserdata-why-is-this-code-broken-vs2008?forum=vcgeneral

Upvotes: -1

Related Questions