问题描述
我正在使用OpenCV和Python处理图像。我需要从图像中删除点/噪声。
我试过扩张使点变小,但是文字被损坏了。我也试过两次循环扩张和侵蚀一次。但这并没有给出令人满意的结果。
还有其他方法可以达到这个目的吗?
谢谢:)
I'm processing the images with OpenCV and Python. I need to remove the dots / noise from the image.I tried dilation which made the dots smaller, however the text is being damaged. I also tried looping dilation twice and erosion once. But this did not give satisfactory results.Is there someother way i can achieve this?Thank you :)
编辑:我是图像处理的新手。我目前的代码如下:
I'm new to image processing. My current code is as follows
image = cv2.imread(file)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kernel = np.ones((2, 2), np.uint8)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
gray = cv2.erode(gray, kernel, iterations=1)
gray = cv2.dilate(gray, kernel, iterations=1)
cv2.imwrite(file.split('.'[0]+"_process.TIF", gray))
编辑2:我尝试了中位数模糊。它解决了90%的问题。我一直在使用gaussianBlurring。
谢谢
EDIT 2: I tried median blurring. It has solved 90% of the issue. I had been using gaussianBlurring all this while. Thank you
推荐答案
如何使用 connectedComponentsWithStats删除小型连接组件
import cv2
import numpy as np
img = cv2.imread('path_to_your_image', 0)
_, blackAndWhite = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(blackAndWhite, 4, cv2.CV_32S)
sizes = stats[1:, -1] #get CC_STAT_AREA component
img2 = np.zeros((labels.shape), np.uint8)
for i in range(0, nlabels - 1):
if sizes[i] >= 50: #filter small dotted regions
img2[labels == i + 1] = 255
res = cv2.bitwise_not(img2)
cv2.imwrite('res.png', res)
编辑:
参见
See also
这篇关于如何在不损坏文本的情况下消除点/噪音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!