我正在使用輪廓過濾器去除補丁邊緣的黑點,但問題仍然存在。有什么辦法可以去掉這些點嗎?
綠色污點的提取結果:

這是已經疊加在背景影像上的影像,您可以看到邊緣上的黑點:

這是提取綠色污點的代碼:
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
背景圖片:


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)
形態學脫粒:

灰色結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/462349.html
