(本文仅用于学习研究图像匹配识别原理,不得用于其他用途。)
验证码识别如果识别率都是100%,那验证码也就没存在的必要了。
其实很多验证码能达到10%的识别率就不错了。
下面来一个稍微复杂一点的,识别率85%左右。
看验证码
挑一张来看
放大看,我们会发现干扰线是纯黑色的,因此去干扰线的方法就有了
对点color[i][j],如果color[i+1][j],color[i-1][j],color[i][j+1],color[i][j-1]都是纯黑或者纯白色的,就认为color[i][j]是干扰,将color[i][j]置为白色。
处理之后
这样就简单了,分割也简单。
识别结果
啥也不说了,贴代码
public class ImagePreProcess4 {
private static Map trainMap = null;
private static int index = 0;
public static int isBlack(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() <= 300) {
return 1;
}
return 0;
}
public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() > 300) {
return 1;
}
return 0;
}
public static int getColorBright(int colorInt) {
Color color = new Color(colorInt);
return color.getRed() + color.getGreen() + color.getBlue();
}
public static int isBlackOrWhite(int colorInt) {
if (getColorBright(colorInt) < 30 || getColorBright(colorInt) > 730) {
return 1;
}
return 0;
}
public static BufferedImage removeBackgroud(String picFile)
throws Exception {
BufferedImage img = ImageIO.read(new File(picFile));
int width = img.getWidth();
int height = img.getHeight();
for (int x = 1; x < width - 1; ++x) {
for (int y = 1; y < height - 1; ++y) {
if (getColorBright(img.getRGB(x, y)) < 100) {
if (isBlackOrWhite(img.getRGB(x - 1, y))
+ isBlackOrWhite(img.getRGB(x + 1, y))
+ isBlackOrWhite(img.getRGB(x, y - 1))
+ isBlackOrWhite(img.getRGB(x, y + 1)) == 4) {
img.setRGB(x, y, Color.WHITE.getRGB());
}
}<