def process_image(image):
"""
Args:
image: The image to process
Returns:
sub_image: The rotated and extracted.
"""
# Convert image to black and white - we cannot take the photos in black and white as we
# must first search for the red triangle.
if len(image.shape) == 3:
processed_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
else:
processed_img = image
if config.real_hardware:
num_iterations = 8
else:
num_iterations = 8
processed_img = cv2.GaussianBlur(processed_img, (21, 21), 0)
_, processed_img = cv2.threshold(processed_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Put a border around the image to stop the edges of the images creating artifacts.
padded_image = np.zeros((processed_img.shape[0] + 10, processed_img.shape[1] + 10), np.uint8)
padded_image[5:processed_img.shape[0]+5, 5:processed_img.shape[1]+5] = processed_img
kernel = np.array([[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0]], np.uint8)
padded_image = cv2.erode(padded_image, kernel, iterations=num_iterations)
processed_img = padded_image[25:padded_image.shape[0] - 25, 25:padded_image.shape[1] - 25]
#cv2.imshow('Padded Image', padded_image)
#cv2.imshow('Processed image', processed_img)
#cv2.waitKey(0)
# Debugging code - useful to show the images are being eroded correctly.
#spacer = processed_img[:, 0:2].copy()
#spacer.fill(100)
#combined_image = np.concatenate((processed_img, spacer), axis=1)
#combined_image = np.concatenate((combined_image, image), axis=1)
#cv2.imshow('PreProcessed and Processed Image', combined_image)
#cv2.waitKey(0)
# Save sub_image to debug folder if required.
if __debug__:
iadebug.save_processed_image(processed_img)
return processed_img
评论列表
文章目录