OpenCV with HoughLines(Visual Studio C++):_CrtIsValidHeapPoi

OpenCV With HoughLines(Visual Studio C++): _CrtIsValidHeapPointer(pUserData) Breakpoint Error

本文关键字:CrtIsValidHeapPoi C++ Studio with HoughLines Visual OpenCV      更新时间:2023-10-16

我对OpenCV还很陌生,最近才遇到HoughLinesP函数。首先,我的目标是编写代码来检测网络摄像头中的矩形。目前,我在下面的代码通常只检测行。但是,我在调试程序时仍然遇到问题。这是我的代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main() {
    int erosion_size = 0;
    VideoCapture cam(0);   
    if (!cam.isOpened()) { 
        cout << "cannot open camera";
    }
    while (true) {
        Mat frame;
        cam.read(frame);
        Mat gray, edge, draw, die;
        cvtColor(frame, gray, CV_BGR2GRAY);
        Canny(gray, edge, 100, 150, 3);
        edge.convertTo(draw, CV_8U);
        dilate(draw, die, Mat(), Point(-1, -1), 2, 1, 1);
        erode(die, die, Mat(), Point(-1, -1), 1, 1, 1);
#if 0
        vector<Vec2f> lines;
        HoughLines(die, lines, 1, CV_PI / 180, 100, 0, 0);
        for (size_t i = 0; i < lines.size(); i++)
        {
            float rho = lines[i][0], theta = lines[i][1];
            Point pt1, pt2;
            double a = cos(theta), b = sin(theta);
            double x0 = a*rho, y0 = b*rho;
            pt1.x = cvRound(x0 + 1000 * (-b));
            pt1.y = cvRound(y0 + 1000 * (a));
            pt2.x = cvRound(x0 - 1000 * (-b));
            pt2.y = cvRound(y0 - 1000 * (a));
            line(frame, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
        }
#else
        vector<Vec4i> lines;
        HoughLinesP(die, lines, 1, CV_PI / 180, 200, 50, 10);
        for (size_t i = 0; i < lines.size(); i++)
        {
            Vec4i l = lines[i];
            line(frame, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, CV_AA);
        }
#endif

        imshow("Canny", die);
        imshow("original", frame);
        if (waitKey(30) >= 0)
            break;
    }
    return 0;
}

当我调试程序时,网络摄像头弹出正常,但是当我显示一个带有线条(一张纸)的矩形对象时,它会停止程序并出现断点错误。我得出的结论是,我的程序每次找到一行时都会停止。当我选择继续而不是中断时,它会给我这个错误:

Debug Assertion failed!
Program: ....Studio
2013/Projects/TrialRectangle/Debug/TrialRectangle.exe
File: f:/dd/vctools/crt/crtw32/misc/dbgheap.c
Line: 1332
Expression: _CrtIsValidHeapPointer(pUserData)

我玩了一下HoughLinesP函数,发现一个高阈值参数(例如500)似乎使我的程序运行良好,但它在我的网络摄像头中根本没有显示任何HoughLines。如果有人能解释为什么会这样,那也会有所帮助!

有没有人对如何解决这个断点错误有任何想法?

我也进入了这个调试断言。

就我而言,这是因为我的项目是静态编译的,但我动态地使用 OpenCV 和它的 dll。所以我把我的项目改成动态编译,解决了问题。

这是因为 OpenCV 对象被分配到不同的堆中。当这个对象被破坏时,当前运行时找不到那个堆,这就是命中断言的原因。

相关文章:
  • 没有找到相关文章