Ask Your Question
0

Errors in Contour detection

asked 2017-02-21 10:44:31 -0600

Phalcon gravatar image

updated 2017-02-21 13:59:36 -0600

Hi

I am doing some simple contour detection, thresholding the images then using findcontours(). To test it, I've created a simple image in paint, manually colouring in 4 pixels hence having a known area and other more complex examples. When I run this through, everything seems offset slightly, in the case of a 2x2 square, this comes out to have an area of 1. Clearly I'm missing something. Any suggestions? Example below(to scale). I feed in the image on the left, 3x3 pixels wide. Looking at the coordinates after findcontours(), the result is only 2x2 pixels wide. I can see this in the raw polygons as well as the bitmap output.

Example attached

image description

    private void GeneratePolygon()
    {
        Mat img = Cv2.ImRead(RawImagePath, ImreadModes.GrayScale);
        Cv2.Threshold(img, img, Threshold, 255, ThresholdTypes.Binary);
        CvPoint[][] Contours;
        HierarchyIndex[] hierarchyIndexes;
        Cv2.FindContours(img, out Contours, out hierarchyIndexes, RetrievalModes.List, ContourApproximationModes.ApproxNone);
edit retag flag offensive close merge delete

Comments

Suggestions : add an image in your post

LBerger gravatar imageLBerger ( 2017-02-21 12:53:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-02-21 13:39:55 -0600

LBerger gravatar image

Without knowing opencv version and platform and without anycode it is difficult to understand your problem.

using opencv 3.2-dev and VS 2015 windows 10 this code gives expecting result

    Mat img(10,10,CV_8UC1,Scalar::all(0));
    vector<vector<Point>> ctr;
    vector<cv::Vec4i> hierarchy;
    for (int i = 3; i <= 5; i++)
        for (int j = 3; j <= 5; j++)
            img.at<uchar>(i, j) = 1;
    findContours(img,ctr, hierarchy,CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
    for (int i = 0; i < ctr.size(); i++)
    {
        cout<<"\n ctr : "<<i<<"\n";
        for (int j=0;j<ctr[i].size();j++)
            cout<<ctr[i][j]<<"\t";
    }

ctr : 0 [3, 3] [3, 4] [3, 5] [4, 5] [5, 5] [5, 4] [5, 3] [4, 3]

edit flag offensive delete link more

Comments

I think I've got it, it's a misunderstanding on my part. I was expecting a contour to trace around the edge of a even a single pixel, i.e. a single pixel would return 4 coordinates. Seemingly it returns the centre points. This results in the reduced area than I was expecting. Any suggestions on how I would get the functionality I need? The code I'm running is above (opencvsharp 3.2)

Phalcon gravatar imagePhalcon ( 2017-02-21 13:55:15 -0600 )edit

I cannot answer about opencvsharp because it is not good forum

About your question relative or image processing contour of one pixel: it is a bad idea and you should forget it. If contour of one pixel is 4 pixels then you will change definition of what is a contour. What is a contour of 4 pixels now ...

LBerger gravatar imageLBerger ( 2017-02-21 14:21:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-21 10:44:31 -0600

Seen: 305 times

Last updated: Feb 21 '17