JavaCV检测二进制图像上的验证码字母

标签 java opencv captcha javacv

我是 JavaCv 的新手。我的任务是在图像上找到符号并生成彼此具有单个符号的图片。 首先,我有这张照片:enter image description here 然后我做阈值处理并得到这个: enter image description here 我正在尝试使用 cvFindContours 然后在每个符号周围绘制一个矩形,我的代码:

 String filename = "captcha.jpg";
    IplImage firstImage=cvLoadImage(filename);
    Mat src = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
    Mat dst = new Mat();
   threshold(src, dst, 200, 255, 0);
   imwrite("out.jpg", dst);

    IplImage iplImage=cvLoadImage("out.jpg",CV_8UC1);
    CvMemStorage memStorage=CvMemStorage.create();
    CvSeq contours=new CvSeq();
    cvFindContours(iplImage,memStorage,contours,Loader.sizeof(CvContour.class),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
    CvSeq ptr;
    CvRect bb;
    for (ptr=contours;ptr!=null;ptr=ptr.h_next()){
        bb=cvBoundingRect(ptr);
        cvRectangle(firstImage , cvPoint( bb.x(), bb.y() ),
                cvPoint( bb.x() + bb.width(), bb.y() + bb.height()),
                CvScalar.BLACK, 2, 0, 0 );

    }
    cvSaveImage("result.jpg",firstImage);
}

我想得到这样的输出:enter image description here ,但我真的明白了:enter image description here

拜托,需要你的帮助。

最佳答案

您正在为 findContour() 使用“out.jpg”图像。
当您将 dst Mat 保存为“out.jpg”时,JPEG 格式会自动量化您的原始像素数据并为您的图像创建噪声。

将 dst 保存为“out.png”而不是“out.jpg”,或者将 dst Mat 直接用于 findContour()。

已添加源代码:C++ 版本

string filename = "captcha.jpg";
Mat src = imread(filename);
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
Mat thres;
threshold(gray, thres, 200, 255, 0);

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;

findContours(thres.clone(), contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

Mat firstImage = src.clone();
for(int i=0; i< contours.sizes(); i++)
{
    Rect r = boundingRect(contours[i]);
    rectangle(firstImage, r, CV_RGB(255, 0, 0), 2);
}

imwrite("result.png", firstImage);

关于JavaCV检测二进制图像上的验证码字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35889601/

相关文章:

java - 在 JSP 表单中传递 javascript 变量

c++ - OpenCV C++ : How to write a list of string in xml file using OpenCV FileStorage?

java - 尝试编码 VBox 子类时发生 IllegalAnnotationExceptions

opencv - 多 View 几何

android - 如何在android中获取图像的大小

ajax - PRIMEFACES - 如何刷新 <p :graphicImage> from clicking a <p:commandButton>?

c# - 无法解析远程名称 : 'www.google.com' using Recaptcha

javascript - Recaptcha v3 集成

java - 在 Kotlin 中使用带有 @NotNull setter 和 @Nullable getter 的 Java 字段

java - Spring MVC - 跨 Controller 的继承变量值