Python 如何选择六位数字组的正确轮廓

Python 如何选择六位数字组的正确轮廓,python,opencv,ocr,Python,Opencv,Ocr,我有一批屏幕截图,如下所示: 我试着用六个数字来检测区域并识别它们。第二部分很有魅力。我在检测正确的区域时遇到问题,因为它可以根据屏幕尺寸进行移位。例如,裁剪图像如下所示: 看起来还可以,但我必须在代码中添加一些变通方法来选择正确的位置 我的代码: import cv2 import numpy as np from matplotlib import pyplot as plt from PIL import Image rectKernel = cv2.getStructuringEle

我有一批屏幕截图,如下所示:

我试着用六个数字来检测区域并识别它们。第二部分很有魅力。我在检测正确的区域时遇到问题,因为它可以根据屏幕尺寸进行移位。例如,裁剪图像如下所示:

看起来还可以,但我必须在代码中添加一些变通方法来选择正确的位置

我的代码:

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)
选定的轮廓看起来像:

它选择一个小于实际区域的轮廓。为什么?如何修复它

谢谢大家!

测试文件:
软件中没有魔法

不合适的过滤器会侵蚀你的部分手指

拆下tophat滤清器。 拆下索贝尔过滤器。 用cv2.THRESH_BINARY_INV替换cv2.THRESH_BINARY。 增加sqKernel的大小。 我建议您绘制等高线,并显示或保存中间结果以供测试

以下是修改后的代码:

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))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 11))

# 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.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | 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 = []


# Draw contours for testing
tmp_im = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
cv2.drawContours(tmp_im, cnts, -1, (0, 255, 0), 1)  # Draw green line around the contour
cv2.imshow('tmp_im', tmp_im)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite('./data/tmp_im.png', tmp_im)


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
    LEFT_TOP = (x, y) # workaround place
    RIGHT_BOTTOM = (x + w, y + h) # workaround place

print(LEFT_TOP, RIGHT_BOTTOM)
img1 = img0.crop(LEFT_TOP + RIGHT_BOTTOM)

img1.show()
img1.save('./data/digits.png')
结果:

测试用tmp_img:

执行代码时,我得到LOC=[]。很可能您没有发布“./data/test.png”,而是发布了其他图像。请将导入语句添加到您发布的代码中。添加了导入行和测试图像。现在,在添加img1.save.'/data/digits.png'后,我得到了不错的数字。这是你的电话号码。好的,我看到了变通方法的位置是的,如果删除变通方法“+5”,结果裁剪文件将剪切部分数字。