Ask Your Question
0

Segmentation Fault in findContours()

asked 2018-03-17 05:26:13 -0500

El-Rey gravatar image

I tried all the solutions given on stack-overflow but nothing works. I have a competition in a month and need this piece of code working ASAP. Help me P.S. If i comment out findcontours() the function runs without a segmentation fault.

vector<RotatedRect> findTextAreas(Mat image)
{
    Mat kernel = getStructuringElement(MORPH_CROSS, Size(3, 3));
    Mat dilated;
    dilate(image, dilated, Mat(), cv::Point(-1, -1), 12);
    imshow("Dilated", dilated);
    waitKey(1000);
    vector<vector<Point> > contours;
    waitKey(1000);
    findContours(dilated, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);**
    vector<RotatedRect> areas(contours.size());
    for (auto contour : contours)
    {
        auto box = minAreaRect(contour);
        if (box.size.width < 20 || box.size.height < 20)
            continue;
        double prop = box.angle < -45 ? box.size.height / box.size.width : box.size.width / box.size.height;
        if (prop < 2)
            continue;
        areas.push_back(box);
    }
    return areas;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-05-09 04:50:41 -0500

bennog gravatar image

The dilate function needs the kernel not a empty Mat() and 12 iterations, it is better to increase the kernel size for better performance.

You initialize areas with contours.size. Later on you push the found box so the first part of areas is empty

findContours wants a thresholded image e.g. 0 = background and everything else is object. so you need to feed a threshold image to this function.

Don't use auto (it can be very confusing what the compiler makes from the auto)

One other option it could be the .lib and .dll are not matching with each other or with the compiler you use to compile this.

I have tested below modified code in VS2015 with opencv 3.4.1 ant it works without crashing.

std::vector<cv::RotatedRect> findTextAreas(cv::Mat image)
{
    Mat kernel = getStructuringElement(MORPH_CROSS, Size(3, 3));
    Mat dilated;
    dilate(image, dilated, kernel, cv::Point(-1, -1), 3);
    imshow("Dilated", dilated);
    waitKey(1000);
    std::vector<std::vector<cv::Point> > contours;
    waitKey(1000);
    findContours(dilated, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); 
    std::vector<cv::RotatedRect> areas;
    for (int i = 0; i < (int)contours.size(); i++)
    {
        cv::RotatedRect box = minAreaRect(contours[i]);
        if (box.size.width < 20 || box.size.height < 20)
            continue;
        double prop = box.angle < -45 ? box.size.height / box.size.width : box.size.width / box.size.height;
        if (prop < 2)
            continue;
        areas.push_back(box);
    }
    return areas;
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-03-17 05:26:13 -0500

Seen: 253 times

Last updated: May 09 '18