常用的两种ORC 验证码 识别方法及实践感言

参考:

用Asprise的OCR包,处理验证码。

java ORC 图片中文识别

浅谈OCR之Tesseract

(原)测试 Tesseract-OCR 在windows平台过程记录

Java OCR 图像智能字符识别技术,可识别中文

 

来由,这几天想做坏事,从一个网站上批量查询东西,但是无奈每次查询都有验证码,所以就搜索到了以上几篇文章

基本介绍:

1、Asprise,是个收费的OCR软件,但是网络的力量是无穷的,可以下载到破解的

关于 Asprise的使用例子可以参考代码:

 Asprise-OCR-Java示例代码

 

2、Tesseract,该技术是google的一个源码项目,出自HP(http://code.google.com/p/tesseract-ocr)

a、首先安装tesseract-ocr-setup-3.01-1.exe

b、安装好了以后你需要哪种语言或者类别的识别支持,到官网的downlist中去查找插件,并放置在安装目录的/tessdata文件夹下(如果需要中文支持,下载tesseract-ocr的中文包

chi_sim.traineddata.gz,解压缩之后复制到tesseract-ocr的安装目录/tessdata文件夹之下)见图

c、安装好以后,c++,java等等都可以进行tesseract的转换操作,我们就以命令行下为例

C:\Program Files\Tesseract-OCR>tesseract -help

Usage:tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...]

pagesegmode values are:

0 = Orientation and script detection (OSD) only.

1 = Automatic page segmentation with OSD.

2 = Automatic page segmentation, but no OSD, or OCR

3 = Fully automatic page segmentation, but no OSD. (Default)

4 = Assume a single column of text of variable sizes.

5 = Assume a single uniform block of vertically aligned text.

6 = Assume a single uniform block of text.

7 = Treat the image as a single text line.

8 = Treat the image as a single word.

9 = Treat the image as a single word in a circle.

10 = Treat the image as a single character.

-l lang and/or -psm pagesegmode must occur before anyconfigfile.

 

实例 tesseract xx.jpg output -l eng -psm 8

详解 tesseract即为安装目录下的tesseract.exe执行文件

     xx.jpg即为你需要ORC解析的图片文件

     output即为你需要将结果保存的文件名

     -l eng 即为以英文字母模式进行解析

     -psm 8即为以单行字母解析

 

关于Tesseract的JAVA中的使用说明可以参考代码:

tesseract安装包及JAVA代码实例 

 

综合使用以后,发现这2者效果一般,识别率很低,

原因很简单,大多数网站的验证码都加入不同程度的噪音,以防止OCR软件的自动分析。

 

 在Java OCR 图像智能字符识别技术,可识别中文  一文中谈到了进行一些图像去噪处理的简单方法,但是效果也一般,不过这的确提供了一些思路,只要有好的噪点处理方法肯定会提高OCR识别率。

package com.ocr;

import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ColorModel;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;

/**
 *
 * 图像过滤,增强OCR识别成功率
 *
 */
public class ImageFilter {

    private BufferedImage image;
    private int iw, ih;
    private int[] pixels;

    public ImageFilter(BufferedImage image) {
       this.image = image;
       iw = image.getWidth();
       ih = image.getHeight();
       pixels = new int[iw * ih];
    }

    /** 图像二值化 */
    public BufferedImage changeGrey() {
       PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw);
       try {
           pg.grabPixels();
       } catch (InterruptedException e) {
           e.printStackTrace();
       }

       // 设定二值化的域值,默认值为100
       int grey = 100;
       // 对图像进行二值化处理,Alpha值保持不变
       ColorModel cm = ColorModel.getRGBdefault();

       for (int i = 0; i < iw * ih; i++) {
           int red, green, blue;
           int alpha = cm.getAlpha(pixels[i]);
           if (cm.getRed(pixels[i]) > grey) {
              red = 255;
           } else {
              red = 0;
           }

           if (cm.getGreen(pixels[i]) > grey) {
              green = 255;
           } else {
              green = 0;
           }

           if (cm.getBlue(pixels[i]) > grey) {
              blue = 255;
           } else {
              blue = 0;
           }

           pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
       }

       // 将数组中的象素产生一个图像
       return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, pixels, 0, iw));
    }

 

    /** 提升清晰度,进行锐化 */
    public BufferedImage sharp() {
       PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw);
       try {
           pg.grabPixels();
       } catch (InterruptedException e) {
           e.printStackTrace();
       }

       // 象素的中间变量
       int tempPixels[] = new int[iw * ih];
       for (int i = 0; i < iw * ih; i++) {
           tempPixels[i] = pixels[i];
       }

       // 对图像进行尖锐化处理,Alpha值保持不变
       ColorModel cm = ColorModel.getRGBdefault();
       for (int i = 1; i < ih - 1; i++) {

           for (int j = 1; j < iw - 1; j++) {
              int alpha = cm.getAlpha(pixels[i * iw + j]);
              // 对图像进行尖锐化
              int red6 = cm.getRed(pixels[i * iw + j + 1]);
              int red5 = cm.getRed(pixels[i * iw + j]);
              int red8 = cm.getRed(pixels[(i + 1) * iw + j]);
              int sharpRed = Math.abs(red6 - red5) + Math.abs(red8 - red5);
              int green5 = cm.getGreen(pixels[i * iw + j]);
              int green6 = cm.getGreen(pixels[i * iw + j + 1]);
              int green8 = cm.getGreen(pixels[(i + 1) * iw + j]);
              int sharpGreen = Math.abs(green6 - green5) + Math.abs(green8 - green5);
              int blue5 = cm.getBlue(pixels[i * iw + j]);
              int blue6 = cm.getBlue(pixels[i * iw + j + 1]);
              int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);
              int sharpBlue = Math.abs(blue6 - blue5) + Math.abs(blue8 - blue5);

              if (sharpRed > 255) {
                  sharpRed = 255;
              }

              if (sharpGreen > 255) {
                  sharpGreen = 255;
              }

              if (sharpBlue > 255) {
                  sharpBlue = 255;
              }

              tempPixels[i * iw + j] = alpha << 24 | sharpRed << 16 | sharpGreen << 8 | sharpBlue;
           }
       }

       // 将数组中的象素产生一个图像
       return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, tempPixels, 0, iw));
    }

 

    /** 中值滤波 */
    public BufferedImage median() {
       PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw);
       try {
           pg.grabPixels();
       } catch (InterruptedException e) {
           e.printStackTrace();
       }

       // 对图像进行中值滤波,Alpha值保持不变
       ColorModel cm = ColorModel.getRGBdefault();
       for (int i = 1; i < ih - 1; i++) {
           for (int j = 1; j < iw - 1; j++) {
              int red, green, blue;
              int alpha = cm.getAlpha(pixels[i * iw + j]);
              // int red2 = cm.getRed(pixels[(i - 1) * iw + j]);
              int red4 = cm.getRed(pixels[i * iw + j - 1]);
              int red5 = cm.getRed(pixels[i * iw + j]);
              int red6 = cm.getRed(pixels[i * iw + j + 1]);
              // int red8 = cm.getRed(pixels[(i + 1) * iw + j]);

              // 水平方向进行中值滤波
              if (red4 >= red5) {
                  if (red5 >= red6) {
                     red = red5;
                  } else {
                     if (red4 >= red6) {
                         red = red6;
                     } else {
                         red = red4;
                     }
                  }
              } else {
                  if (red4 > red6) {
                     red = red4;
                  } else {
                      if (red5 > red6) {
                         red = red6;
                     } else {
                         red = red5;
                     }
                  }
              }

              // int green2 = cm.getGreen(pixels[(i - 1) * iw + j]);
              int green4 = cm.getGreen(pixels[i * iw + j - 1]);
              int green5 = cm.getGreen(pixels[i * iw + j]);
              int green6 = cm.getGreen(pixels[i * iw + j + 1]);
              // int green8 = cm.getGreen(pixels[(i + 1) * iw + j]);

              // 水平方向进行中值滤波
              if (green4 >= green5) {
                  if (green5 >= green6) {
                     green = green5;
                  } else {
                     if (green4 >= green6) {
                         green = green6;
                     } else {
                         green = green4;
                     }
                  }
              } else {
                  if (green4 > green6) {
                      green = green4;
                  } else {
                     if (green5 > green6) {
                         green = green6;
                     } else {
                         green = green5;
                     }
                  }
              }

              // int blue2 = cm.getBlue(pixels[(i - 1) * iw + j]);
              int blue4 = cm.getBlue(pixels[i * iw + j - 1]);
              int blue5 = cm.getBlue(pixels[i * iw + j]);
              int blue6 = cm.getBlue(pixels[i * iw + j + 1]);
              // int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]);

              // 水平方向进行中值滤波
              if (blue4 >= blue5) {
                  if (blue5 >= blue6) {
                     blue = blue5;
                  } else {
                     if (blue4 >= blue6) {
                         blue = blue6;
                     } else {
                         blue = blue4;
                     }
                  }
              } else {
                  if (blue4 > blue6) {
                     blue = blue4;
                  } else {
                     if (blue5 > blue6) {
                         blue = blue6;
                     } else {
                         blue = blue5;
                     }
                  }
              }
              pixels[i * iw + j] = alpha << 24 | red << 16 | green << 8 | blue;
           }
       }

       // 将数组中的象素产生一个图像
       return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, pixels, 0, iw));
    }

    /** 线性灰度变换 */
    public BufferedImage lineGrey() {
       PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw);
       try {
           pg.grabPixels();
       } catch (InterruptedException e) {
           e.printStackTrace();
       }

       // 对图像进行进行线性拉伸,Alpha值保持不变
       ColorModel cm = ColorModel.getRGBdefault();

       for (int i = 0; i < iw * ih; i++) {
           int alpha = cm.getAlpha(pixels[i]);
           int red = cm.getRed(pixels[i]);
           int green = cm.getGreen(pixels[i]);
           int blue = cm.getBlue(pixels[i]);

           // 增加了图像的亮度
           red = (int) (1.1 * red + 30);
           green = (int) (1.1 * green + 30);
           blue = (int) (1.1 * blue + 30);
           if (red >= 255) {
              red = 255;
           }

           if (green >= 255) {
              green = 255;
           }

           if (blue >= 255) {
              blue = 255;
           }
           pixels[i] = alpha << 24 | red << 16 | green << 8 | blue;
       }

       // 将数组中的象素产生一个图像
       return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, pixels, 0, iw));
    }

    /** 转换为黑白灰度图 */
    public BufferedImage grayFilter() {
       ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
       ColorConvertOp op = new ColorConvertOp(cs, null);
       return op.filter(image, null);
    }

    /** 平滑缩放 */
    public BufferedImage scaling(double s) {
       AffineTransform tx = new AffineTransform();
       tx.scale(s, s);
       AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
       return op.filter(image, null);
    }

    public BufferedImage scale(Float s) {
       int srcW = image.getWidth();
       int srcH = image.getHeight();
       int newW = Math.round(srcW * s);
       int newH = Math.round(srcH * s);

       // 先做水平方向上的伸缩变换
       BufferedImage tmp=new BufferedImage(newW, newH, image.getType());
       Graphics2D g= tmp.createGraphics();
       for (int x = 0; x < newW; x++) {
           g.setClip(x, 0, 1, srcH);
           // 按比例放缩
           g.drawImage(image, x - x * srcW / newW, 0, null);
       }

        // 再做垂直方向上的伸缩变换
       BufferedImage dst = new BufferedImage(newW, newH, image.getType());
       g = dst.createGraphics();
       for (int y = 0; y < newH; y++) {
           g.setClip(0, y, newW, 1);
           // 按比例放缩
           g.drawImage(tmp, 0, y - y * srcH / newH, null);
       }
       return dst;
    }

}

 


 

 后记:

浅谈OCR之Onenote 2010

这个是另外一个OCR工具

 

 20121115补充:

tesseract-ocr 识别码库训练方法  提高验证码识别率

 

转载于:https://www.cnblogs.com/shuzui1985/archive/2012/05/18/3020958.html

你可能感兴趣的:(常用的两种ORC 验证码 识别方法及实践感言)