[英]Remove noise from image using OpenCV
I have these images我有这些图片
enter image description here在此处输入图像描述
enter image description here在此处输入图像描述
I want to remove noise from these images so I can convert them into text using pytesseract.我想从这些图像中去除噪音,以便我可以使用 pytesseract 将它们转换为文本。 The noise is only in blue colour so I tried to remove blue from the image.
噪点只有蓝色,所以我试图从图像中去除蓝色。 Still not good results.
还是没有好结果。
This is what I did
import cv2
import pytesseract
# Extract the blue channel
blue = img[:, :, 0]
# Apply thresholding to the blue channel
thresh = cv2.threshold(blue, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# Perform morphological operations to remove noise
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,1))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=7)
# Apply blur to smooth out the image
blur = opening#cv2.medianBlur(opening, 1)
cv2.imwrite("/Users/arjunmalik/Desktop/blur.png",blur)
display("/Users/arjunmalik/Desktop/blur.png")
The result was结果是
enter image description here在此处输入图像描述
The OCR results were FL1S4y. OCR 结果为 FL1S4y。
As stated by Sembei, You need to use a closing operator which's a must for a situation like this because you want to close black points on the object to improve the image quality.正如 Sembei 所说,您需要使用关闭运算符,这对于这种情况是必须的,因为您想要关闭 object 上的黑点以提高图像质量。
Solution:解决方案:
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4,4))
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
You can modify your code to this one to achieve the following output for the second image.您可以将您的代码修改为此代码,以实现第二张图片的以下 output。
Output: Output:
You might need to change the size of the kernel for different input images.您可能需要为不同的输入图像更改 kernel 的大小。
Thoughts:想法:
I think it'd be better if you do the character segmentation first before applying the closing operator in order to achieve the finest results.我认为最好先进行字符分割,然后再应用关闭运算符以获得最佳结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.