将小的反锯齿数字与背景分开

时间:2019-02-06 15:08:04

标签: python opencv

我正在尝试将数字与背景分开,我目前正在使用这种方法:

源图像:Source Image

import cv2
import numpy as np

frame = cv2.imread('so1.png',0)
frame = cv2.adaptiveThreshold(frame, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 7, 0)

adaptiveThreshold之后:After adaptiveThreshold

nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(frame, connectivity=4)
valid_label = []
for idx in range(nlabels):
     left,top,width,height,area = stats[idx]
     if 3 <= width <= 10 and 5<= height <= 12 and 5<= top<= 9 and area > 17:
        valid_label.append(idx)

display = np.zeros(frame.shape)
for y in range(frame.shape[0]):
    for x in range(frame.shape[1]):
        display[y,x] = 255 if labels[y,x] in valid_label else 0

Final image

这有些用,但是在大多数情况下,它会消除很多。

这是Samples

的列表

0 个答案:

没有答案