Python image Processing (7): Using contour chunking

Source: Internet
Author: User

Happy Shrimp

Welcome reprint, but please keep the author information

After getting the foreground image of the green plants, we hope to further identify the cotton plants and weeds. The test image is still it:

The first thing to do, of course, is to process the image in a sub-region. In the previous step we got a two-value image that identifies green plants, and a natural idea is to use the contour of this binary image for chunking.

# to get the contour, our aim is to block, so only use the outer contour, using the form of point sequence Bin_img_save = Np.copy (bin_img) (contoures, hierarchy) = Cv2.findcontours (bin_img_ Save, Cv2. Retr_external, Cv2. Chain_approx_none)

It is important to note that the use of the findcontours The image must be copied one copy before the function changes the incoming image data.

the results of the calculations are actually thearea, we don't really care about areas that are too small, we calculate area sizes and sort them from large to small. OpenCVprovides two sorting functions,Sortand theSortidx, whereSortyou can get an array of sorted values, andSortidxcan get the ordinal number of these values in the original array, so this is usedSortidx.

# Sort by Area areas = Np.zeros (len (contoures)) idx = 0for cont in contoures:     areas[idx] = Cv2.contourarea (cont)    idx = i DX + 1areas_s = cv2.sortidx (areas, Cv2. sort_descending | Cv2. Sort_every_column)

then the processing area is greater than - the area:

(B8, G8, r8) = Cv2.split (src) # Processing for each zone for IDX in areas_s:    if AREAS[IDX] <: Break    # draws an area image by Thickn The ESS is set to 1 to fill the entire area, otherwise only draw edges    poly_img = Np.zeros (bin_img.shape, dtype = np.uint8)    cv2.drawcontours (poly_img, Contoures, IDX, [255,255,255],-1)    poly_img = poly_img & bin_img    # Get the color image    color_img = Cv2.merge ([B8 & ; Poly_img, G8 & Poly_img, R8 & Poly_img])    cv2.imshow (' poly_img ', color_img)    Cv2.waitkey ()

At the moment we are merely displaying the color images of this area. Finally get the result:

We need to correctly identify whether the two areas are cotton or weeds.


Python image Processing (7): Using contour chunking

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: 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.