您的位置:首页 > 其它

验证码识别技术(二)去除图片背景

2015-08-14 16:28 295 查看
去除图片的背景,这里有一个基本的分析方法,可以下载下来几个验证码,然后,把像点读出,导入到EXCEL文件中,自动生成散点图,通过散点图,能很快的分析出背景的值是多少,根据这个值,去处理背景很方便。

例如:

原始的验证码为



通过程序分析



比如评出的值为500左右。

去除背景的图片为:



相关的代码如下所示。

/**
* 去除图片的背景(二值化)
*
* @param picFile 图片的文件全名
* @return 图片结构
* @throws Exception
*/
public static BufferedImage StartRemove(String picFile) throws Exception {
BufferedImage img = ImageIO.read(new File(picFile));
int width = img.getWidth();
int height = img.getHeight();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (isWhite(img.getRGB(x, y)) == 1) {
img.setRGB(x, y, Color.WHITE.getRGB());
} else {
img.setRGB(x, y, Color.BLACK.getRGB());
}
}
}

return img;
}

// 其中一个像素点是否有效
private static int isWhite(int colorInt) {
Color color = new Color(colorInt);

int sumPix = color.getRed() + color.getGreen() + color.getBlue();

//// 测试
//System.out.println(sumPix);

if (sumPix > 500) {
return 1;
} else {
return 0;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: