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. ------ ------
2. ------ ------
3. ------ ------
4. ------ ------
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.
Border removal for OCR
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Border removal for OCR
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.
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
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.
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 ?
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
Mean time do you have any guess on the performance of IM vs Java in image thresholding and trimming ?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Border removal for OCR
Sorry, no.Mean time do you have any guess on the performance of IM vs Java in image thresholding and trimming ?