【发布时间】:2021-08-21 16:50:25
【问题描述】:
我有一批像这里这样的截图:
我尝试用六位数字检测区域并识别它们。第二部分就像一个魅力。我在检测正确区域时遇到问题,因为它可以根据屏幕尺寸进行移位。例如,裁剪图像如下所示:
看起来还不错,但我必须在代码中添加一些解决方法才能选择正确的位置。
我的代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 6))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8))
# Load and resize image to standard size
img0 = Image.open('./data/test.png')
img0.thumbnail((720, 1423))
img = np.array(img0)
# The magic from https://www.pyimagesearch.com/2017/07/17/credit-card-ocr-with-opencv-and-python/
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)
gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
gradX = np.absolute(gradX)
(minVal, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255 * ((gradX - minVal) / (maxVal - minVal)))
gradX = gradX.astype("uint8")
gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
locs = []
for (i, c) in enumerate(cnts):
(x, y, w, h) = cv2.boundingRect(c)
ar = w / h
if x > 140 and x < 220 and w > 100 and h > 12 and h < 20 and ar >= 4 and ar <= 6:
locs.append((x, y, w, h))
# Calculate the crop rectangle
LEFT_TOP = (181, 316)
RIGHT_BOTTOM = (299, 346)
if len(locs) > 0:
(x, y, w, h) = locs[0]
LEFT_TOP = (x - 5, y - 5) # workaround place
RIGHT_BOTTOM = (x + w + 5, y + h) # workaround place
print(LEFT_TOP, RIGHT_BOTTOM)
img1 = img0.crop(LEFT_TOP + RIGHT_BOTTOM)
选定的轮廓看起来像:
它选择一个小于实际区域的轮廓。为什么?如何解决?
谢谢!
【问题讨论】:
-
执行您的代码时,我收到
locs = []。您很可能没有发布'./data/test.png',而是发布了其他图片。请将import语句添加到您发布的代码中。 -
添加了导入行和测试图片。
-
现在我在添加
img1.save('./data/digits.png')后得到了不错的数字。这是result。好的,我看到了“解决方法” -
是的,如果您删除解决方法'+5',结果裁剪文件将切断部分数字。