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()
|
原始圖:
遮罩:
結果:
沒有留言:
張貼留言