Python Opencv : Filter Image for Text Detection

标签 python opencv machine-learning computer-vision contour

unfiltered image

我有这些我想要去噪的图像集,以便在其上运行 OCR:

我正在尝试从图像中读取 1973。

我试过了

import cv2,numpy as np


img=cv2.imread('uxWbP.png',0)
img = cv2.resize(img, (0, 0), fx=2, fy=2)
copy_img=np.copy(img)
#adaptive threshold as the image has different lighting conditions in different areas
thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 2)

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#kill small contours
for i_cnt, cnt in enumerate(sorted(contours, key=lambda x: cv2.boundingRect(x)[0])):
    _area = cv2.contourArea(cnt)
    x, y, w, h = cv2.boundingRect(cnt)
    x_y_area = w * h
    if 10000 < x_y_area and x_y_area < 400000:
        pass
        # cv2.rectangle(copy_img, (x, y), (x + w, y + h), (255, 0, 255), 2)
        # cv2.putText(copy_img, str(int(x_y_area)) + ' , ' + str(w) + ' , ' + str(h), (x, y + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
        # cv2.drawContours(copy_img, [cnt], 0, (0, 255, 0), 1)
    elif 10000 > x_y_area:
        #write over small contours
        cv2.drawContours(thresh, [cnt], -1, 255, -1)

cv2.imshow('img',copy_img)
cv2.imshow('thresh',thresh)
cv2.waitKey(0)

这显着改善了图像:

filtered image

关于如何充分过滤此图像的任何建议,无论是对过滤图像的改进还是从一开始就完全更改,我可以在此上运行 OCR 或一些 ML 检测脚本?我想拆分出用于检测的数字,但也对其他方法开放。

最佳答案

另一件事要尝试 - 与 the blurring 分开(或与之结合 - 是 erosion/dilation game ,正如@eldesgraciado 在 the comment 中暗示的那样,我认为这些答案的很大一部分应该归功于他们。

这两个(腐 eclipse 和膨胀)可以一个接一个地重复应用。我认为诀窍是改变内核大小。无论如何,我知道我过去曾用它来减少噪音。下面是一个膨胀的例子:

>>> import cv2
>>> import numpy as np
>>> im_0 = cv2.imread("FWM8b.png")
>>> k_size = 3
>>> kernel = np.ones((k_size, k_size), np.uint8)
>>> im_dilated = cv2.dilate(im_0, kernel, iterations=1)
>>> cv2.imshow("d", im_dilated)
>>> cv2.waitKey(0)

Quick Dilation

制作任何你想要侵 eclipse 的内核,并检查效果。

>>> im_eroded = cv2.erode(im_0, kernel, iterations=1)
>>> cv2.imshow("erosion", im_eroded)
>>> cv2.waitKey(0)

编辑 可能的改进:

>>> im_blurred = cv2.GaussianBlur(im_dilated, (0, 0), 3)
>>> im_better = cv2.addWeighted(im_0, 0.5, im_blurred, 1.2, 0) 
# Getting closer.

Dilated, blurred, and combined with original, 1st

^ d 翻译, b 引诱,并结合( a dded)与原始, 1 圣路

# Even better, I think.
im_better2 = cv2.addWeighted(im_0, 0.9, im_blurred, 1.7, 0)

Dilated, blurred, and combined with original, 2

^ d 翻译, b 引诱,并结合( a dded)与原始, 2 方式

您可以进行工件去除,但注意不要摆脱 7 的茎。如果您可以将 7 保持在一起,则可以进行连通分量分析并保留最大的连通分量。

您可以对每一列和每一行的像素值求和,这可能会导致类似的结果(非常近似 - 几乎是工作时间)。请注意,我对绿色曲线 - 列的总和 - 更加小心,但缩放的一致性可能已关闭。

Sums of rows and columns

请注意,这更多的是 (255 - pixel_value) 的总和。这可以在您要找到的字形(数字)所在的位置找到矩形。你可以做一个 column_pixel_sum + row_pixel_sum 的二维 map ,或者只是做一些近似,就像我在下面所做的那样。

Sums and rectangles

也可以随意旋转图像(或以不同角度获取像素总和),并为每次旋转组合您的信息。

还有很多其他的东西可以尝试...... suggestion @eldesgraciado 的噪声模型特别有趣。

Another thing you could try out is to create a "noise model" and subtract it from the original image. First, take the image and apply Gaussian Blur with very low parameters, just barely blurring it, next subtract this mask from the image. From here, the steps are experimental: The difference should be again blurred and thresholded. Save this image. You run this pre-processing with various parameters and saving each time the final binary image, then, average the masks obtained so far. The persistent blobs should be the ones you are looking for... like some sort of spatial bandstop, I guess...



继续实验。

Unsharp mask (my other answer) on this result image .更多的噪音消失了,但伤害了 7。

关于Python Opencv : Filter Image for Text Detection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60312191/

相关文章:

python - 找不到符号 : __PyCodecInfo_GetIncrementalDecoder

image-processing - 如何计算图像是否有噪声和几何失真?

opencv - 在图像上查找硬币

python - 从IP摄像机馈送OpenCV的VideoCapture发生故障

python - 支持向量机: Python Error Message

python - 使用 while 循环重复生成随机数的函数

Python:正则表达式元素与列表匹配

python - 如何找到真实数据的概率分布和参数? (Python 3)

python - CondaVerificationError : || ClobberError: Create a new conda environment with Python 2. 7.x 或 3.7.x

python - Flask request.form ["name"] 仅返回一个 token