Home > Mobile >  How to remove black dots from the edge of the patch?
How to remove black dots from the edge of the patch?

Time:04-14

I am using the contour filter to remove the black dots on the edge of the patch, but the problem persists. Is there any way to remove those dots?

The result of the extraction of the green stain: enter image description here

And this is the image already superimposed on the background image and you can see the black dots on the edge: enter image description here

This is the code to extract the green stain:

image = cv2.imread('/content/frame_patch.jpg')

img_hsv=cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

#color boundaries [H, S, V]
lower = (44, 30, 10)
upper = (120, 255, 255)

# threshold on green color
thresh = cv2.inRange(img_hsv, lower, upper)

# get largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# draw
mask = np.zeros_like(image)
cv2.drawContours(mask, [big_contour], 0, (255,255,255), -1)

img_result = image.copy()
img_result[mask==0] = 0

The background image: enter image description here

enter image description here

import cv2
import numpy as np

image = cv2.imread('stain.png')

img_hsv=cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

#color boundaries [H, S, V]
lower = (44, 30, 10)
upper = (120, 255, 255)

# threshold on green color
thresh = cv2.inRange(img_hsv, lower, upper)

# use morphology to (optionally close and then) erode the thresh image
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel)

# get largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# draw
mask = np.zeros_like(image)
cv2.drawContours(mask, [big_contour], 0, (255,255,255), -1)

img_result = image.copy()
img_result[mask==0] = 128

# write results to disk
cv2.imwrite("stain_mask.png", mask)
cv2.imwrite("stain_result.png", img_result)

# display it
cv2.imshow("thresh", thresh)
cv2.imshow("result", img_result)
cv2.waitKey(0)

Thresh with Morphology:

enter image description here

Result on gray:

enter image description here

  • Related