def process_image(self, cv_image, header, tag):
""" process the image """
hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
# mask for color range
if self.color_range:
mask = cv2.inRange(hsv, self.color_range[0], self.color_range[1])
count = cv2.countNonZero(mask)
if count:
kernel = np.ones((5, 5), np.uint8)
mask = cv2.dilate(mask, kernel, iterations=2)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
for i, c in enumerate(contours):
x, y, w, h = cv2.boundingRect(c)
if self.prefix is not None:
name = '{0}{1}_{2}_{3}.png'.format(self.prefix,
tag,
header.seq, i)
print name
roi = cv_image[y:y+h, x:x+w]
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
cv2.imwrite(name, gray)
for c in contours:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(cv_image, (x, y), (x+w, y+h), (0, 255, 0))
elif self.prefix is not None:
name = '{0}Negative_{1}_{2}.png'.format(self.prefix, tag,
header.seq, )
cv2.imwrite(name, cv_image)
cv2.namedWindow(tag, cv2.WINDOW_NORMAL)
cv2.resizeWindow(tag, 600, 600)
cv2.imshow(tag, cv_image)
cv2.waitKey(1)
评论列表
文章目录