Ask Your Question

How to increase contrast of a image with text

asked 2017-05-20 06:34:10 -0500

joshua101 gravatar image

updated 2017-05-20 06:34:48 -0500

I have an image that contains text. Before sending it to OCR, I would like to increase the contrast of it.

The original image is like this:

image description

I can increase the contrast of the text using imagemagick: convert orig.jpg -level 60%x85% contrast.jpg. This turns the image into

image description

The imagemagick code is basically doing this

In normal usage (-level) the image values are stretched so that the given 'black_point' value in the original image is set to zero (or black), while the given 'white_point' value is set to QuantumRange (or white). This provides you with direct contrast adjustments to the image. The 'gamma' of the resulting image will then be adjusted.


How can I do the same thing in OpenCV? I tried the threshold functions with cv2.THRESH_BINARY but it doesn't produce the same result.

edit retag flag offensive close merge delete


use lut like in this post

LBerger gravatar imageLBerger ( 2017-05-20 06:48:19 -0500 )edit

I tried using LUT as mentioned in this post however, the results were not same as imagemagick at all

joshua101 gravatar imagejoshua101 ( 2017-05-20 06:57:53 -0500 )edit

Of course you have to understand what is a lut : linear : lut[i]=i exponential lut[i]=exp(i)/(exp(256))*256 : dangerous lut

if you want to have same results than image magick save an image x in opencv with x[i,j]=i; apply your transform in image magick. Open result image in opencv and you have your specific LUT

LBerger gravatar imageLBerger ( 2017-05-20 07:41:36 -0500 )edit

Well I don't want to use imagemagick because that is not part of my automated pipeline. I would rather have the results replicated in opencv....maybe not exactly like imagemagick but close to it. I agree the LUT formula in the referenced blog post seems very aggressive. Instead, I am looking for something that makes the black pixels blacker and white pixels whiter.

joshua101 gravatar imagejoshua101 ( 2017-05-20 07:54:09 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-05-20 08:04:03 -0500

LBerger gravatar image

updated 2017-05-20 08:37:53 -0500

Ok back to basis threshold your image using OTSU. I cropped first image

image description

blur 17x17 and otsu threshold

image description

edit flag offensive delete link more


Thank you but as you can see that the thresholded image is quite different from what imagemagick provides. Therefore, tesseract can not seem to OCR the one thresholded from opencv but can OCR the one from imagemagick. Perhaps I should add some gaussian blur after the threshold. What values did you use for the threshold OTSU?

joshua101 gravatar imagejoshua101 ( 2017-05-20 08:29:30 -0500 )edit

there is no values for OTSU

LBerger gravatar imageLBerger ( 2017-05-20 08:37:19 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-05-20 06:34:10 -0500

Seen: 1,156 times

Last updated: May 20 '17