Python-opencv extracts the contour of an image when there is noise, and python-opencv outlines
For general image contour extraction, this blog introduces a good method, but for noisy images, the target object 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
1 img = cv2.imread ("temp.jpg") # load the image for 2 h, w = img. shape [: 2] # obtain the height and width of the Image 3 cv2.imshow ("Origin", img) # display the original image
Then perform low-pass filtering to reduce noise
1 blured = cv2.blur (img, (5, 5) # filter and remove noise 2 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.
1 mask = np. zeros (h + 2, w + 2), np. uint8) # The mask length and width are two more pixels than the input image, the pan flood fill will not exceed the non-zero edge mask 2 # The Pan flood fill 3 cv2.floodFill (blured, mask, (W-1, h-1), (255,255,255), (, 2), (, 3), 8) 4 cv2.imshow ("floodfill", blured)
Then convert to grayscale
1 gray = cv2.cvtColor(blured,cv2.COLOR_BGR2GRAY) 2 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.
1 # define the structure element 2 kernel = cv2.getStructuringElement (cv2.MORPH _ RECT, (50, 50) 3 # Open and Close operations first to remove background noise, CLOSE the operation to fill in the target hole 4 opened = cv2.morphologyEx (gray, cv2.MORPH _ OPEN, kernel) 5 closed = cv2.morphologyEx (opened, cv2.MORPH _ CLOSE, kernel) 6 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.
1 # locate outline 2 _, contours, hierarchy = cv2.findContours (binary, cv2.RETR _ TREE, cv2.CHAIN _ APPROX_SIMPLE) 3 # Draw outline 4 cv2.drawContours (img, contours,-1, 255), 3) 5 # draw result 6 cv2.imshow ("result", img)
The Code is as follows:
1 # coding = UTF-8 2 import cv2 3 import numpy as np 4 5 img = cv2.imread ("temp.jpg") # Load image 6 h, w = img. shape [: 2] # obtain the height and width of the image 7 cv2.imshow ("Origin", img) # display the original image 8 9 blured = cv2.blur (img, (5, 5 )) # filter out noise 10 cv2.imshow ("Blur", blured) # display the image 11 12 mask = np after low-pass filtering. 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 the non-zero edge 13 # flooding fill 14 cv2.floodFill (blured, mask, (W-1, h-1), (255,255,255), (, 2), (, 3), 8) 15 cv2.imshow ("floodfill", blured) 16 17 # obtain the grayscale image 18 gray = cv2.cvtColor (blured, cv2.COLOR _ BGR2GRAY) 19 cv2.imshow ("gray", gray) 20 21 22 # define the structure element 23 kernel = cv2.getStructuringElement (cv2.MORPH _ RECT, (50, 50) 24 # Open and Close operations, first open operations to remove background noise, continue to CLOSE the operation and fill in the target hole 25 opened = cv2.morphologyEx (gray, cv2.MORPH _ OPEN, kernel) 26 closed = cv2.morphologyEx (opened, cv2.MORPH _ CLOSE, kernel) 27 cv2.imshow ("closed", closed) 28 29 # evaluate the binary Graph 30 ret, BINARY = cv2.threshold (closed, 250,255, cv2.THRESH _ binary) 31 cv2.imshow ("binary", binary) 32 33 # locate outline 34 _, S, hierarchy = cv2.findContours (binary, cv2.RETR _ TREE, cv2.CHAIN _ APPROX_SIMPLE) 35 # Draw outline 36 37 cv2.drawContours (img, contours,-1, (255,), 3) 38 # drawing result 39 cv2.imshow ("result", img) 40 41 cv2.waitKey (0) 42 cv2.destroyAllWindows ()