我有一張有污點(水面上的綠色污點)的影像,我的目標是提取該污點。它用
嘗試:
img = cv2.imread('/content/001.jpg')
# blur
blur = cv2.GaussianBlur(img, (3,3), 0)
# convert to hsv and get saturation channel
sat = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)[:,:,1]
# threshold saturation channel
thresh = cv2.threshold(sat, 90, 255, cv2.THRESH_BINARY)[1]
# apply morphology close and open to make mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
mask = cv2.morphologyEx(morph, cv2.MORPH_OPEN, kernel, iterations=1)
# do OTSU threshold to get circuit image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)[1]
# write black to otsu image where mask is black
otsu_result = otsu.copy()
otsu_result[mask==0] = 0
# write black to input image where mask is black
img_result = img.copy()
img_result[mask==0] = 0
結果:

uj5u.com熱心網友回復:
我遵循了您的方法,但改用了 LAB 顏色空間。
img = cv2.imread(image_path)
# blur
blur = cv2.GaussianBlur(img, (3,3), 0)
# convert to LAB space and get a-channel
lab = cv2.cvtColor(blur, cv2.COLOR_BGR2LAB)
a = lab[:,:,1]
thresh = cv2.threshold(a, 95, 255, cv2.THRESH_BINARY_INV)[1]
thresh = cv2.threshold(lab[:,:,1], 95, 255, cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel, iterations=1)
inv_morph = cv2.bitwise_not(morph)
img_result = img.copy()
img_result[inv_morph==0] = 0
cv2.imshow('Final result', img_result)

結果不準確。您可以更改閾值和/或形態學內核以獲得所需的結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457148.html
上一篇:Facebook客戶端OAuth設定域不適用于查詢字串
下一篇:在網路攝像頭視頻上創建多個ROI
