[Opencv-python] Image processing Part IV (IV.) in OpenCV

Source: Internet
Author: User
Tags python list

The contours in the OpenCV


21.1 Initial Knowledge Contour
Goal
? Understand what outlines are
? Learn to look for outlines, draw outlines, etc.
? Function: Cv2.findcontours (), cv2.drawcontours ()


21.1.1 what is a contour
A contour can be simply thought of as a curve that connects successive points (contiguous boundaries) with the same, color, or grayscale. Profiles are useful for shape analysis and object detection and recognition.
? To be more accurate, use two to value the image. Threshold processing or Canny boundary detection is needed before the contour is searched.
? Functions that look up outlines modify the original image. If you want to use the original diagram, like, after you've found the outline, you should store the original image in another variable.
? In OpenCV, look for contours like ultra-white objects in a black background. You should remember that the object you are looking for should be white and the background should be black.
Let's look at how to find the contour in a binary image:
The function cv2.findcontours () has three parameters, the first one is the input image, the second is the contour retrieval mode, and the third is the contour approximation method. The return value has three, the first is the image, the second is the contour, and the third is the (contour) chromatography structure. The outline (the second return value) is a Python list that stores all the outlines in the image. Each contour is a Numpy array that contains the coordinates of the object's boundary point (x, y).
Note: The second and third parameters, as well as the hierarchy, are described in detail behind us. Until then, the parameter values used in the example are applicable to all images.


21.1.2 how to draw outlines
The function cv2.drawcontours () can be used to draw outlines. It can draw any shape based on the boundary points you provide. Its first parameter is the original image, the second parameter is the outline, and a Python list. The third parameter is the index of the outline (it is useful to draw a separate outline when you set it to 1 to draw all outlines). The next parameter is the color and thickness of the contour.
Draw all the contours on an image:

Import numpy as Npimport cv2im = cv2.imread (' test.jpg ') Imgray = Cv2.cvtcolor (im,cv2. Color_bgr2gray) Ret,thresh = Cv2.threshold (imgray,127,255, 0) image, contours, hierarchy = cv2.findcontours ( Thresh,cv2. Retr_tree,cv2. Chain_approx_simple) draw an independent contour, such as a fourth profile: IMG = Cv2.drawcontour (img, contours,-1, (0,255,0), 3) but most of the time, the following method is more useful: IMG = Cv2.drawcontours (IMG, contours, 3, (0,255,0), 3)      

Note: The last two methods are the same, but the knowledge behind will tell you that the last method is more useful.


Approximate method of 21.1.3 Contour
This is the third parameter of the function cv2.findcountours (). What does it mean?
Above we have already mentioned that the contour is a shape with the same gray value boundary. It stores all (x, y) coordinates on the shape boundary. But do you need to store all of these boundary points? This is what this parameter tells the function to Cv2.findcontours.
This parameter is set to CV2. Chain_approx_none, all boundary points will be stored. But do we really need that much? For example, when we look for a boundary that is a straight line. Do you use all the dots in the line to represent the line? No, we just need the two ends of the line. This is CV2. Chain_approx_simple to do. It removes the redundant points on the contour, compresses the contours, and saves memory overhead. We use the rectangle in the to demonstrate this technique. Draws a blue circle on each coordinate in the contour list. The first figure shows the use of CV2. The effect of Chain_approx_none, altogether 734 points. The second figure is the use of CV2. The result of Chain_approx_simple is only 4 points. See how powerful he is!

[Opencv-python] Image processing Part IV (IV.) in OpenCV

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.