What does the output of findContours mean and how can I parse it?

Question:

When using the findContours() function I get an array. I am confused as to what the numbers in the array mean and also what a ‘dtype’ is.

code:

contour = str(cv2.findContours(edges, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE))
print(contour)

output:

([array([[[21, 21]],

       [[21, 90]],

       [[90, 90]],

       [[90, 21]]], dtype=int32), array([[[21, 22]],

       [[22, 21]],

       [[89, 21]],

       [[90, 22]],

       [[90, 89]],

       [[89, 90]],

       [[22, 90]],

       [[21, 89]]], dtype=int32), array([[[23, 23]],

       [[23, 88]],

       [[88, 88]],

       [[88, 23]]], dtype=int32), array([[[23, 24]],

       [[24, 23]],

       [[87, 23]],

       [[88, 24]],

       [[88, 87]],

       [[87, 88]],

       [[24, 88]],

       [[23, 87]]], dtype=int32)], array([[[-1, -1,  1, -1],
        [-1, -1,  2,  0],
        [-1, -1,  3,  1],
        [-1, -1, -1,  2]]], dtype=int32))

How would I go about parsing this array to use later? I plan on making a turtle application that reads instructions from a file containing image data. I want to know how I would be able to convert this into said instructions.

Asked By: yemi.JUMP

||

Answers:

cv2.findConturs()

cnt,heirarchy = cv2.findContours(edged_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)     # Finding contours detection 

This method returns two values. contours, and hierarchy. contours is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x,y) coordinates of boundary points of the object.

And in your case, it’s returning the same two values but the difference is that you are parsing it to string and getting it in a single variable which is making it hard to understand.(Read the fix part)

Dtype

Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about :

  • Type of the data (integer, float, Python object etc.)
  • Size of the data (number of bytes)
  • Byte order of the data (little-endian or big-endian)
  • If the data type is a sub-array, what is its shape and data type.

In your case, it is telling that the data inside ndarray is of int type along with array dimension

Fixing your code

In your code, the problem is with this part:

contour = str(cv2.findContours(edges, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE))

In this, you are parsing the values returned by findCountours to str type.

cnt,heirarchy = cv2.findContours(edged_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)     # Finding contours detection 

Edit ::

For drawing

If you want to draw all the detected contours then you have to use cv2.drawContours() method.

cv2.drawContours(image,contours,-1,(255,0,0),2)        # Drawing the detected contours on the image.

It takes 5 values. Its first argument is source image, the second argument is the contours which should be passed as a Python list, the third argument is the index of contours (useful when drawing the individual contour. To draw all contours, pass -1) and remaining arguments are color, thickness, etc.

If you want to see a python program rather which takes an image and draws contours(INFORMATION) on it then you can check this open Source code.

https://github.com/0xPrateek/ComputerVision-OpenCV3-Python/blob/master/contours%20detection.py

Answered By: 0xPrateek
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.