【问题标题】:findContours gives memory heap errorfindContours 给出内存堆错误
【发布时间】:2012-04-29 04:15:53
【问题描述】:

我有以下图片,并尝试使用 OpenCV 用这些线条找到最大的矩形

std::vector< std::vector<cv::Point> > contours;
cv::findContours(result,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);

但是上面的语句会导致内存堆错误。谁能给我一个线索,为什么会这样?过去几个小时我一直在拉头发。

我认为这与 cv::Point 分配器有关,因为调用堆栈表明了这一点。

更新:我只是用 CvFindContours 运行程序,没有任何问题。所以一定是OpenCV 2.3.1。

Update2:感谢@karlphillip 的回答,我重新审视了我的项目,这是我的Visual Studio 项目设置。由于烦人的内存泄漏消息,我将 MFC 链接为静态库。这就是问题的原因。当我使用 MFC 作为共享 DLL 时,问题就消失了。

【问题讨论】:

  • 这是什么操作系统? Linux?视窗?你能提供一个最小的应用程序来重现这个问题吗?这两行似乎不是错误的原因。
  • @karlphillip 这是 Windows 7 64 位。你说的对。对不起我的懒惰。我会尝试想出一种方法来制作一个小型测试应用程序。

标签: c++ opencv image-processing


【解决方案1】:

我刚刚在 Linux 和 Windows XP(32 位)上使用 OpenCV 2.3.1 测试了 the following application,我没有遇到任何问题。

除非您可以编写一个最小的应用程序来重现您观察到的问题,否则这就是我所能做到的。

这是input image,代码就在下面:

#include <cv.h>
#include <highgui.h>

using namespace cv;

double angle( cv::Point pt1, cv::Point pt2, cv::Point pt0 ) 
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}


void find_squares(Mat& image, vector<vector<Point> >& squares)
{
    // blur will enhance edge detection
    Mat blurred(image);
    medianBlur(image, blurred, 9);

    Mat gray0(blurred.size(), CV_8U), gray;
    vector<vector<Point> > contours;

    // find squares in every color plane of the image
    for (int c = 0; c < 3; c++)
    {
        int ch[] = {c, 0};
        mixChannels(&blurred, 1, &gray0, 1, ch, 1);

        // try several threshold levels
        const int threshold_level = 2;
        for (int l = 0; l < threshold_level; l++)
        {
            // Use Canny instead of zero threshold level!
            // Canny helps to catch squares with gradient shading
            if (l == 0)
            {
                Canny(gray0, gray, 10, 20, 3); // 

                // Dilate helps to remove potential holes between edge segments
                dilate(gray, gray, Mat(), Point(-1,-1));
            }
            else
            {
                    gray = gray0 >= (l+1) * 255 / threshold_level;
            }

            // Find contours and store them in a list
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            // Test contours
            vector<Point> approx;
            for (size_t i = 0; i < contours.size(); i++)
            {
                    // approximate contour with accuracy proportional
                    // to the contour perimeter
                    approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

                    // Note: absolute value of an area is used because
                    // area may be positive or negative - in accordance with the
                    // contour orientation
                    if (approx.size() == 4 &&
                            fabs(contourArea(Mat(approx))) > 1000 &&
                            isContourConvex(Mat(approx)))
                    {
                            double maxCosine = 0;

                            for (int j = 2; j < 5; j++)
                            {
                                    double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                                    maxCosine = MAX(maxCosine, cosine);
                            }

                            if (maxCosine < 0.3)
                                    squares.push_back(approx);
                    }
            }
        }
    }
}

int main()
{    
    Mat img = imread("paper.jpg");

    vector<vector<Point> > squares;
    find_squares(img, squares);

    std::cout << "squares size: " << squares.size() << std::endl;
    getchar();

    return 0;
}

【讨论】:

  • 它也对我有用。所以是我。如果可以的话,我想给你1000分。非常感谢。
  • 您的回答不是解决方案,但帮助我找到了一个。这就是我接受你的回答的原因。
猜你喜欢
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 2013-03-01
  • 2011-03-27
  • 2012-12-13
  • 2018-03-09
  • 2021-12-14
  • 1970-01-01
相关资源
最近更新 更多