Bruce
Bruce

Reputation: 2574

How to use OpenCV to process image so that the text become sharp and clear?

Wanted to achieve something like this: http://www.leptonica.com/binarization.html

While searching for solutions, most of the answers were general instructions such as advise to look at adaptive filter, gaussian blur, dilation and erosion but none of them provide any sample code to start with (so can play around with the values)..

I know different image require different methods and values to achieve optimum clarity, but I just need some general filter so that the image at least slightly sharper and less noisy compare to the original, before doing any OCR on it.

this is what I've tried so far..

Mat imageMat = new Mat();
Utils.bitmapToMat(photo, imageMat);
Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);
Imgproc.adaptiveThreshold(imageMat, imageMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 5, 4);

but being an image processing newb, obviously I don't know what I'm doing XD

original image: original image

after applying the above: image after applying filters

How to do it correctly?

UPDATE: got it much closer thanks to metsburg, berak and Aurelius

Using the medianBlur method since cvSmooth with CV_MEDIAN is deprecated and replaced with medianBlur:

Imgproc.medianBlur(imageMat, imageMat, 3);
Imgproc.threshold(imageMat, imageMat, 0, 255, Imgproc.THRESH_OTSU);

Result: using medianblur before applying otsu

Using back the GaussianBlur method, the result actually is slightly better:

Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);
Imgproc.threshold(imageMat, imageMat, 0, 255, Imgproc.THRESH_OTSU);

Result: using gaussianblur before applying otsu

For this image the difference is not noticable, so I tried another image which is a photo taken off the computer screen. Computer screen gives a lot of noises (wavy lines) so it is very hard to remove the noise.

Example original image: pcscreen original image

Directly applying otsu: pcscreen directly apply otsu

using medianBlur before otsu: pcscreen using medianBlur before applying otsu

using GaussianBlur before otsu: pcscreen using GaussianBlur before applying otsu

Seems like gaussian blur is slightly better, however I'm still playing with the settings.. If anyone can advise on how to improve the computer screen photo further, please, let us know :) One more thing.. using this method on the image inside the top link yields horrible results :( see it here: https://i.sstatic.net/ju73J.jpg

Upvotes: 21

Views: 21383

Answers (2)

Bhushan N K
Bhushan N K

Reputation: 11

Instead of using Imgproc.THRESH_BINARY_INV use Imgproc.THRESH_BINARY only as _INV is inverting your image after binarisations and resulted is the said output shown above in your example.

correct code:

Imgproc.adaptiveThreshold(imageMat, imageMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 5, 4);

Upvotes: 1

metsburg
metsburg

Reputation: 2021

Well, you're almost there. Just try these modifications:

Instead of

    Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);

try:

     cvSmooth(imageMat, imageMat, CV_MEDIAN, new Size(3, 3), 0);

check the syntax, may not exactly match

The link you posted uses thresholding of Otsu, so try this:

 Imgproc.threshold(imageMat, imageMat, 0, 255, Imgproc.THRESH_OTSU);

for thresholding.

Try tweaking the parameters here and there, you should get something pretty close to your desired outcome.

Upvotes: 10

Related Questions