java 验证码 字母 识别_java验证码识别--3

本文介绍了如何使用Java进行复杂验证码的字母识别。通过图像预处理,将验证码平均分成5份,分析每部分的颜色分布,找出验证码的主要颜色并去除背景。之后,通过训练数据和识别算法,实现了100%的识别率。
摘要由CSDN通过智能技术生成

(本文仅用于学习研究图像匹配识别原理,不得用于其他用途。)

前面的验证码背景都比较简单,用亮度稍微区分一下就可以去掉背景

来看个稍微复杂一点的

0_12813668667PUx.gif

1。图片预处理

怎么去掉背景干扰呢。

可以注意到每个验证码数字或字母都是同一颜色,所以把验证码平均分成5份

0_1281367162flTG.gif

计算每个区域的颜色分布,除了白色之外,颜色值最多的就是验证码的颜色

因此很容易将背景去掉

代码:

public static BufferedImage removeBackgroud(String picFile)

throws Exception {

BufferedImage img = ImageIO.read(new File(picFile));

img = img.getSubimage(1, 1, img.getWidth() - 2, img.getHeight() - 2);

int width = img.getWidth();

int height = img.getHeight();

double subWidth = (double) width / 5.0;

for (int i = 0; i < 5; i++) {

Map map = new HashMap();

for (int x = (int) (1 + i * subWidth); x < (i + 1) * subWidth

&& x < width - 1; ++x) {

for (int y = 0; y < height; ++y) {

if (isWhite(img.getRGB(x, y)) == 1)

continue;

if (map.containsKey(img.getRGB(x, y))) {

map.put(img.getRGB(x, y), map.get(img.getRGB(x, y)) + 1);

} else {

map.put(img.getRGB(x, y), 1);

}

}

}

int max = 0;

int colorMax = 0;

for (Integer color : map.keySet()) {

if (max < map.get(color)) {

max = map.get(color);

colorMax = color;

}

}

for (int x = (int) (1 + i * subWidth); x < (i + 1) * subWidth

&& x < width - 1; ++x) {

for (int y = 0; y < height; ++y) {

if (img.getRGB(x, y) != colorMax) {

img.setRGB(x, y, Color.WHITE.getRGB());

} else {

img.setRGB(x, y, Color.BLACK.getRGB());

}

}

}

}

retur

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值