OpenCV--Get contour Photos

Source: Internet
Author: User



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.