Is Tim
-
使用技术
- OpenCV
- Tesseract
- Java 8
-
OpenCV
以windos10为例,首先从官网下载exe执行文件,执行安装,安装完后在opencv\build目录下找到java文件夹,将jar包和dll文件复制到项目中,在项目中引入jar包// 加载动态库 System.load("opencv_java490.dll"); // 读取原始彩色图像 Mat image = Imgcodecs.imread("17119755916493.png"); Mat hsvImage = new Mat(); Imgproc.cvtColor(image, hsvImage, Imgproc.COLOR_BGR2HSV); // 设定红色的HSV范围 Scalar lowerBound = new Scalar(0, 40, 40); // 调整这些值以适应不同的红色 Scalar upperBound = new Scalar(10, 255, 255); Mat mask = new Mat(); Core.inRange(hsvImage, lowerBound, upperBound, mask); // 在掩模图像中查找矩形轮廓 List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // 设定矩形框面积 double minAreaThreshold = 1700; for (int i = 0; i < contours.size(); i++) { Rect rect = Imgproc.boundingRect(contours.get(i)); // // 计算边界框的面积 double area = rect.width * rect.height; // 只有当面积大于等于设定阈值时才保留此轮廓 if (area <= minAreaThreshold) { continue; } // 截取红色边框区域 Mat cropped = new Mat(image, rect); // 在原始图像上绘制矩形框,可用于直观调整参数 // Imgproc.rectangle(image, rect.tl(), rect.br(), new Scalar(0, 255, 0), 2); // 绿色边框,线条宽度为2 // String filenameWithRect = "out_file/original_with_rect"+i+".jpg"; // Imgcodecs.imwrite(filenameWithRect, image); // 将OpenCV Mat转换为BufferedImage供Tesseract识别 BufferedImage bufferedImage = matToBufferedImage(cropped);
-
Tesseract
引入Tesseract Jar包,并下载对于的语言库<dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>4.5.4</version> </dependency>
图片识别
ITesseract instance = new Tesseract(); //设置语言库所在的文件夹位置, instance.setDatapath("tessdata-main"); //设置使用的语言库类型:chi_sim 中文简体 instance.setLanguage("chi_sim"); String result = instance.doOCR(new File("output_gray.jpg")); System.out.println(result);
-
完整例子
public static void main(String[] args) throws TesseractException { // OCR ITesseract instance = new Tesseract(); //设置语言库所在的文件夹位置 instance.setDatapath("tessdata-main"); //设置使用的语言库类型:chi_sim 中文简体 instance.setLanguage("chi_sim"); // 加载动态库 System.load("opencv_java490.dll"); // 读取原始彩色图像 Mat image = Imgcodecs.imread(""); Mat hsvImage = new Mat(); Imgproc.cvtColor(image, hsvImage, Imgproc.COLOR_BGR2HSV); // 设定红色的HSV范围 Scalar lowerBound = new Scalar(0, 40, 40); // 调整这些值以适应不同的红色 Scalar upperBound = new Scalar(10, 255, 255); Mat mask = new Mat(); Core.inRange(hsvImage, lowerBound, upperBound, mask); // 在掩模图像中查找矩形轮廓 List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // 设定矩形框面积 double minAreaThreshold = 1700; for (int i = 0; i < contours.size(); i++) { Rect rect = Imgproc.boundingRect(contours.get(i)); // 计算边界框的面积 double area = rect.width * rect.height; // 只有当面积大于等于设定阈值时才保留此轮廓 if (area <= minAreaThreshold) { continue; } // 截取红色边框区域 Mat cropped = new Mat(image, rect); // 将OpenCV Mat转换为BufferedImage供Tesseract识别 BufferedImage bufferedImage = matToBufferedImage(cropped); // 对裁剪区域进行OCR识别 String result = instance.doOCR(bufferedImage); System.out.println("识别到的文字: "+ i + result); } // 释放资源 image.release(); mask.release(); hierarchy.release(); } // 辅助方法:将OpenCV Mat转换为BufferedImage private static BufferedImage matToBufferedImage(Mat matrix) { int type = BufferedImage.TYPE_BYTE_GRAY; if (matrix.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; } int bufferSize = matrix.channels() * matrix.cols() * matrix.rows(); byte[] buffer = new byte[bufferSize]; matrix.get(0, 0, buffer); BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(buffer, 0, targetPixels, 0, buffer.length); return image; } }