gpt4 book ai didi

python - Opencv:从许可证中裁剪文本区域

转载 作者:太空狗 更新时间:2023-10-29 17:33:02 25 4
gpt4 key购买 nike

我有一张驾驶执照的下图,我想提取有关驾驶执照、姓名、出生日期等的信息。我的想法是找到一种方法将它们逐行分组,然后裁剪出单个矩形其中包含 eng 和 ara 的名称、许可证等。但我失败得很惨。

enter image description here

import cv2
import os
import numpy as np

scan_dir = os.path.dirname(__file__)
image_dir = os.path.join(scan_dir, '../../images')


class Loader(object):
def __init__(self, filename, gray=True):
self.filename = filename
self.gray = gray
self.image = None

def _read(self, filename):
rgba = cv2.imread(os.path.join(image_dir, filename))

if rgba is None:
raise Exception("Image not found")

if self.gray:
gray = cv2.cvtColor(rgba, cv2.COLOR_BGR2GRAY)

return gray, rgba


def __call__(self):
return self._read(self.filename)


class ImageScaler(object):

def __call__(self, gray, rgba, scale_factor = 2):
img_small_gray = cv2.resize(gray, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_AREA)
img_small_rgba = cv2.resize(rgba, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_AREA)


return img_small_gray, img_small_rgba



class BoxLocator(object):
def __call__(self, gray, rgba):
# image_blur = cv2.medianBlur(gray, 1)
ret, image_binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
image_not = cv2.bitwise_not(image_binary)

erode_kernel = np.ones((3, 1), np.uint8)
image_erode = cv2.erode(image_not, erode_kernel, iterations = 5)

dilate_kernel = np.ones((5,5), np.uint8)
image_dilate = cv2.dilate(image_erode, dilate_kernel, iterations=5)


kernel = np.ones((3, 3), np.uint8)
image_closed = cv2.morphologyEx(image_dilate, cv2.MORPH_CLOSE, kernel)
image_open = cv2.morphologyEx(image_closed, cv2.MORPH_OPEN, kernel)

image_not = cv2.bitwise_not(image_open)
image_not = cv2.adaptiveThreshold(image_not, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, -2)

image_dilate = cv2.dilate(image_not, np.ones((2, 1)), iterations=1)
image_dilate = cv2.dilate(image_dilate, np.ones((2, 10)), iterations=1)

image, contours, heirarchy = cv2.findContours(image_dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
# if w > 30 and h > 10:
cv2.rectangle(rgba, (x, y), (x + w, y + h), (0, 0, 255), 2)

return image_dilate, rgba



def entry():
loader = Loader('sample-004.jpg')
# loader = Loader('sample-004.jpg')
gray, rgba = loader()

imageScaler = ImageScaler()
image_scaled_gray, image_scaled_rgba = imageScaler(gray, rgba, 1)

box_locator = BoxLocator()
gray, rgba = box_locator(image_scaled_gray, image_scaled_rgba)

cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
cv2.namedWindow('Image2', cv2.WINDOW_NORMAL)

cv2.resizeWindow('Image', 600, 600)
cv2.resizeWindow('Image2', 600, 600)

cv2.imshow("Image2", rgba)
cv2.imshow("Image", gray)

cv2.moveWindow('Image', 0, 0)
cv2.moveWindow('Image2', 600, 0)

cv2.waitKey()
cv2.destroyAllWindows()

当我运行上面的代码时,我得到了下面的分割。这与我想要的不接近

enter image description here

但下面是我想要实现的,对于所有输入许可证 enter image description here

最佳答案

在我的脑海中,我可以想到两种方法:

方法 1. 如评论中所述,您可以裁剪左上角的鹰符号和右上角的旗帜,将它们用作模板,然后根据找到的模板的位置找到您感兴趣的两个框,左下角(小框)和中心(大框)。作为开始,你可以使用这个:

模板一

Template 1

模板 2

Template 2

代码:

import numpy as np
import cv2
import matplotlib.pyplot as plt

image = cv2.imread("ID_card.jpg")

template_1 = cv2.imread("template_1.jpg", 0)
w_1, h_1 = template_1.shape[::-1]

template_2 = cv2.imread("template_2.jpg", 0)
w_2, h_2 = template_2.shape[::-1]

res_1 = cv2.matchTemplate(image=image, templ=template_1, method=cv2.TM_CCOEFF)
min_val_1, max_val_1, min_loc_1, max_loc_1 = cv2.minMaxLoc(res_1)

res_2 = cv2.matchTemplate(image=image, templ=template_2, method=cv2.TM_CCOEFF)
min_val_2, max_val_2, min_loc_2, max_loc_2 = cv2.minMaxLoc(res_2)

cv2.rectangle(image, max_loc_1, (max_loc_1[0] + w_1, max_loc_1[1] + h_1), 255, 2)
cv2.rectangle(image, max_loc_2, (max_loc_2[0] + w_2, max_loc_2[1] + h_2), 255, 2)

结果:

Result Template

您可以使用找到的模板的中心来获取所需框(小框和大框)的相对位置。

方法 2. 与您基于轮廓所做的类似,基本思想是使用形态学在更大的盒子中获得明确的线条。

代码:

import numpy as np
import cv2
import matplotlib.pyplot as plt

image = cv2.imread("ID_card.jpg")
imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 150, 255, 0)
# cv2.imwrite("thresh.jpg", thresh)

# Morphological operation
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN,
cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7)))

im2, contours, heirarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)

# Sort the contours based on area
cntsSorted = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)

approxes = []

for cnt in cntsSorted[1:10]:
peri = cv2.arcLength(cnt, True)
# approximate the contour shape
approx = cv2.approxPolyDP(cnt, 0.04 * peri, True)
approxes.append(approx)
if len(approx) == 4:
# length of 4 means 4 vertices so it should be a quadrilateral
cv2.drawContours(image, approx, -1, (0, 255, 0), 10)

cv2.imwrite("ID_card_contours.jpg", image)
print(approxes)

结果:

阈值图像

Thresholded

形态开运算后

Closed

最终图像,两个预期框的各自角标有绿色

Final image

所以,这种方法非常简单,我相信您可以完成剩下的工作,从大盒子中找到较小的子集。如果没有,请给我留言,我很乐意提供帮助(基本上从图像中裁剪该区域,使用 HoughlinesP 应该没问题。或者,我可以看到较小的子集具有相同的宽度,因此您可以只需根据 y 坐标裁剪它们)

附言。希望“更大”、“更小”的盒子被很好地理解,为我的懒惰没有在图像中显示它们而道歉。

注意:只给出一张图像,我不能确定它是否适用于数据集中的所有图像。您可能需要调整 thresholdmorph_open 参数。如果您可以上传更多图片,我可以试穿。

礼貌:OpenCV shape detection用于检测轮廓中的形状。

关于python - Opencv:从许可证中裁剪文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53151293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com