OCR with javacv

徘徊边缘 提交于 2019-12-07 15:46:24

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/9648482/ocr-with-javacv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!