Page 1 of 1

Border removal for OCR

Posted: 2014-07-04T12:44:40-07:00
by appu
Hi All,

I've a legacy application in which I'm testing the button texts using our OCR service. But our OCR is failing to read text correctly, if the text image is enclosed in borders. We normally choose a large button area(including borders) for accommodating drift in different platform. The button can be highlighted or not.

I've implemented a logic to do border scan for pixels in top, bottom, left and right to identify borders. Scanning is done on grey or binary converted images. If continuous pixels are are detected along image width(horizontal scan) or image height(vertical scan) assumed it as a border and trimmed it. During border line scanning, if pixels are discontinuous, it can be an edge of a character(if images captured tightly with text). So we stop scanning and assume it as a character boundary.

Image ------ Before Crop ------ After Crop

1. Image ------ Image ------ Image
2. Image ------Image ------Image
3. Image ------Image ------Image
4. Image ------ Image ------Image

For the images 1,2 and 3, I was able to successfully crop the borders using border line scanning and the resulting ocr comparison was successful. But for the 4th one cropping failed. This is due to the fact that, there were speckles on the image boundary. Since these speckles are discontinuous, my cropping routine consider it as a character boundary. So cropping is not effective thus ocr comparison failed.

I'm wondering is there any better way(noise filters, masks or other built-in tools?) by which I can remove borders of a text image effectively OR remove the speckles outside border line, so that my cropping routine will work correctly. Also there can be cases where there were no borders at all. So need border removal solution for both cases.

Please guide and share your suggestions.

Re: Border removal for OCR

Posted: 2014-07-04T14:03:57-07:00
by fmw42
Use a couple of iterations of fuzzy trim, to remove the borders. That should work for all your examples above.

convert image.jpg -fuzz 20% -trim -trim +repage -colorspace gray -negate -threshold 70% result.gif

or

convert image.jpg -fuzz 20% -trim -trim +repage -colorspace gray -negate +dither -colors 2 -auto-level result.gif


You will have to determine if the background is dark or light with light or dark text to know whether to negate.

If you do too many -trims, it should not hurt. You will just get the image trimmed to bounds of the text. You can always add a little pad with -border, if you want.

Re: Border removal for OCR

Posted: 2014-07-06T11:39:08-07:00
by appu
Great Fred, it worked like a charm ! I really happy with the outcome of -trim operation.

I tried the following command on the binary images generated from a set of color images (generated using existing Java code) and it worked.

Code: Select all

convert input.jpg -fuzz 60% -trim -trim -bordercolor white -border 2x2 output.jpg
Since I tried it on binary images - negation,dither, auto-level commands are not used. But I'm planning to rewrite the code module such that the ocr preprocessing should be done through ImageMagick rather than Java code(currently implemented one). First, I'm going to profile the performance of IM vs Java code for Image thresholding(similar to fuzz in IM) and will share the details soon.

Mean time do you have any guess on the performance of IM vs Java in image thresholding and trimming ?

Re: Border removal for OCR

Posted: 2014-07-06T13:01:59-07:00
by fmw42
Mean time do you have any guess on the performance of IM vs Java in image thresholding and trimming ?
Sorry, no.