1、Tess4j
最近在GitHub上看到一個圖像識別的開源框架tess4j,而且是Java版的,為此利用此框架來識別驗證碼中的信息,利用它提供的字體庫,來提取信息,對於沒有什么干擾線的驗證碼准確率還是蠻高的,對於有一些干擾線的就差一些,不過也可以能通過訓練字體庫,從而可以提高准確率的。
根據范例,寫了一個簡單的提取驗證碼信息的工具類VerificationCode:
主要是用這個類的extract方法,這個方法有3個參數:
- 第1個參數是指定圖片的路徑
- 第2個參數是指定字體庫的,其中chi_sim表示中文簡體,eng表示英文
- 第3個參數是指定是否需要去除干擾線,true表示需要,false表示不需要
package com.swnote.tess4j.test;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.recognition.software.jdeskew.ImageDeskew;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.util.ImageHelper;
/**
* 識別驗證碼
*
* @author lzj
* @date [2019-03-03]
*/
public class VerificationCode {
/**
* 配置文件
*/
private static String config_path = "src/main/resources";
/**
* 調整傾斜度的閾值
*/
private static double deskew_threshold = 0.05d;
/**
* 提取驗證碼圖片中的文字
*
* @param img_path
* @param lang
* @param clear
* @return
*/
public static String extract(String img_path, String lang, boolean clear) throws Exception {
// 圖片文件
File img = new File(img_path);
if (clear) {
// 將去除干擾后的圖片保存在同級目錄下的ext目錄下
String ext_path = img.getParentFile().getPath() + "/ext";
// 去除干擾
CleanImage.cleanLinesInImage(img, ext_path);
// 處理后的圖片
img = new File(ext_path, img.getName());
}
// 設置語言庫
ITesseract instance = new Tesseract();
File directory = new File(config_path);
String course_file = directory.getCanonicalPath();
instance.setDatapath(course_file + "/tessdata");
// chi_sim表示中文簡體
// eng表示英文
instance.setLanguage(lang);
BufferedImage buffer_img = ImageIO.read(img);
ImageDeskew img_deskew = new ImageDeskew(buffer_img);
double img_skew_angle = img_deskew.getSkewAngle();
if ((img_skew_angle > deskew_threshold || img_skew_angle < -(deskew_threshold))) {
buffer_img = ImageHelper.rotateImage(buffer_img, -img_skew_angle);
}
String result = instance.doOCR(buffer_img);
return result;
}
}
其中CleanImage類是用於清楚驗證碼干擾線的,這個類是我從網上找到的,加上這個類有一定的效果,但是不是特別理想,希望大家能夠找到更好的去除干擾線方式。
在此也貼一下CleanImage類的代碼:
package com.swnote.tess4j.test;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* 網上找到清除圖片干擾工具
*/
public class CleanImage {
public static void cleanLinesInImage(File sfile, String destDir) throws IOException {
File destF = new File(destDir);
if (!destF.exists()) {
destF.mkdirs();
}
BufferedImage bufferedImage = ImageIO.read(sfile);
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 = 1; y < h - 1; y++) {
for (int x = 1; x < w - 1; x++) {
boolean flag = false;
if (isBlack(binaryBufferedImage.getRGB(x, y))) {
// 左右均為空時,去掉此點
if (isWhite(binaryBufferedImage.getRGB(x - 1, y))
&& isWhite(binaryBufferedImage.getRGB(x + 1, y))) {
flag = true;
}
// 上下均為空時,去掉此點
if (isWhite(binaryBufferedImage.getRGB(x, y + 1))
&& isWhite(binaryBufferedImage.getRGB(x, y - 1))) {
flag = true;
}
// 斜上下為空時,去掉此點
if (isWhite(binaryBufferedImage.getRGB(x - 1, y + 1))
&& isWhite(binaryBufferedImage.getRGB(x + 1, y - 1))) {
flag = true;
}
if (isWhite(binaryBufferedImage.getRGB(x + 1, y + 1))
&& isWhite(binaryBufferedImage.getRGB(x - 1, y - 1))) {
flag = true;
}
if (flag) {
binaryBufferedImage.setRGB(x, y, -1);
}
}
}
}
// 矩陣打印
// 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(destDir, sfile.getName()));
}
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;
}
}
2、測試
首先測試一張沒有干擾線的圖片,即:
然后調用工具類,可以得到如下結果:
結果是正確的。
再一測試一個中文的,同時具有干擾線的,即:
測試結果為:
中文內容是識別出來了,但是也識別了一些其它信息。
關注我
以你最方便的方式關注我:
微信公眾號: