3

I have three types of images and want to segment text from them. So I get a clean binarized img like the first image below. The three types of images are below

I've tried various techniques but it always have some cases to fail. I tried first to threshold the img using otsu algorithm but it gave bad results in images below

I tried Guassian, bilateral and normal blur kernel but didn't enhance the results too much

Any one can provide help!

Code as the best I got results from

import cv2

gray = cv2.imread("/home/shrouk/Pictures/f2.png", 0)
thresholded = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
cv2.imshow("img", thresholded)

This is the final result I need enter image description here

This the first type of images that fail. It fails because the text grey level it lighter in the right of the image

enter image description here

The result of otsu on it is here, I just need a way to enhance the words in the third line from right:

enter image description here

Second type that fails because the darker background

enter image description here

otsu result is not very good as the words to the left look like dilated words

enter image description here

This is the type that correctly thresholded by otsu as there is no noise

enter image description here

shrouk mansour
  • 381
  • 3
  • 16

1 Answers1

2

Try using cv2.adaptiveThreshold()

enter image description here enter image description here

import cv2

image = cv2.imread("2.png", 0)
adaptive = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,5)
cv2.imshow("adaptive", adaptive)
cv2.waitKey()
nathancy
  • 42,661
  • 14
  • 115
  • 137