有一种任务是从单据或者是书籍的图片中找到相应的感兴趣区域,并将其提取出来以作后续的使用;示例如下所述:
比如上述红圈所描述的区域,提取出人名等有用信息。
解决这种问题的算法可以按照下面的过程进行:
1. 图像二值化
2. 简单去噪声干扰
3. 找到闭合区域,并用闭合区域的面积进行过滤选取
另附代码如下:
import cv2
# 图像预处理
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]
# 排除干扰
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
cnts = cv2.findContours(opening, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# 使用面积进行过滤提取
ROI_number = 0
for c in cnts:
area = cv2.contourArea(c)
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
x,y,w,h = cv2.boundingRect(approx)
if len(approx) == 4 and (area > 1000) and (area < 80000):
ROI = image[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
ROI_number += 1
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.waitKey()
效果如下: