Home > database >  Finding contour points in subpixel in OpenCV
Finding contour points in subpixel in OpenCV

Time:12-22

If we find contours for an image using cv2.findContours(), the co-ordinates present in each contour are of datatype of int32. Example: Printing contours[0] gives,

array([[[0,1]],
       [[1,1]],
          .
          .
          .
       [[400,232]]])

Is there any way to find the co-ordinates in the contour with the higher precision (subpixel)?

array([[[0.11,1.78]],
       [[1.56,1.92]],
          .
          .
          .
       [[400.79,232.35]]])

CodePudding user response:

In principle, in a properly sampled gray-scale image you do have information to guess at the location with sub-pixel precision. But it’s still a guess. I’m not aware of any existing algorithms that try to do this, I only know of algorithms that trace the contour in a binary image.

There have been a few papers published (that I’m aware of) that try to simplify the outline polygon in a way that you get a better representation of the underlying contour. These papers all make an assumption of smoothness that allows them to accomplish their task, without such an assumption there is no other information in the binary image than what is extracted by the simple pixel contour algorithm. The simplest method in this category is smoothing the polygon: move each vertex a little closer to the line formed by its two neighboring vertices.

If the goal however is to get more precise measurements of the object, then there are several different approaches to use the gray-value data to significantly improve the precision of these measurements. I’m familiar with two of them:

  • L.J. van Vliet, “Gray-scale measurements in multi-dimensional digitized images”, PhD thesis Delft University of Technology, 1993. PDF
  • N. Sladoje and J. Lindblad, “High Precision Boundary Length Estimation by Utilizing Gray-Level Information”, IEEE Transactions on Pattern Analysis and Machine Intelligence 31(2):357-363, 2009. DOI

The first one does area, perimeter and local curvature in 2D (higher dimensions lead to additional measurements), based on the assumption of a properly sampled band-limited image. The latter does length, but is the start point of the “pixel coverage” model: the same authors have papers also on area and Feret diameters. This model assumes area sampling of a sharp boundary.

  • Related