【问题标题】:Numerical image recognition in JavaJava中的数值图像识别
【发布时间】:2015-06-30 08:01:51
【问题描述】:

我想识别Pic1中的数字。

我做了一些工作,它返回到pic2

这是我的代码:

package captchadecproj;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/*
 * @author Mr__Hamid
 */



public class NewClass {

public static void main(String args[]) throws IOException {
    int width = 110;
    int heigth = 40;
    BufferedImage image1 = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
    BufferedImage num1 = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
    BufferedImage image = null;
    File f = null;
    try {
        f = new File("E:\\Desktop 2\\Captcha Project\\CaptchaDecoder\\captchaDecProj\\167.png");
        image = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_ARGB);
        image = ImageIO.read(f);
        System.out.println("Read!");
    } catch (IOException e) {
        System.out.println("Error" + e);
    }
    int[] pixel = null;
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            pixel = image.getRaster().getPixel(x, y, new int[3]);
            if (pixel[0] < 30 & pixel[1] > 130 & pixel[2] < 110 & pixel[2] > 60) {
                image1.setRGB(x, y, Integer.parseInt("ffffff".trim(), 16));
                System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (image.getWidth() * y + x));
            } else {
                image1.setRGB(x, y, 1);
                System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (image.getWidth() * y + x));
            }

        }
    }

    try {
        f = new File("D:\\Original.jpg");
        ImageIO.write(image, "jpg", f);
        f = new File("D:\\black&White.jpg");
        ImageIO.write(image1, "jpg", f);
        System.out.println("Writed");
    } catch (IOException e) {
        System.out.println("Error" + e);
    }
}
}

我有两个问题:

如何拆分这些数字?

我如何识别哪一个是我的号码?

例如上传的图片:7,1,6

【问题讨论】:

  • How can I split these numbers? 将您的图像转换为 bw(白色背景,黑色文本),然后在具有最少黑色(0 像素)的列上拆分 How can I recognize which one is my number?
  • 我的代码将图像转换为 bw(白色文本和黑色背景)。然后呢?
  • 然后创建直方图,计算每列中非背景颜色的数量。然后从您的图像中提取仅具有非背景颜色的区域。这将是带有数字(或任何其他字符)的部分
  • 你介意分享一些代码吗?

标签: java image image-processing numbers ocr


【解决方案1】:

这是第一个问题的答案,即如何拆分数字。 我向你推荐一些东西,就是将你的图像转换为二维数组,然后所有操作都会比你使用BufferedImage时执行得更快。

BufferedImage image = ImageIO.read(new URL("http://i.stack.imgur.com/QaTj5.jpg"));
    int startPos = 0, lastValue = 0;
    Set<Integer> colours = new HashSet<>();
    for (int x = 0; x < image.getWidth(); x++) {
        int histValue = 0;

        for (int y = 0; y < image.getHeight(); y++) {
            colours.add(image.getRGB(x, y) );
            if (image.getRGB(x, y) == 0xffffFFFF) {
                histValue++;
            }
        }

        if (histValue == 0 && lastValue == 0) {
            startPos = x;
        } else if (histValue == 0 && lastValue != 0) {
            BufferedImage segment = image.getSubimage(startPos, 0, x
                    - startPos, image.getHeight());
            ImageIO.write(segment, "jpg", new File("Segment" + startPos
                    + ".jpg"));

        }
        lastValue = histValue; 
    }
    if (lastValue!=0){
        BufferedImage segment = image.getSubimage(startPos, 0, image.getWidth()
                - startPos, image.getHeight());
        ImageIO.write(segment, "jpg", new File("Segment" + startPos
                + ".jpg"));
    }

现在你需要做的就是找到一些不错的 ocr 算法。

【讨论】:

  • 分割后的图片包含灰色像素。但我将它们设置为白色。颜色 myWhite = 新颜色(255、255、255); int rgb = myWhite.getRGB(); image1.setRGB(x, y, rgb);我怎样才能得到只有黑白像素的图片?
  • @Hamid 通过添加,您的代码存在一些问题,首先,当您将图像转换为 jpg 时,因为压缩算法会丢失精确的颜色,另一件事是您的颜色。黑色是 0 而不是 1。但正如我所建议的,不要使用图像,使用数组。当我进行图像处理时,我有 2d int 数组,其中 1 是白色,0 是黑色
  • 感谢您帮助我。但我没有从使用数组中得到什么。你介意分享一些代码吗?
  • @Hamid 我的意思是在图像中设置颜色,创建辅助二维数组,并在那里更新值。所以改为image1.setRGB(x, y,newValue); 你会做helper[x][y] = newValue 通过在我的一个应用程序中进行简单的更改,我将帧速率从一帧 4 秒提高到每秒 15 帧
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
相关资源
最近更新 更多