欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java調(diào)用opencv身份證號識別詳解

 更新時(shí)間:2024年03月06日 10:36:08   作者:冒泡的肥皂  
這篇文章主要為大家詳細(xì)介紹了java如何調(diào)用opencv實(shí)現(xiàn)身份證號的識別,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、實(shí)現(xiàn)思路

1.矯正圖片 這個(gè)可以參考上篇文章 Java調(diào)用opencv圖片矯正

2.因?yàn)樯矸葑C大小是固定的 這里可以按照比例截取身份證號的區(qū)域

3.把截取圖像用tess4j進(jìn)行識別

4.效果圖

二、部分代碼

2.1圖片剪輯

public static Mat cutRect(Mat image) {
Mat clone=image.clone();
Mat src=image.clone();

Imgproc.GaussianBlur(clone, clone, new Size(3, 3), 0, 0);
HighGui.imshow("GaussianBlur1", clone);

Imgproc.cvtColor(clone, clone,Imgproc.COLOR_BGR2GRAY);
HighGui.imshow("GRY1", clone);

int lowThresh=20;
//邊緣檢測
Imgproc.Canny(clone, clone,lowThresh, lowThresh*3,3);
HighGui.imshow("Canny1", clone);

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();

// 尋找輪廓
Imgproc.findContours(clone, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
System.out.println("輪廓:"+contours.size());
// 找出匹配到的最大輪廓
double area = Imgproc.boundingRect(contours.get(0)).area();
int index = 0;

// 找出匹配到的最大輪廓
for (int i = 0; i < contours.size(); i++) {
    double tempArea = Imgproc.boundingRect(contours.get(i)).area();
    if (tempArea > area) {
        area = tempArea;
        index = i;
    }
}

MatOfPoint2f matOfPoint2f = new MatOfPoint2f(contours.get(index).toArray());

RotatedRect rect = Imgproc.minAreaRect(matOfPoint2f);

Mat temp = new Mat(src , rect.boundingRect());
Mat t = new Mat();
temp.copyTo(t);

HighGui.imshow("cut", temp);
return t;
}

2.2身份證提取位置

參考

public static String card(Mat mat){
    Point point1=new Point(mat.cols()*0.34,mat.rows()*0.80);
    Point point2=new Point(mat.cols()*0.34,mat.rows()*0.80);
    Point point3=new Point(mat.cols()*0.89,mat.rows()*0.91);
    Point point4=new Point(mat.cols()*0.89,mat.rows()*0.91);
    List<Point> list=new ArrayList<>();
    list.add(point1);
    list.add(point2);
    list.add(point3);
    list.add(point4);
    Mat card= shear(mat,list);
    card=ImageUtil.drawContours(card,50);
    HighGui.imshow("card", card); 
  //高斯濾波
  	Imgproc.cvtColor(card, card,Imgproc.COLOR_BGR2GRAY);
  	Imgproc.GaussianBlur(card, card, new Size(3, 3), 0, 0);
  	Imgproc.threshold(card, card, 165, 255, Imgproc.THRESH_BINARY);
	 //Imgproc.Canny(image, image,lowThresh, lowThresh*3,3);
    System.out.println(ImageUtil.getImageMessage(Mat2BufImg(card,".jpg"),"eng"));
    return null;
}

2.3圖片數(shù)字識別 tess4j

語言包的下載

pom

<dependency>
  <groupId>net.sourceforge.tess4j</groupId>
  <artifactId>tess4j</artifactId>
  <version>4.5.4</version>
</dependency>
public static String getImageMessage(BufferedImage img,String language){
    String result="";
    try{
        ITesseract instance = new Tesseract();
        instance.setTessVariable("user_defined_dpi", "300");
        //語言包的位置
        File tessDataFolder = new File("E:\\tessdata-master");
        instance.setLanguage(language);
        instance.setDatapath(tessDataFolder.getAbsolutePath());
        result = instance.doOCR(img);
        System.out.println(result);
    }catch(Exception e){
    	e.printStackTrace();
    }
    return result;
}

到此這篇關(guān)于java調(diào)用opencv身份證號識別詳解的文章就介紹到這了,更多相關(guān)java opencv身份證號識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論