咖啡豆辨識, 先設計打光場景, 背景用單色, 先用HSV色彩過濾得到一個初步遮罩, 再用 morphology處理遮罩, 把遮罩內的洞補起來, 並且縮小邊緣, 以獲得完全不含背景色的圖片
import cv2
import numpy as np
pic = 'pic/b2.jpg'
img=cv2.imread(pic,cv2.IMREAD_COLOR)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_coffee = np.array([0,0,0])
upper_coffee = np.array([60,255,180])
mask = cv2.inRange(hsv, lower_coffee, upper_coffee)
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(13,13))
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(15,15))
#mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.dilate(mask ,kernel1)
mask = cv2.erode(mask, kernel2)
out = cv2.bitwise_and(img, img, mask= mask)
cv2.imshow("img",img)
cv2.imshow('mask',mask)
cv2.imshow("out",out)
cv2.waitKey(0)
cv2.destroyAllWindows()
|
原始圖:
遮罩:
結果:
全站熱搜
留言列表