PYTHON+OPENCV image Processing (16)--Contour Discovery

Source: Internet
Author: User
Tags function prototype

Summary: Contour discovery is a method based on image edge extraction to find the contour of an object, so the threshold selection of edge extraction affects the final contour discovery results.

The code is as follows:

ImportCv2 as CVImportNumPy as NPdefContours_demo (image): DST= CV. Gaussianblur (Image, (3, 3), 0)#Gaussian Blur denoisingGray =Cv.cvtcolor (DST, CV. Color_rgb2gray) ret, binary= Cv.threshold (Gray, 0, 255, CV. Thresh_binary | Cv. Thresh_otsu)#image binary using large law and global adaptive threshold methodCv.imshow ("binary Image", binary) clonetmage, contours, Heriachy=cv.findcontours (binary, CV. Retr_external, CV. Chain_approx_simple) forI, ContourinchEnumerate (contours): cv.drawcontours (image, Contours, I, (0, 0,255), 2)        Print(i) cv.imshow ("Contours", image) forI, ContourinchEnumerate (contours): cv.drawcontours (image, Contours, I, (0, 0,255),-1) Cv.imshow ("pcontours", image) src= Cv.imread ('e:/imageload/coins.jpg') Cv.namedwindow ('Input_image', CV. Window_normal)#set to Window_normal can be arbitrarily scaledCv.imshow ('Input_image', SRC) contours_demo (src) cv.waitkey (0) cv.destroyallwindows ()

Operation Result:

Attention:

1.OPENCV Discovery Contour Function Prototype: findcontours (image, Mode, method[, contours[, hierarchy[, offset]]), image, contours, Hierarchy

The image parameter represents a 8-bit single-channel image matrix, which can be a grayscale image, but more commonly a two-value image, typically a two-value image that has been processed by edge detection operators such as canny and Laplace.

The mode parameter represents the contour retrieval pattern:

①cv_retr_external: Detects only the outermost contour, and the inner contour that is contained within the perimeter contour is ignored.

②cv_retr_list: Detects all contours, including inner and outer contours, but the detected contours do not establish hierarchical relationships, are independent from each other, and do not have hierarchical relationships, which means that there is no parent contour or inline outline in this retrieval mode.

③cv_retr_ccomp: All contours are detected, but all contours are only two hierarchical, with the periphery being the top layer, and if the inner contour of the periphery contains other contour information, all the contours within the inner circumference belong to the top layer.

④cv_retr_tree: Detects all contours, creates a hierarchical tree structure for all contours, the outer contour contains an inner contour, and the inner contour can continue to contain an inline contour.

The method parameter represents an approximation of the contour:

①cv_Chain_approx_none stores all the contour points, the pixel position difference of the adjacent two points is not more than 1, that is, Max (ABS (X1-X2), ABS (y2-y1) = = 1.

②cv_Chain_approx_simple Compresses horizontal, vertical, diagonal elements, preserving only the end-point coordinates of that direction, such as a rectangular outline with only 4 points to hold the contour information.

③cv_Chain_approx_tc89_l1,cv_chain_approx_tc89_kcos uses the Teh-chinl CHAIN approximation algorithm.

The contours parameter is a list that represents the collection of points stored for each profile.

The hierarchy parameter is the same number of elements and outlines in a list,list, each contour contours[i] corresponds to 4 hierarchy elements hierarchy[i][0] ~hierarchy[i][3], respectively, representing the latter contour, The index number of the previous outline, parent outline, inline outline, or negative if there is no corresponding item.

The offset parameter represents an optional offset for each contour point movement.

The function prototypes for 2.OPENCV draw outlines are: drawcontours (image, contours, contouridx, color[, thickness[, linetype[, hierarchy[, maxlevel[, Offset]], image

The Imgae parameter represents the target image.

The contours parameter represents all the input outlines.

The CONTOURIDX parameter indicates which outline is drawn in the outline list, and if it is negative, all outlines are drawn.

The color parameter represents the colors of the outline.

The thickness parameter represents the outline line thickness drawn, and if it is negative, the interior of the contour is drawn.

The Linetype parameter represents a linetype.

The hierarchy parameter represents optional information about the hierarchy.

The Maxlevel parameter represents the maximum level for drawing outlines. If 0, only the specified outline is drawn. If 1, the function draws the outline and all nested outlines. If 2, the function draws outlines, all nested outlines, all nested to nested outlines, and so on. This parameter is considered only if there is a hierarchy available.

The offset parameter represents an optional contour offset parameter that moves all sketched outlines in the specified manner.

PYTHON+OPENCV image Processing (16)--Contour Discovery

Related Article

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.