Java 中的数值图像识别
Numerical image recognition in Java
我想认识Pic1中的数字。
我做了一些工作,它 returns 到 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
这是第一个问题的答案,即如何拆分数字。
我向你推荐一些东西,就是将你的图像转换为二维数组,然后所有操作都会比你使用 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算法。
我想认识Pic1中的数字。
我做了一些工作,它 returns 到 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
这是第一个问题的答案,即如何拆分数字。
我向你推荐一些东西,就是将你的图像转换为二维数组,然后所有操作都会比你使用 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算法。