如何将此图像处理从 Matlab 转换为 OpenCV? [英] How to translate this image processing from Matlab to OpenCV?

查看:21
本文介绍了如何将此图像处理从 Matlab 转换为 OpenCV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我没有可试用的 Matlab,而且我是 OpenCV 的新手.虽然我知道一些关于这个过程背后的理论的基础知识,但是将 Matlab 语言翻译成 OpenCV 3.0 有点困难.最好使用 Java.

.

添加 4 - VC++ 2013 中的编码

以下是我现在参考 正如 Miki 所建议的那样.它需要一些经过训练的模型文件才能运行.它花了将近 2 分钟才结束,但我们可以稍后离开表演.我的方案是从复杂的屏幕截图中提取 OCR 文本(很抱歉直到现在才透露).虽然结果对于自然场景来说非常好.它对屏幕截图不那么有吸引力.结果如下:

解决方案

作为答案发布只是为了显示 OpenCV 的结果

您可以在 这里找到一个示例

The link below use Matlab to remove non-text content from an image. I want to do the same thing with OpenCV in Java.

I don't have a Matlab to try with and I am new to OpenCV. Though I know some basics about the theory behind the process, but it's kind of difficult to make the translation from Matlab language into OpenCV 3.0. And preferably in Java.

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

ADD 1 - region detection with MSER (not resolved yet)

For MSER detection, I can use the following code to detect the MSER keypoints.

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

}

And the output looks like this:

But I don't know how to convert keypoints into regions. What I need is below:

ADD 2 - Canny edges and intersection with MSER regions (not resolved yet)

Once I am able to find the MSER regions, I am supposed to intersect it with Canny edges. I can find some Canny edges as below. But I don't know how to do the intersection operation.

public static void CANNYedge(String imgName1, String suffix1) {
    Mat imgMat1 = Imgcodecs.imread(picDir + imgName1 + "." + suffix1, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    //imgMat1 = ImageUtilities.Convert2BW(imgMat1);
    String outImgName1 = picDir + "_CANNY_" + imgName1 + ".tif";
    Mat outImg1 = new Mat();
    Imgproc.Canny(imgMat1, outImg1, 0, 500);
    Imgcodecs.imwrite(outImgName1, outImg1);
}

My canny edges output looks like this:

ADD 3 - Now I turned to use VS 2013 Community

For setting up OpenCV with VS2013, check here.

ADD 4 - Coding in VC++ 2013

Below is what I tried for now with reference to 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);

What I got are these:

vis1 - MSER by contours

vis2 - MSER by points

ADD 5

I just experimented with the text detection sample as suggested by Miki. It requires some trained model files to run. And it took almost 2 minutes to finish but we can leave the performance later. My scenario is to OCR texts from complex screenshots (sorry to reveal that until now). Though the result is quite good for natural scenes. It is not so appealing for screenshots. Below is the result:

解决方案

Posting as an answer just to show the result of the OpenCV text detection example

Now you need to apply text recognition, using for example OCRHMMDecoder

You'll find a sample here

这篇关于如何将此图像处理从 Matlab 转换为 OpenCV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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