Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove noise in image OpenCV, Python?

I have some cropped images and I need images that have black texts on white background. Firstly I apply adaptive thresholding and then I try to remove noise. Although I tried a lot of noise removal techniques but when the image changed, the techniques I used failed.

enter image description here

The best method for converting image color to binary for my images is Adaptive Gaussian Thresholding. Here is my code:

im_gray = cv2.imread("image.jpg",  cv2.IMREAD_GRAYSCALE)
image = cv2.GaussianBlur(im_gray, (5,5), 1)
th =  cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,3,2)

enter image description here

I need smooth values, Decimal separator(dot) and postfix letters. How can I do this?

like image 916
Ugurcan Avatar asked Dec 01 '22 13:12

Ugurcan


1 Answers

Before binarization, it is necessary to correct the nonuniform illumination of the background. For example, like this:

import cv2

image = cv2.imread('9qBsB.jpg')
image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
se=cv2.getStructuringElement(cv2.MORPH_RECT , (8,8))
bg=cv2.morphologyEx(image, cv2.MORPH_DILATE, se)
out_gray=cv2.divide(image, bg, scale=255)
out_binary=cv2.threshold(out_gray, 0, 255, cv2.THRESH_OTSU )[1] 

cv2.imshow('binary', out_binary)  
cv2.imwrite('binary.png',out_binary)

cv2.imshow('gray', out_gray)  
cv2.imwrite('gray.png',out_gray)

Result: enter image description here enter image description here

like image 194
Alex Alex Avatar answered Dec 06 '22 19:12

Alex Alex