我有这种类型的图像,我只想从中提取字符。
二值化后,我得到这个图像
img = cv2.imread('the_image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 9)
然后找到该图像上的轮廓。
(im2, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
for contour in cnts[:2000]:
x, y, w, h = cv2.boundingRect(contour)
aspect_ratio = h/w
area = cv2.contourArea(contour)
cv2.drawContours(img, [contour], -1, (0, 255, 0), 2)
我正进入(状态
我需要一种方法来过滤轮廓,以便它只选择字符。所以我可以找到边界框并提取 roi。
我可以找到轮廓并根据区域的大小对其进行过滤,但源图像的分辨率不一致。这些图像是从移动相机拍摄的。
此外,由于盒子的边界已断开。我无法准确地检测到这些盒子。
Edit:
如果我取消选择长宽比小于 0.4 的框。然后它会在一定程度上发挥作用。但我不知道它是否适用于不同分辨率的图像。
for contour in cnts[:2000]:
x, y, w, h = cv2.boundingRect(contour)
aspect_ratio = h/w
area = cv2.contourArea(contour)
if aspect_ratio < 0.4:
continue
print(aspect_ratio)
cv2.drawContours(img, [contour], -1, (0, 255, 0), 2)