def detect(self, image, mask = None):
floatimage = np.float32(image)
fb,fg,fr = cv2.split(floatimage)
# red-to-blue channel operation
ra = fr + fb
rb = fr - fb
rb[ra > 0] /= ra[ra > 0]
#mi = np.min(rb)
#ma = np.max(rb)
#rb = np.uint8((rb - mi) / (ma - mi) * 255)
# morphology open
if self.kernel is None or self.kernel.shape[0] != Configuration.background_rect_size:
self.kernel = np.ones((Configuration.background_rect_size, Configuration.background_rect_size), np.uint8) * 255
result = cv2.morphologyEx(rb, cv2.MORPH_OPEN, self.kernel)
# background subtraction
# homogeneous background image V
result = rb - result
mi = np.min(result)
ma = np.max(result)
result = np.uint8((result - mi) / (ma - mi) * 255)
# adaptive threshold T
T, _ = cv2.threshold(result[mask == 0], 0, 1, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# V(i, j) > T
return np.uint8((T - np.float32(result)) <= 0)
评论列表
文章目录