[bug] cvFindContours can not find contour with left-top point at (0,0) (Bug #2362)


Added by lin lin over 12 years ago. Updated over 12 years ago.


Status:Cancelled Start date:
Priority:Normal Due date:
Assignee:Vadim Pisarevsky % Done:

0%

Category:imgproc, video
Target version:2.4.3
Affected version: Operating System:
Difficulty: HW Platform:
Pull request:

Description

version: OpenCV 2.0, OpenCV 2.4.2
Platform: Windows XP
Compilor: VS 2008

    if (m_pForeStorage)
        cvReleaseMemStorage(&m_pForeStorage);
    m_pForeStorage = cvCreateMemStorage(0);

    CvSeq* contours = NULL;

    IplImage * image_find = cvCloneImage(image);
    int nOut = cvFindContours(image_find,m_pForeStorage,&contours, sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);

    int contour_points_number = 0;
    CvSeq * obj_contour = NULL;

    for( CvSeq* c=contours; c!=NULL; c=c->h_next )
    {
        printf("%d ", c->total);
        if (contour_points_number < c->total)
        {
            contour_points_number = c->total;
            obj_contour = c;
        }
    }

    if (obj_contour == NULL)
        return 1;


Now the obj_contour is the contour that gets most point number.
For the attatchment white.png, the output contour is expected: (0,0), (0, 1920), (1920,1080), (1080, 0).
But the function return the error contour: (1,1),........

white.png (29 kB) lin lin, 2012-09-18 06:01 am


Associated revisions

Revision 57d96990
Added by Vadim Pisarevsky over 12 years ago

added the note about 1-pixel border in findContours (relates to ticket #2362)

Revision a0a4f6c2
Added by Roman Donchenko about 11 years ago

Merge pull request #2362 from alekcac:docs_style_change

History

Updated by Andrey Pavlenko over 12 years ago

  • Description changed from version: OpenCV 2.0, OpenCV 2.4.2 Platform: Windows XP Compilor: VS 2008 /*... to version: OpenCV 2.0, OpenCV 2.4.2 Platform: Windows XP Compilor: VS 2008 <... More

Updated by Andrey Pavlenko over 12 years ago

  • Start date deleted (2012-09-18)
  • Description changed from version: OpenCV 2.0, OpenCV 2.4.2 Platform: Windows XP Compilor: VS 2008 <... to version: OpenCV 2.0, OpenCV 2.4.2 Platform: Windows XP Compilor: VS 2008 <... More
  • Subject changed from [bug] cvFindContours can not find contour whose leftop point at (0,0)! to [bug] cvFindContours can not find contour with left-top point at (0,0)

Updated by Andrey Pavlenko over 12 years ago

Looks like a bug or a methods restriction, look at the code:

import cv2
import numpy as np

img = np.zeros((100, 200), dtype=np.uint8)
cv2.rectangle(img, (0, 0), (199, 99), 255)
contours, h = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print contours

Result: []
But:
img = np.zeros((100, 200), dtype=np.uint8)
cv2.rectangle(img, (1, 1), (198, 98), 255)
cv2.rectangle(img, (0, 0), (199, 99), 255)
contours, h = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print contours

Result:
[array([[[  1,   1]],

       [[  1,  98]],

       [[198,  98]],

       [[198,   1]]])]

Updated by Andrey Pavlenko over 12 years ago

  • Category set to imgproc, video
  • Priority changed from High to Normal

Updated by Andrey Pavlenko over 12 years ago

  • Assignee set to Vadim Pisarevsky

Updated by Vadim Pisarevsky over 12 years ago

Hello!
this is limitation of the method - pixel-wide border of the image is ignored. The note about it was added into reference manual. If you absolutely need to detect such contours in your image, you can pad the image with zero border and pass (-1,-1) offset to findContours:

...
/*IplImage * image_find = cvCloneImage(image);*/
IplImage * image_find = cvCreateImage(cvSize(image->width+2,image->height+2), 8, 1);
cvCopyMakeBorder(image, image_find, cvPoint(1,1), CV_BORDER_CONSTANT);
int nOut = cvFindContours(image_find,m_pForeStorage,&contours, sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE, cvPoint(-1,-1));
  • Status changed from Open to Cancelled
  • Target version set to 2.4.3

Also available in: Atom PDF