Import cv2img = Cv2.imread (' E:\A.jpeg ') cv2.imshow (' img ', img) Gray = Cv2.cvtcolor (img, Cv2. Color_bgr2gray) ret, binary = Cv2.threshold (gray, 127, 255, Cv2. thresh_binary) contours, hierarchy = Cv2.findcontours (BINARY, Cv2. Retr_tree, Cv2. chain_approx_simple) cv2.drawcontours (IMG, contours,-1, (3) cv2.imshow (' IMG1 ', img) cv2.waitkey (0) C V2.destroyallwindows ()
cv2.findcontours () function:
number of references:
1 ( is the image looking for contour)
There are four ways to refer to 2 (the retrieval mode for outlines):
(1). Cv2. Retr_external only detects external contours
(2). Cv2. Retr_list detection of the contour does not establish a hierarchical relationship
(3). Cv2. Retr_ccomp establishes two levels of contour,
the first layer above is an outer boundary.
The layer inside the is the bounding information of the inner hole.
assuming another connected object inside the hole,
the boundary of this object is also on the top floor.
(4). Cv2. Retr_tree Create a hierarchical tree structure with the outline
3 (method is the approximate method of contour):
(1) Cv2. Chain_approx_none stores all the contour points,
the pixel position difference of two adjacent points does not exceed 1,
that is Max (ABS (X1-X2), ABS (Y2-Y1)) ==1
(2). Cv2. Chain_approx_simple the horizontal direction of compression.
vertical, diagonal elements,
only retains the end coordinates of that direction.
For example, a rectangular outline requires only 4 points to hold contour information
(3). Cv2. Chain_approx_tc89_l1,cv_chain_approx_tc89_kcos
use TEH-CHINL chain Approximate algorithm
return value:
value 1:
is the contour itself.
Value 2:
The corresponding properties for each contour.
OpenCV--Get contour Photos