OCR与javacv [英] OCR with javacv

查看:240
本文介绍了OCR与javacv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目制作一个OCR并坚持一个点,现在我正在基于轮廓进行分割它的工作正常,只有很少的图像,但是即使图像质量很好,也很少有它失败的地方如果有人建议我更准确的方式,如果有人提供代码示例,这将是我当前的代码。

I am making an OCR for my project and stuck on a point, Right now i am performing segmentation on the basis of contours its working fine with few images but there few more where it fails even when the image quality is good, I would appreciate if someone suggest me more accurate way, and if someone provide a code example, here is my current code.

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);
}


推荐答案

尝试使用一些全局阈值大津等算法。但是JavaCV还没有实现。因此,尝试使用直方图处理找到Otsu阈值水平,并将该值应用于

Try to use some Global Thresholding algorithm such as Otsu. But JavaCV haven't implemented that. So try to find the Otsu threshold level using histogram processing and apply that value to

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

这篇关于OCR与javacv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆