如何将这种图像处理从Matlab转换成OpenCV?

2024-06-02 06:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的链接使用Matlab从图像中删除非文本内容。我想在Java中用OpenCV做同样的事情。在

我没有一个Matlab可以尝试,而且我是OpenCV的新手。虽然我对这个过程背后的理论有一些基本的了解,但是从Matlab语言到opencv3.0的转换有点困难。最好是Java语言。在

http://www.mathworks.com/help/vision/examples/automatically-detect-and-recognize-text-in-natural-images.html

添加1-带MSER的区域检测(尚未解决)

对于MSER检测,我可以使用以下代码来检测MSER关键点。在

public static void MSERdetector(String imgName1, String suffix1) {
    Mat imgMat1 = Imgcodecs.imread(picDir + imgName1 + "." + suffix1, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    String outImgName1 = picDir + "MSER" + "_keypoints_" + imgName1 + "_"   + ".tif";
    Mat outImg1 = new Mat();        

FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.MSER); // create the feature detector

MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
featureDetector.detect(imgMat1, keypoints1);

if (!keypoints1.empty()) {
    Features2d.drawKeypoints(imgMat1, keypoints1, outImg1);
    Imgcodecs.imwrite(outImgName1, outImg1);
    System.out.println("done");
}
else {
    System.out.println("No keypoints found for: " + imgName1);
}

}

输出如下: enter image description here

但我不知道如何将关键点转换为区域。我需要的是:

enter image description here

添加2-Canny边和与MSER区域相交(尚未解析)

一旦我能找到MSER区域,我就应该intersect it with Canny edges。我可以找到一些精明的边缘如下。但是我不知道怎么做intersection操作。在

^{pr2}$

我的canny edges输出如下所示: enter image description here

添加3-现在我转向使用VS2013社区

要使用VS2013设置OpenCV,请选中here。在

VC-4编码ADD

下面是我现在对here的引用。在

//Step2: Detect MSER regions
Mat grayImage;
cvtColor(colorImage, grayImage, CV_BGR2GRAY);
imshow("Gray Image", grayImage);
waitKey(0);


Ptr<MSER> mserExtractor = MSER::create(); // create MSER extractor with default parameters. http://code.opencv.org/projects/opencv/wiki/MSER http://docs.opencv.org/master/d3/d28/classcv_1_1MSER.html#a49d72a1346413106516a7fc6d95c09bb
mserExtractor->setMinArea(150);
mserExtractor->setMaxArea(2000);
//Mat mserOutMask = Mat::zeros(grayImage.rows, grayImage.cols, CV_8UC3);

Mat vis;
//vis = Mat::zeros(grayImage.rows, grayImage.cols, CV_8UC3);
grayImage.copyTo(vis);

vector<vector<Point>> mserContours;
vector<Rect> mserBBox;//what's this?
mserExtractor->detectRegions(grayImage, mserContours, mserBBox);

for (int i = 0; i<mserContours.size(); i++)
{
    drawContours(vis, mserContours, i, Scalar(255, 255, 255), 4);
}

imshow("MSER by contours", vis);
waitKey(0);

Mat vis2;
grayImage.copyTo(vis2);
for (vector<cv::Point> v : mserContours){
    for (cv::Point p : v){
        vis2.at<uchar>(p.y, p.x) = 255;
    }
}
imshow("MSER by points", vis);
waitKey(0);

我得到的是:

vis1 - MSER by contoursenter image description here

vis2 - MSER by pointsenter image description here

加5

我只是按照Miki的建议用text detection sample做了实验。 它需要一些经过训练的模型文件才能运行。花了将近2分钟才结束,但我们可以稍后离开演出。我的方案是从复杂的屏幕截图中提取OCR文本(抱歉,直到现在才透露)。虽然这样的效果对自然景物是相当好的。对于截图来说就没那么吸引人了。结果如下:

enter image description here


Tags: 区域forbycreateviscvvectormat