开发者

how to read a number from an image? [closed]

开发者 https://www.devze.com 2023-04-05 02:03 出处:网络
Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

开发者_如何转开发

Closed 1 year ago.

Improve this question

I have some images that inside have a string with a number, like: "We have 3 books" I need to get 3: Is there a library in Java that can read the image and extract the number? or maybe the string then I will parse that string to find the number?

Thank you


It is called Optical Character Recognition (OCR). See this A few classes and libraries that may help you are:

asprise

abstractnonsense

jocr

tess4j


with tess4j you could try this:

public static void main(final String[] args) throws Exception {
    final File imageFile = new File("imageWith Digits.jpg");
    final ITesseract instance = new Tesseract();
    instance.setTessVariable("tessedit_char_whitelist", "0123456789");
    final String result = instance.doOCR(imageFile);
    System.out.println(result);
}


Tess4J is great tried with version 4.5.5 and tessdata from official repository: https://github.com/nguyenq/tess4j

I have to mention that it did not work with jpg and tif format, but perfect with png format. I had a simple case with white numbers on black background.

I searched a while, without finding a quick solution. So maybe even if this post is old, my comment will bring it up to the one or other who searches for a simple number recognition on images.

I was told to improve my answer with some code snippet. So it is recommended to binarize an image to get better results while processing for text and number recognition. One could use this simple calculation, it's in Kotlin using an java.awt.image.BufferedImage:

    fun binarize(img: BufferedImage): BufferedImage {
        val bufferedImage = BufferedImage(img.width, img.height, BufferedImage.TYPE_INT_RGB)
        for (i in 0 until img.width) {
            for (j in 0 until img.height) {
                val rgbValues: Int = img.getRGB(i, j)
                //An int can be represented with 8 hex numbers. The first two are the alpha value, 
                // which we will ignore within this calculation followed by two hex numbers for red, 
                // two for green and two for blue 
                val r = 0x00ff0000 and rgbValues shr 16
                val g = 0x0000ff00 and rgbValues shr 8
                val b = 0x000000ff and rgbValues
                val m = r + g + b
                //(255+255+255)/2 = 383 middle of dark and light
                bufferedImage.setRGB(i, j, if (m >= 383) Color.WHITE.rgb else 0)
            }
        }
        return bufferedImage
    }

What happens here is that we sum up for each pixel the values of the red, green and blue value and divide it by 2. When the result is lower then 383 we make this pixel black otherwise we make it white. So we get a new image with only black and white pixels which is then returned.

To see a complete example refer to: https://github.com/pachecoberlin/screenshotter

0

精彩评论

暂无评论...
验证码 换一张
取 消