我正在尝试使用 inRange 来发现柠檬上的黑色污渍。 由于某些原因,我无法找到正确的HSV值(我曾尝试使用GIMP和photoshop并没有成功)
我在C ++中找到了类似的参考文章:http://answers.opencv.org/question/72176/detect-red-pimples-on-face-using-opencv/
到目前为止,我的代码很短:
image = cv2.imread("./data/lemon1big.jpg")
# parse BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([20,5,5],np.uint8)
upper = np.array([68,70,68],np.uint8)
imageHsv = cv2.inRange(imageHsv, lower, upper)
imageAdaptive = cv2.adaptiveThreshold(imageHsv,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,2)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(1,1))
imageDilate = cv2.dilate(imageAdaptive,kernel)
result, contours, hierarchy = cv2.findContours(imageDilate.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) != 0:
for (i, c) in enumerate(contours):
area = cv2.contourArea(c)
if (area > 700 and area < 1000):
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(result,(x,y),(x+w,y+h),(0,255,0),12)
这是原始的蒙版照片:
我寻找的结果看起来像这样(大约12个黑色污点):