来源:http://blog.csdn.net/lmj623565791/article/details/23955883
公司有需求啊,所以就得研究哈,最近公司需要读验证码,于是就研究起了图像识别,应该就是传说中的(OCR:光学字符识别OCR),下面把今天的收获整理一个给大家做个分享。
本人程序用的tesseract,官方地址:https://code.google.com/p/tesseract-ocr/,不为别的,谁让它支持我们的天朝的文字呢~哈
下载好程序后解压:
大概可以看到这样一个目录,别见怪楼主里面一堆测试文件。
然后就开始我们的测试之旅:
tesseract的用法:
参数1:需要识别的文件
参数2:输出的文件名称,输出的是文本文件,里面保存了识别的信息
识别英文这两个参数就可以了,下面做个实验:
我们在命令行输入:tesseract 5.jpg 6 ,可以看到程序生成了一个6.txt ,里面保存着识别后的文本,怎么样简单又给力~
上面说道tesseract 是支持中文的,所以么,接下来看看如何使用tesseract 实现我们中文的识别,下面继续介绍其他参数
参数3:-l
参数4: 使用的语言库
参数3 -l应该是知道参数4所使用的语言库,默认英文,也就是为什么上面识别英文的例子,并没有输入参数3和参数4,也实现了识别。
下面继续我们的实验:
我们准备了一张图片,然后使用tesseract zhongwen.jpg 7 -l chi_sim 指明了中文语言,然后效果图上,还是很不错的,毕竟我们的中文是如此的博大精深,并且tesseract可以经过训练,然后识字的能力就会大幅度提升。
好了,由于一行代码没写,就不上传代码了,大家自己去官网下载。接下来我会使用Java带大家实现这样的小程序。
如果这篇文章对你有用,就赞一个~欢迎大家留言,多交流~
接着上一篇OCR所说的,上一篇给大家介绍了tesseract 在命令行的简单用法,当然了要继承到我们的程序中,还是需要代码实现的,下面给大家分享下java实现的例子。
拿代码扫描上面的图片,然后输出结果。主要思想就是利用Java调用系统任务。
下面是核心代码:
代码很简单,中间那部分ProcessBuilder其实就类似Runtime.getRuntime().exec("tesseract.exe 1.jpg 1 -l chi_sim"),大家不习惯的可以使用Runtime。
测试代码:
- package com.zhy.test;
-
- import java.io.File;
-
- public class Test
- {
- public static void main(String[] args)
- {
- try
- {
-
- File testDataDir = new File("testdata");
- System.out.println(testDataDir.listFiles().length);
- int i = 0 ;
- for(File file :testDataDir.listFiles())
- {
- i++ ;
- String recognizeText = new OCRHelper().recognizeText(file);
- System.out.print(recognizeText+"\t");
-
- if( i % 5 == 0 )
- {
- System.out.println();
- }
- }
-
- } catch (Exception e)
- {
- e.printStackTrace();
- }
-
- }
- }
输出结果:
对比第一张图片,是不是很完美~哈哈 ,当然了如果你只需要实现验证码的读写,那么上面就足够了。下面继续普及图像处理的知识。
-------------------------------------------------------------------我的分割线--------------------------------------------------------------------
当然了,有时候图片被扭曲或者模糊的很厉害,很不容易识别,所以下面我给大家介绍一个去噪的辅助类,绝对碉堡了,先看下效果图。
来张特写:
一个类,不依赖任何jar,把图像中的干扰线消灭了,是不是很给力,然后再拿这样的图片去识别,会不会效果更好呢,嘿嘿,大家自己实验~
代码:
- package com.zhy.test;
-
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
-
- import javax.imageio.ImageIO;
-
- public class ClearImageHelper
- {
-
- public static void main(String[] args) throws IOException
- {
-
-
- File testDataDir = new File("testdata");
- final String destDir = testDataDir.getAbsolutePath()+"/tmp";
- for (File file : testDataDir.listFiles())
- {
- cleanImage(file, destDir);
- }
-
- }
-
-
-
-
-
-
-
-
-
- public static void cleanImage(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 = 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];
-
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- int red = 0xFF & gray[x][y];
- histData[red]++;
- }
- }
-
-
- 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];
- if (wB == 0)
- continue;
-
- wF = total - wB;
- if (wF == 0)
- break;
-
- sumB += (float) (t * histData[t]);
-
- float mB = sumB / wB;
- float mF = (sum - sumB) / wF;
-
-
- float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);
-
-
- if (varBetween > varMax)
- {
- varMax = varBetween;
- threshold = t;
- }
- }
-
- return threshold;
- }
- }
好了,就到这里。如果这篇文章对你有用,赞一个吧~