假设区域是通过鼠标单击选择的,请按照以下步骤进行操作:
示例代码:
import cv2
import numpy as np
# original image
image = cv2.imread('image.png')
# mask (of course replace corners with yours)
mask = np.zeros(image.shape, dtype=np.uint8)
roi_corners = np.array(points, dtype=np.int32) #pointsOf the polygon Like [[(10,10), (300,300), (10,300)]]
white = (255, 255, 255)
cv2.fillPoly(mask, roi_corners, white)
# apply the mask
masked_image = cv2.bitwise_and(image, mask)
# display your handywork
cv2.imshow('masked image', masked_image)
cv2.waitKey()
cv2.destroyAllWindows()
为了找到ROI的颜色,您可以根据需要使用以下两种方法之一:
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray,mask = mask)
mean_val = cv2.mean(im,mask = mask)