Example of extracting the image contour with python-opencv under noisy conditions, python-opencv Contour

Source: Internet
Author: User

Example of extracting the image contour with python-opencv under noisy conditions, python-opencv Contour

A good method is introduced for extracting the outlines of general images. However, for noisy images, the target objects cannot be captured well.

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

So this article adds the noise removal part.

First load the original image and display the image

Img = cv2.imread ("temp.jpg") # Load image h, w = img. shape [: 2] # obtain the height and width of the image cv2.imshow ("Origin", img)

Then perform low-pass filtering to reduce noise

Blured = cv2.blur (img, (5, 5) # remove noise by filtering cv2.imshow ("Blur", blured) # display the image after low-pass Filtering

Use floodfill to remove the background around the target. The flood filling class begins with the magic wand tool of ps, which is used to clear the background.

Then convert to grayscale

gray = cv2.cvtColor(blured,cv2.COLOR_BGR2GRAY) cv2.imshow("gray", gray) 

In this case, the target image is not smooth, and there is still some noise. Therefore, the open and closed operations are performed to obtain a smooth target.

# Define the structure element kernel = cv2.getStructuringElement (cv2.MORPH _ RECT, (50, 50) # enable and disable operations to remove background noise, CLOSE the operation and fill in the target hole opened = cv2.morphologyEx (gray, cv2.MORPH _ OPEN, kernel) closed = cv2.morphologyEx (opened, cv2.MORPH _ CLOSE, kernel) cv2.imshow ("closed ", closed)

Then convert the image to a two-value graph to obtain the image contour.

Finally, extract the contour and capture the target.

# Locate the contour _, S, hierarchy = cv2.findContours (binary, cv2.RETR _ TREE, cv2.CHAIN _ APPROX_SIMPLE) # Draw the contour cv2.drawContours (img, contours,-1, (255 ), 3) # Draw the result cv2.imshow ("result", img)

The Code is as follows:

# Coding = UTF-8 import cv2 import numpy as npimg = cv2.imread ("temp.jpg") # Load image h, w = img. shape [: 2] # obtain the height and width of the image cv2.imshow ("Origin", img) # display the original image blured = cv2.blur (img, (5, 5 )) # filter out noise cv2.imshow ("Blur", blured) # display the Image mask after low-pass filtering = np. zeros (h + 2, w + 2), np. uint8) # mask length and width than the input image more than two pixels, filled with water will not exceed the mask of non-zero edge # flooding fill cv2.floodFill (blured, mask, (W-1, h-1 ), (255,255,255), (, 2), (, 3, 3), 8) cv2.imshow ("floodfill", blured) # obtain the grayscale map gray = cv2.cvtColor (blured, cv2.COLOR _ BGR2GRAY) cv2.imshow ("gray", gray) # define the structure element kernel = cv2.getStructuringElement (cv2.MORPH _ RECT, (50, 50) # Open and Close operations, first enable operations to remove background noise, CLOSE the operation and fill in the target hole opened = cv2.morphologyEx (gray, cv2.MORPH _ OPEN, kernel) closed = cv2.morphologyEx (opened, cv2.MORPH _ CLOSE, kernel) cv2.imshow ("closed ", closed) # evaluate the binary Graph ret, BINARY = cv2.threshold (closed, 250,255, cv2.THRESH _ binary) cv2.imshow ("binary", binary) # Find the contour _, S, hierarchy = cv2.findContours (binary, cv2.RETR _ TREE, cv2.CHAIN _ APPROX_SIMPLE) # Draw the contour cv2.drawContours (img, contours,-1, (255,), 3) # Draw Results cv2.imshow ("result", img) cv2.waitKey (0) cv2.destroyAllWindows ()

The above python-opencv example of extracting the image contour when there is noise is all the content shared by xiaobian. I hope to give you a reference, and I hope you can also provide more support for helping customers.

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.