分享

12306验证码识别

 zhengyoucai 2015-05-08
猪圈

   

12306验证码识别


           
猪圈 发布于 2013年01月18日 5时,
            64评/19465阅

           

   
   





分享到: 








收藏 +165




4






干扰线去除判断比较挫是导致识别率低的原因,希望高手指点吧~后面的特征提取和训练识别就交给大家了~ (很不幸地告诉大家,上班前12306的验证码干扰又加强了,主要还是干扰线部分)

实现原理很简单:

1.图像灰度化与
二值化

2.去除干扰线(二值化在一定程度上已经消弱部分)



在5点以前,12306二值化后的图像干扰线是离散的,所以很容易地使用纵向扫描就能擦除干扰点提取字模,代码如下:


		for (int x = 0; x < w; ++x) {
			for (int y = 0; y < h; ++y) {
				if (isBlack(gray[x][y])) {
					if (x > 0 && x < (w - 1) && isWhite(gray[x - 1][y]) && isWhite(gray[x + 1][y])) {
						gray[x][y] = 65535;
					}
				}
			}
		}
		
		for (int x = 0; x < w; ++x) {
			for (int y = 0; y < h; ++y) {
				if (isBlack(gray[x][y])) {
					if (y > 0 && y < (h - 1) && isWhite(gray[x][y - 1]) && isWhite(gray[x][y + 1])) {
						gray[x][y] = 65535;
					}
				}
				binaryBufferedImage.setRGB(x, y, gray[x][y]);
			}
		}


注:目前该方法已经不奏效,已经删除~(16:18更新调整亮度提高识别率)

   

    标签:
    <无>
   

   

   

       

       




       


代码片段(1)
[全屏查看所有代码]


                       

                       

1. [代码][Java]代码    


跳至
                    [1]
   

[全屏预览]


           
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.chinasb.client;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BinaryTest {
    public static void main(String[] args) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(new File("D:/passCodeAction.jpg"));
        int h = bufferedImage.getHeight();
        int w = bufferedImage.getWidth();
         
        // 灰度化
        int[][] gray = new int[w][h];
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int argb = bufferedImage.getRGB(x, y);
                // 图像加亮(调整亮度识别率非常高)
                int r = (int)(((argb >> 16) & 0xFF) * 1.1 + 30);
                int g = (int)(((argb >> 8) & 0xFF) * 1.1 + 30);
                int b = (int)(((argb >> 0) & 0xFF) * 1.1 + 30);
                if (r >= 255) {
                    r = 255;
                }
                if (g >= 255) {
                    g = 255;
                }
                if (b >= 255) {
                    b = 255;
                }
                gray[x][y] = (int) Math.pow((Math.pow(r, 2.2) * 0.2973 + Math.pow(g, 2.2) * 0.6274 + Math.pow(b, 2.2) * 0.0753), 1/2.2);
            }
        }
        // 二值化
        int threshold = ostu(gray, w, h);
        BufferedImage binaryBufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                if (gray[x][y] > threshold) {
                    gray[x][y] |= 0x00FFFF;
                } else {
                    gray[x][y] &= 0xFF0000;
                }
                binaryBufferedImage.setRGB(x, y, gray[x][y]);
            }
        }
        // 矩阵打印
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (isBlack(binaryBufferedImage.getRGB(x, y))) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        ImageIO.write(binaryBufferedImage, "jpg", new File("D:/code.jpg"));
    }
    public static boolean isBlack(int colorInt) {
        Color color = new Color(colorInt);
        if (color.getRed() + color.getGreen() + color.getBlue() <= 300) {
            return true;
        }
        return false;
    }
    public static boolean isWhite(int colorInt) {
        Color color = new Color(colorInt);
        if (color.getRed() + color.getGreen() + color.getBlue() > 300) {
            return true;
        }
        return false;
    }
    public static int isBlackOrWhite(int colorInt) {
        if (getColorBright(colorInt) < 30 || getColorBright(colorInt) > 730) {
            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 ostu(int[][] gray, int w, int h) {
        int[] histData = new int[w * h];
        // Calculate histogram
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int red = 0xFF & gray[x][y];
                histData[red]++;
            }
        }
        // Total number of pixels
        int total = w * h;
         
        float sum = 0;
        for (int t = 0; t < 256; t++)
            sum += t * histData[t];
        float sumB = 0;
        int wB = 0;
        int wF = 0;
        float varMax = 0;
        int threshold = 0;
        for (int t = 0; t < 256; t++) {
            wB += histData[t]; // Weight Background
            if (wB == 0)
                continue;
            wF = total - wB; // Weight Foreground
            if (wF == 0)
                break;
            sumB += (float) (t * histData[t]);
            float mB = sumB / wB; // Mean Background
            float mF = (sum - sumB) / wF; // Mean Foreground
            // Calculate Between Class Variance
            float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);
            // Check if new maximum found
            if (varBetween > varMax) {
                varMax = varBetween;
                threshold = t;
            }
        }
        return threshold;
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多