Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why are contours points stored two times(Contour clockwise and then in clockwise) in numpy array?

I want to know how the the contours are stored in opencv. The stored points seems to be like stack. Where in the last object detected is the first object that is printed out.

Here we can see that the points are first stored in the contour clockwise direction the first time, and then in the clockwise direction the second time round

ORIGINAL IMAGE

ORIGINAL IMAGE

PROCESSED IMAGE

PROCESSED IMAGE

Here the python code

import cv2
import numpy as np

s_img = cv2.imread("SimpleShape.jpg")

e_img = cv2.Canny(s_img,1,10)

im2, contours, hi =cv2.findContours(e_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

print(contours)

Output:

(I'll only show the first object that this prints as the list is very long)

[array([[[126, 279]],

   [[125, 280]],

   [[124, 281]],

   [[124, 282]],

   [[124, 283]],

   [[123, 284]],

   [[122, 284]],

   [[121, 284]],

   [[120, 284]],

   [[119, 284]],

   [[118, 284]],

   [[117, 284]],

   [[116, 284]],

   [[115, 285]],

   [[114, 286]],

   [[114, 287]],

   [[114, 288]],

   [[115, 289]],

   [[116, 289]],

   [[117, 289]],

   [[118, 289]],

   [[119, 289]],

   [[120, 289]],

   [[121, 289]],

   [[122, 289]],

   [[123, 289]],

   [[124, 289]],

   [[125, 289]],

   [[126, 289]],

   [[127, 289]],

   [[128, 289]],

   [[129, 289]],

   [[129, 288]],

   [[129, 287]],

   [[129, 286]],

   [[129, 285]],

   [[129, 284]],

   [[129, 283]],

   [[129, 282]],

   [[129, 281]],

   [[129, 280]],

   [[128, 279]],

   [[127, 279]]], dtype=int32), array([[[125, 280]],

   [[126, 279]],

   [[127, 279]],

   [[128, 279]],

   [[129, 280]],

   [[129, 281]],

   [[129, 282]],

   [[129, 283]],

   [[129, 284]],

   [[129, 285]],

   [[129, 286]],

   [[129, 287]],

   [[129, 288]],

   [[128, 289]],

   [[127, 289]],

   [[126, 289]],

   [[125, 289]],

   [[124, 289]],

   [[123, 289]],

   [[122, 289]],

   [[121, 289]],

   [[120, 289]],

   [[119, 289]],

   [[118, 289]],

   [[117, 289]],

   [[116, 289]],

   [[115, 289]],

   [[114, 288]],

   [[114, 287]],

   [[114, 286]],

   [[115, 285]],

   [[116, 284]],

   [[117, 284]],

   [[118, 284]],

   [[119, 284]],

   [[120, 284]],

   [[121, 284]],

   [[122, 284]],

   [[123, 284]],

   [[124, 284]],

   [[124, 283]],

   [[124, 282]],

   [[124, 281]]], dtype=int32), array([[[ 61, 269]],