gpt4 book ai didi

java - 使用 javacv 进行 OCR

转载 作者:行者123 更新时间:2023-12-02 09:21:22 25 4
gpt4 key购买 nike

我正在为我的项目制作 OCR 并卡在一个点上,现在我正在根据轮廓执行分割,它在很少的图像上工作正常,但即使图像质量很好,也很少有失败的地方,我如果有人建议我更准确的方法,并且如果有人提供代码示例,这是我当前的代码,我将不胜感激。

public static void imageBinarization(IplImage src, IplImage dst){
IplImage r, g, b, s;
r = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
g = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
b = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);

cvSplit(src, r, g, b, null);

s = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);

cvAddWeighted(r, 1./3., g, 1./3., 0.0, s);
cvAddWeighted(s, 2./3., b, 1./3., 0.0, s);
cvThreshold(s, dst, 100, 100, CV_THRESH_BINARY_INV);
cvReleaseImage(r);
cvReleaseImage(g);
cvReleaseImage(b);
cvReleaseImage(s);
}
public static void imageSegmentation(String sourcePath, String targetPath){
cvConvert(t0, mat0);
cvConvert(t8, mat8);
cvConvert(t9, mat9);

IplImage image = cvLoadImage(sourcePath);
IplImage grayImage = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);

//cvSmooth(image, image, CV_BLUR_NO_SCALE, 2);

//cvSmooth(image, image, CV_BLUR, 9, 9, 2, 2);

//cvSmooth(image, image, CV_GAUSSIAN, 3);

imageBinarization(image, grayImage);



CvMemStorage mem;
CvSeq contours = new CvSeq();
CvSeq ptr = new CvSeq();
mem = cvCreateMemStorage(0);
CvRect rect = null;
int px1,px2, py1, py2;

CvScalar blue = CV_RGB(0, 0, 250);
int n = 0; int i = 0;
cvFindContours(grayImage, mem, contours, sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

Random rand = new Random();
for (ptr = contours; ptr != null; ptr = ptr.h_next()) {

Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
CvScalar color = CV_RGB( randomColor.getRed(), randomColor.getGreen(), randomColor.getBlue());

rect = cvBoundingRect(ptr, n);//new CvRect(cvGetSeqElem(c, c.total()));
px1 = rect.x(); py1 = rect.y(); px2 = (rect.x() + rect.width()); py2 = (rect.y() + rect.height());
cvRectangle(image, cvPoint(px1, py1), cvPoint(px2, py2), blue, 1, CV_AA, 0);

//----
xbox = rect.x(); ybox = rect.y(); wbox = rect.width(); hbox = rect.height();
img = cvCreateImage(cvSize(wbox, hbox), image.depth(), image.nChannels());
cvSetImageROI(image, cvRect(xbox, ybox, wbox, hbox));
cvCopy(image, img);
cvResetImageROI(image);

//cvSaveImage(targetPath+i+".jpg", img);
i++;
//---
//cvDrawContours(image, ptr, color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
}
cvSaveImage(targetPath+"mat.jpg", image);
}

最佳答案

尝试使用一些全局阈值算法,例如 Otsu。但 JavaCV 还没有实现这一点。因此,尝试使用直方图处理找到 Otsu 阈值级别并将该值应用于

cvThreshold(s, dst, 100, 100, CV_THRESH_BINARY_INV);

关于java - 使用 javacv 进行 OCR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9648482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com