从帐单上移除邮票 python
Remove stamp from bill python
关于如何在 OCR 处理之前从该帐单上移除邮票有什么想法吗?
这是 Python/OpenCV 中的一种方法。
- 读取输入
- 黄色阈值
- 扩大以填满矩形
- 获取最大轮廓
- 在输入图像上绘制白色填充轮廓
- 保存结果
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('form_with_label.jpg')
# threshold on yellow
lower=(0,200,200)
upper=(100,255,255)
thresh = cv2.inRange(img, lower, upper)
# apply dilate morphology
kernel = np.ones((9,9), np.uint8)
mask = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
# get largest contour
contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
x,y,w,h = cv2.boundingRect(big_contour)
# draw filled white contour on input
result = img.copy()
cv2.drawContours(result,[big_contour],0,(255,255,255),-1)
# save cropped image
cv2.imwrite('form_with_label_thresh.png',thresh)
cv2.imwrite('form_with_label_mask.png',mask)
cv2.imwrite('form_with_label_removed.png',result)
# show the images
cv2.imshow("THRESH", thresh)
cv2.imshow("MASK", mask)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
阈值图像:
形态膨胀图像:
结果:
关于如何在 OCR 处理之前从该帐单上移除邮票有什么想法吗?
这是 Python/OpenCV 中的一种方法。
- 读取输入
- 黄色阈值
- 扩大以填满矩形
- 获取最大轮廓
- 在输入图像上绘制白色填充轮廓
- 保存结果
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('form_with_label.jpg')
# threshold on yellow
lower=(0,200,200)
upper=(100,255,255)
thresh = cv2.inRange(img, lower, upper)
# apply dilate morphology
kernel = np.ones((9,9), np.uint8)
mask = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
# get largest contour
contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
x,y,w,h = cv2.boundingRect(big_contour)
# draw filled white contour on input
result = img.copy()
cv2.drawContours(result,[big_contour],0,(255,255,255),-1)
# save cropped image
cv2.imwrite('form_with_label_thresh.png',thresh)
cv2.imwrite('form_with_label_mask.png',mask)
cv2.imwrite('form_with_label_removed.png',result)
# show the images
cv2.imshow("THRESH", thresh)
cv2.imshow("MASK", mask)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
阈值图像:
形态膨胀图像:
结果: