【发布时间】:2018-10-24 21:06:38
【问题描述】:
我正在尝试分割下图的数字和/或字符,然后使用 ocr 将每个单独的 num/char 转换为文本:
这是使用的代码(在python中):
new, contours, hierarchy = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
digitCnts = []
final = gray.copy()
# loop over the digit area candidates
for c in contours:
(x, y, w, h) = cv2.boundingRect(c)
# if the contour is sufficiently large, it must be a digit
if (w >= 20 and w <= 290) and h >= (gray.shape[0]>>1)-15:
x1 = x+w
y1 = y+h
digitCnts.append([x,x1,y,y1])
#print(x,x1,y,y1)
# Drawing the selected contour on the original image
cv2.rectangle(final,(x,y),(x1,y1),(0, 255, 0), 2)
plt.imshow(final, cmap=cm.gray, vmin=0, vmax=255)
我得到以下输出:
您会看到除了中间的 2 之外,所有的都被正确检测到,只有顶部有边界框,而不是整个数字周围。我无法弄清楚为什么只有这个没有正确检测到,尤其是它与其他的相似。知道如何解决这个问题吗?
【问题讨论】:
-
尝试绘制轮廓,看看它提取了什么。
-
我画了包围矩形。我画轮廓而不是?我试试看
-
图像应该是二值的,而不是灰色的,如果是灰色最好将其转换为具有一定阈值的黑白。
-
嗯,它是二进制的。实际上,当我在尝试检测之前将图像延迟为二进制转换时,我解决了这个问题。图像最初在转换后旋转。也许,二进制转换后的旋转对数字检测的影响比以前更大。也许我需要总是将二进制转换延迟到最后。
标签: opencv bounding-box opencv-contour