Python-opencv extracting contours of an image in the case of noise

Source: Internet
Author: User

For general image extraction outlines, this blog post describes a good method, but for noisy images, it is not very good to capture the target object.

For example, for my mouse, the extracted contour effect is not good, because the noise is many:

So this article adds the part that removes the noise.

First load the original image and display the image

1 img = cv2.imread ("temp.jpg")                # load Image 2 h, w = Img.shape[:2]                        # Gets the height  and width of the image 3 cv2.imshow ("Origin  ", img)                   # Show original image

The low-pass filter is then processed for noise reduction

1 blured = Cv2.blur (img, (5,5))                # filter to remove noise 2 cv2.imshow ("Blur  ", blured)                  # shows the image after low-pass filtering

Using FloodFill to remove the background around the target, the flood fill class starts with the PS Magic Wand tool, which is used to clear the background.

1 mask = Np.zeros ((h+2, w+2), np.uint8)       # masking length and width are two pixels more than the input image, flood fills will not exceed the mask's non 0 edge   2  # to flood fill 3 cv2.floodfill (blured, Mask, (w-1,h-1), (255,255,255), (2,2,2), (3,3,3), 8 )4 cv2.imshow ("floodfill", blured)  

and convert it into grayscale.

1 Gray = Cv2.cvtcolor (blured,cv2. Color_bgr2gray)  2 cv2.imshow ("Gray", Gray)  

At this time the target image is not smooth, there are some noise, so open and close operation, to get a more smooth target

1 # defining structural elements   2 kernel = cv2.getstructuringelement (cv2. Morph_rect, ((3)))# Opening and closing operations, first open operation to remove the background noise, and then continue the closed operation to fill the hole in the target 4 opened =  Cv2.morphologyex (Gray, Cv2. Morph_open, kernel)  5 closed = Cv2.morphologyex (opened, Cv2. Morph_close, kernel)  6 cv2.imshow ("closed", closed)  

Then converted to a two-value graph to make it easy to get an image's outline

Finally, the contour is extracted and the target is captured.

1 # Find Outlines 2 _,contours, hierarchy = cv2.findcontours (binary,cv2. Retr_tree,cv2. chain_approx_simple)  3# draw Contour 4 cv2.drawcontours (Img,contours,-1, ( 0,0,255), 3)  5# Draw result 6 cv2.imshow ("  Result", IMG)

The entire code is as follows

1 #Coding=utf-82 ImportCv23 ImportNumPy as NP4 5img = Cv2.imread ("temp.jpg")#Loading Images6H, W = img.shape[:2]#Get the height and width of the image7Cv2.imshow ("Origin", IMG)#Show Original Image8 9blured = Cv2.blur (img, (5,5))#filter to remove noiseTenCv2.imshow ("Blur", blured)#Show Low-pass filtered images One  AMask = Np.zeros ((h+2, w+2), np.uint8)#The mask length and width are two pixels more than the input image, and a full water fill will not exceed the mask's non-0 edge . - #to flood fill -Cv2.floodfill (blured, Mask, (w-1,h-1), (255,255,255), (2,2,2), (3,3,3), 8) theCv2.imshow ("FloodFill", blured) -  - #get a grayscale image -Gray =Cv2.cvtcolor (Blured,cv2. Color_bgr2gray) +Cv2.imshow ("Gray", Gray) -  +  A #Defining structural elements atKernel = cv2.getstructuringelement (cv2. Morph_rect, (50, 50)) - #opening and closing operations, first open operation to remove background noise, and then continue closed operation to fill the holes in the target -Opened =Cv2.morphologyex (Gray, Cv2. Morph_open, Kernel) -closed =Cv2.morphologyex (opened, Cv2. Morph_close, Kernel) -Cv2.imshow ("closed", closed) -  in #Two-value graph -RET, binary = Cv2.threshold (closed,250,255, Cv2. Thresh_binary) toCv2.imshow ("binary", binary) +  - #Find Outlines the_,contours, hierarchy =cv2.findcontours (Binary,cv2. Retr_tree,cv2. Chain_approx_simple) * #Drawing Outlines $ Panax NotoginsengCv2.drawcontours (Img,contours,-1, (0,0,255), 3)   - #Drawing Results theCv2.imshow ("result", IMG) +  A cv2.waitkey (0) theCv2.destroyallwindows ()

Python-opencv extracting contours of an image in the case of noise

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.