OpenCV-去除图像中的噪点 [英] OpenCV - Removal of noise in image

查看:2323
本文介绍了OpenCV-去除图像中的噪点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一张桌子的图像..在右列中,背景充满了噪声

I have an image here with a table.. In the column on the right the background is filled with noise

如何检测有噪点的区域?我只想在有噪声的零件上使用某种滤波器,因为我需要对它进行OCR,任何一种滤波器都会降低整体识别度

How to detect the areas with noise? I only want to apply some kind of filter on the parts with noise because I need to do OCR on it and any kind of filter will reduce the overall recognition

哪种滤镜最能去除图像中的背景噪声?

And what kind of filter is the best to remove the background noise in the image?

如上所述,我需要对图像进行OCR

As said I need to do OCR on the image

推荐答案

我在OpenCV中尝试了一些过滤器/操作,但效果似乎很好.

I tried some filters/operations in OpenCV and it seems to work pretty well.

第1步:放大图像-

kernel = np.ones((5, 5), np.uint8)
cv2.dilate(img, kernel, iterations = 1)

如您所见,噪点消失了,但是字符很轻,所以我腐蚀了图像.

As you see, the noise is gone but the characters are very light, so I eroded the image.

第2步:腐蚀图像-

kernel = np.ones((5, 5), np.uint8)
cv2.erode(img, kernel, iterations = 1)

如您所见,杂音消失了,但是其他列上的某些字符坏了.我建议仅在嘈杂的列上运行这些操作.您可能要使用 HoughLines 查找最后一列.然后,您只能提取该列,进行扩散+腐蚀,然后将其替换为原始图像中的相应列. 此外,膨胀+侵蚀实际上是称为 关闭 的操作.您可以使用-

As you can see, the noise is gone however some characters on the other columns are broken. I would recommend running these operations on the noisy column only. You might want to use HoughLines to find the last column. Then you can extract that column only, run dilation + erosion and replace this with the corresponding column in the original image. Additionally, dilation + erosion is actually an operation called closing. This you could call directly using -

cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

正如@Ermlg所建议的那样,具有3内核的midBlur也可以很好地工作.

As @Ermlg suggested, medianBlur with a kernel of 3 also works wonderfully.

cv2.medianBlur(img, 3)

替代步骤

如您所见,所有这些滤波器都起作用,但是最好仅在有噪声的部分实现这些滤波器.为此,请使用以下命令:

As you can see all these filters work but it is better if you implement these filters only in the part where the noise is. To do that, use the following:

edges = cv2.Canny(img, 50, 150, apertureSize = 3) // img is gray here
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, 1000, 50) // last two arguments are minimum line length and max gap between two lines respectively.
for line in lines: 
    for x1, y1, x2, y2 in line: 
        print x1, y1
// This gives the start coordinates for all the lines. You should take the x value which is between (0.75 * w, w) where w is the width of the entire image. This will give you essentially **(x1, y1) = (1896, 766)**

然后,您只能像这样提取该部分:

Then, you can extract this part only like :

extract = img[y1:h, x1:w] // w, h are width and height of the image

然后,在此图像中实现过滤器(中间值或关闭值).消除噪点后,需要将此滤波后的图像替换为原始图像中的模糊部分. image [y1:h,x1:w] =中位数

Then, implement the filter (median or closing) in this image. After removing the noise, you need to put this filtered image in place of the blurred part in the original image. image[y1:h, x1:w] = median

这在C ++中很简单:

This is straightforward in C++ :

extract.copyTo(img, new Rect(x1, y1, w - x1, h - y1))

使用其他方法的最终结果

希望对您有帮助!

这篇关于OpenCV-去除图像中的噪点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆