0

I have an image in which text is lighter and not clearly visible, i want to enhance the text and lighten then background using python pil or cv2. Help is needed. I needed to print this image and hence it has ro be clearer.

what i have done till now is below

from PIL import Image, ImageEnhance
im = Image.open('t.jpg')  
im2 = im.point(lambda p:p * 0.8)    
im2.save('t1.jpg')

Above has slightly darken the text but still i needed to make text clear and background whiter or lighter. The image is a photo of a printed document with text colour in black on white paper but that is not clearly visible. Below is snap of photo enter image description here

enter image description here

In image above behind and surrounding letters and words there is some kind of noise white and silver dots. How can i completely remove them so that text borders can be clearly visible.

I have also inverted image colours and given below sample enter image description here

I have tried following Adaptive thresolding ,(failed) Otsu method, (failed) Gaussian blurring, (failed) Colour segmentation, (failed) Connected component removal (failed) Denoising( failed because it also removed text) but nothing worked. Above all methods made it even worse because result of above methods gave me below image. Also text is affected when connected compoment method is used enter image description here

Please help is needed.

Also tried image magick morphology editing but failed How actually noise exist is shown in below image. It is continous rectangle bar filled with dots surrounding text ![enter image description here (https://i.stack.imgur.com/ZMHim.jpg)!

3 Answers3

0

Use image thresholding

You can use this example as well

from PIL import Image  
  
# creating a object  
im = Image.open(r"t.jpg").convert('LA')
  
# using point function 
threshold = 191  # range from 0 to 255
im = im.point(lambda p: p >value threshold and 255) 
im.show() 
Ando
  • 375
  • 2
  • 8
0

You can use denoising

img = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)

and then convert the image to grayscale

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

then dilate the image

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

The result

enter image description here

You can further explore on threshold to get better result

WenJuan
  • 624
  • 1
  • 8
  • 21
0

You can use Otsu binary thresholding:

import cv2

image = cv2.imread('input.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)[:,:,2]
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

cv2.imshow("Thresh", thresh)
cv2.waitKey(0)

The output:

enter image description here

amras
  • 1,499
  • 2
  • 6
  • 10