如何将这个图像处理从Matlab到OpenCV? [英] How to translate this image processing from Matlab to OpenCV?

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

问题描述

以下连结使用Matlab从图片中移除非文字内容。我想在Java中使用OpenCV做同样的事情。



我没有一个Matlab来尝试,我是新的OpenCV。虽然我知道关于这个过程背后的理论的一些基础,但是很难使从Matlab语言翻译成OpenCV 3.0。最好在Java中。





ADD 4 - 在VC ++ 2013中编码



下面是我现在尝试参考

解决方案

发布为答案只是为了显示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天全站免登陆