LineSegmentDetector throws exception on resized image

asked 2017-12-07 03:35:31 -0600

Grillteller gravatar image

updated 2017-12-07 06:34:35 -0600


another question concerning the LineSegmentDetector. I have one image and detect lines in it with the LineSegmentDetector. After that I resize the image and try the same but I get the error:

OpenCV Error: Assertion failed ((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at, file C:\opencv\opencv-master\modules\core\include\opencv2/core/mat.inl.hpp, line 978

Here is my code:

#include "stdafx.h"
#include <opencv2/line_descriptor.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>

using namespace cv;
using namespace cv::xfeatures2d;
using namespace cv::line_descriptor;

int main(int argc, char** argv)

    // Load two images and modify them  
    Mat img_1, img_2;   
    img_1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);   
    img_2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);

    if (! || ! {
        cout << "Error reading image" << endl;
        return EXIT_FAILURE;

    Mat img_1_half;
    vector<Vec4f> lines_1, lines_1_half;
    Ptr<LineSegmentDetector> Line_Detector = createLineSegmentDetector();

    cout << "First image..." << endl;
    Line_Detector->detect(img_1, lines_1);

    cout << "Second image..." << endl;
    resize(img_1, img_1_half, Size(img_1.cols / 2, img_1.rows / 2));    
    Line_Detector->detect(img_1_half, lines_1_half);    

return 0;
edit retag flag offensive close merge delete


Size(img_1.rows / 2, img_1.cols / 2) i'm almost sure, you wanted Size(img_1.cols / 2, img_1.rows / 2)

(not that it matters here..)

berak gravatar imageberak ( 2017-12-07 04:20:25 -0600 )edit

Yes, I corrected it, but still the same error ;)

Grillteller gravatar imageGrillteller ( 2017-12-07 05:53:45 -0600 )edit

sorry, cannot reproduce it ;(

berak gravatar imageberak ( 2017-12-07 06:01:12 -0600 )edit

Really weird, I added my headerfiles and namespaces now. I get the same error if I follow the example on

Grillteller gravatar imageGrillteller ( 2017-12-07 06:36:49 -0600 )edit

Try applying a canny edge before running the detector.

eshirima gravatar imageeshirima ( 2017-12-07 06:55:21 -0600 )edit

Same error with Canny edge operation before using Line_Detector->detect

Grillteller gravatar imageGrillteller ( 2017-12-07 07:18:32 -0600 )edit

Is the error thrown at the first image or the resized one? I'd so recommend checking your image as well. Confirm the number of channels, view them to see if they everything is working fine before running the detectors etc

eshirima gravatar imageeshirima ( 2017-12-07 07:35:44 -0600 )edit

I think I remember that a vector wasn't resized when the image is scaled down. So it's size was from calculation before and runs out of bound, but I can't find the place in source code.

matman gravatar imagematman ( 2017-12-07 14:00:10 -0600 )edit

let's switch to full-on paranoid mode :)

since you're obviously using widows: go and triple-check your linker settings. any chance you're using opencv debug libs with a release project (or other way round) ? crt not the same as the libs ?

your code probably runs fine for anyone else (assuming from a single sample: me) , there's no obvious flaw in it

berak gravatar imageberak ( 2017-12-07 14:05:57 -0600 )edit

@berak I can confirm as well that the code works on my mac

eshirima gravatar imageeshirima ( 2017-12-07 14:50:29 -0600 )edit