我正在尝试开发一个简单的PC应用程序,用于车牌识别(Java + OpenCV + Tess4j)。图像质量不太好(以后会改善),我想对图像进行预处理以供tesseract使用,但目前我卡在了车牌检测(矩形检测)上。
我的步骤:
1)原始图像
Mat img = new Mat();
img = Imgcodecs.imread("sample_photo.jpg");
Imgcodecs.imwrite("preprocess/True_Image.png", img);
2) 灰度
Mat imgGray = new Mat();
Imgproc.cvtColor(img, imgGray, Imgproc.COLOR_BGR2GRAY);
Imgcodecs.imwrite("preprocess/Gray.png", imgGray);
3) 高斯模糊
Mat imgGaussianBlur = new Mat();
Imgproc.GaussianBlur(imgGray,imgGaussianBlur,new Size(3, 3),0);
Imgcodecs.imwrite("preprocess/gaussian_blur.png", imgGaussianBlur);
4)自适应阈值
Mat imgAdaptiveThreshold = new Mat();
Imgproc.adaptiveThreshold(imgGaussianBlur, imgAdaptiveThreshold, 255, CV_ADAPTIVE_THRESH_MEAN_C ,CV_THRESH_BINARY, 99, 4);
Imgcodecs.imwrite("preprocess/adaptive_threshold.png", imgAdaptiveThreshold);
这里应该是第五步,即检测车牌区域(暂时可能不需要去倾斜)。
我使用画图软件从图像中裁剪出所需的区域(在第四步之后),得到了:
然后我通过tesseract,tess4j进行了OCR识别:
File imageFile = new File("preprocess/adaptive_threshold_AFTER_PAINT.png");
ITesseract instance = new Tesseract();
instance.setLanguage("eng");
instance.setTessVariable("tessedit_char_whitelist", "acekopxyABCEHKMOPTXY0123456789");
String result = instance.doOCR(imageFile);
System.out.println(result);
并获得了足够好的结果 - "Y841ox EH"(几乎正确)
在第四步之后,我该如何检测和裁剪车牌区域?我需要在1-4步中进行一些改进吗?希望看到一些使用Java + OpenCV(而不是JavaCV)实现的示例。
提前感谢。
编辑(感谢@Abdul Fatir的答案)
好吧,我为那些对这个问题感兴趣的人提供了工作(至少对我来说是这样)的代码示例(Netbeans+Java+OpenCV+Tess4j)。 代码不是最好的,但我只是为了学习而编写它。
http://pastebin.com/H46wuXWn(不要忘记将tessdata文件夹放入项目文件夹中)