Android - 从图像中裁剪文本(使用openCV或其他任何东西)



我是初学者,我正在尝试为OCR过程制作一个Android模块。为了优化流程,我正在尝试自动裁剪图像的文本部分。我一直在挣扎,但我就是做不到。我找到了几个这样的教程,但它不是用java的,我的大脑就是无法工作。任何帮助将不胜感激,我仍在努力学习。已经研究了几天了。

目前使用 openCV 进行一些后处理以提高准确性,并从图像中提取数据(来自某些机器的收据(,我正在使用正则表达式来获取相关数据(数据、时间、机器编号 5 位,以及一些 6 位数字,有时它不起作用,这就是我现在能想到的(。

尝试尽可能改进流程。如果需要,我可以提供代码,但这完全是一团糟。我创建了一个单独的安卓工作室项目,仅用于opencv处理。

很抱歉文字很长,希望改进(我认为我真的是一个初学者(。谢谢!

忘了提 - 使用 TESSERACT 进行 OCR 过程,使用 openCV 进行图像处理。该应用程序将被一些不太精通技术的人使用,我想使用手动裁剪工具,但它不会有太大用处。 全部在设备上完成,无法连接互联网。

需要裁剪图像的文本部分

附加代码,可在文本上创建框(具有一定程度的准确性(

仍然愿意就如何提高准确性提出建议,谢谢!

public Vector<Rect> detectLetters(Mat img){
Mat img_gray = new Mat();
Mat img_sobel = new Mat();
Mat img_threshold = new Mat();
Mat element = new Mat();
Mat contourOutput = new Mat();
Vector<Rect> boundRect = new Vector<>();
Imgproc.cvtColor(img, img_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.Sobel(img_gray, img_sobel, CvType.CV_8U, 1,0,3,1,0,BORDER_DEFAULT);
Imgproc.threshold(img_sobel, img_threshold, 0, 255, Imgproc.THRESH_OTSU+Imgproc.THRESH_BINARY);
element = getStructuringElement(MORPH_RECT, new Size(30,30));
Imgproc.morphologyEx(img_threshold, img_threshold, 3, element);
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(img_threshold, contours, contourOutput, 0, 1);
Iterator<MatOfPoint> iterator = contours.iterator();
List<MatOfPoint> contours_poly = new ArrayList<>(contours.size());
for (int i=0; i<contours.size(); i++){
if(contours.get(i).toArray().length > 100){
double epsilon = 0.1*Imgproc.arcLength(new MatOfPoint2f(contours.get(1).toArray()),true);
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(1).toArray()),approx,epsilon,true);
Rect appRect = Imgproc.boundingRect(contours.get(i));
if(appRect.width > appRect.height);
boundRect.add(appRect);
}
}
return boundRect;
}

ML Kit 文本识别是您需要的东西吗?

相关内容

最新更新