如何使用OpenCV的处理图像,文字变得锐利和清晰?(How to use OpenCV to process image so that the text become sharp and clea

问 题

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: http://imgur.com/vOZAaE0

解决方案

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.

本文地址:IT屋 » How to use OpenCV to process image so that the text become sharp and clear?

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值