简体   繁体   English

使用OpenCV从UIImage检测Image对象

[英]Detect Image object from UIImage using OpenCV

I am completely new for OpenCV, but during googling I came to know about Object Detection & Edge Detection. 我对OpenCV来说是全新的,但在谷歌搜索期间我开始了解对象检测和边缘检测。 But, Still not able to figure out proper way to Detect Image from ScreenShot. 但是,仍然无法找到从ScreenShot检测图像的正确方法。

For Example, If I pass an image having photo inside it like below, then I need to extract that Photo from source Image. 例如,如果我像下面一样传递带有照片的图像,那么我需要从源图像中提取该照片。

在此输入图像描述

EDIT After following Answer of @Amitay Nachmani, I tried to implement the following code up to step 4. 编辑在听完@Amitay Nachmani的答案后,我尝试将以下代码实施到第4步。

-(UIImage*)processImage:(UIImage*)sourceImage{

    cv::Mat processMat;
    UIImageToMat(sourceImage, processMat);

    cv::Mat grayImage;
    cvtColor(processMat, grayImage, CV_BGR2GRAY);

    cv::Mat cannyImage;
    cv::Canny(grayImage, cannyImage, 0, 50);


    cv::Vec2f lines2;
    std::vector<cv::Vec2f> lines;
    cv::HoughLines(cannyImage, lines, 1, CV_PI/180, 300);

    size_t sizeOfLine = lines.size();
    for(size_t i=0;i<sizeOfLine;i++){
        float rho = lines[i][0], theta = lines[i][1];

        if(rho==0){
            cv::Point pt1,pt2;
            double a = cos(theta), b = sin(theta);
            double x0 = a*rho, y0 = b*rho;
            pt1.x = cvRound(x0 + 1000*(-b));
            pt1.y = cvRound(y0 + 1000*(a));
            pt2.x = cvRound(x0 - 1000*(-b));
            pt2.y = cvRound(y0 - 1000*(a));

            cv::line(cannyImage, pt1, pt2, cv::Scalar(255,0,0),2.0);
        }
    }

    UIImage *result = MatToUIImage(cannyImage);
    return result;
}

From above code, I got generated following Image. 从上面的代码,我得到了以下Image。 在此输入图像描述

EDIT 2 I revised code by replacing Condition if(rho==0) with if(theta==0) 编辑2我通过用if(theta==0)替换条件if(rho==0)修改代码

This resulted in below Image 这导致下面的图像 在此输入图像描述

But, Still What to do next ? 但是,还有下一步做什么? I am bit confused in next Steps. 我在后续步骤中有点困惑。

I am not completely sure but, did you try template matching technique? 我不完全确定,但你尝试过模板匹配技术吗? If you are using c++ to code opencv: http://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html 如果您使用c ++编写opencv代码: http//docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

I hope this will be helpful to find cross-correlation between template (your source image) and your test image (screenshot). 我希望这有助于找到模板(源图像)和测试图像(屏幕截图)之间的互相关。

In the link below you will find a complete example of how to apply and draw template matching. 在下面的链接中,您将找到有关如何应用和绘制模板匹配的完整示例。

Hope this helps. 希望这可以帮助。

Cheers. 干杯。

Unai. 垂发。

If you know that the image is always between the second horizontal line and the third i would do the following: 如果您知道图像始终位于第二条水平线和第三条水平线之间,则会执行以下操作:

  1. Convert to gray scale (opencv cvtColor) 转换为灰度(opencv cvtColor)
  2. Run Canny edge detection (opecv Canny()) 运行Canny边缘检测(opecv Canny())
  3. Find lines using Hough lines (opencv HoughLines() http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html ) 使用Hough行查找行(opencv HoughLines() http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html
  4. Take only the 4 most prominent horizontal lines ( to take the horizontal ones you need theta = 90) 只取4条最突出的水平线(取水平线,你需要theta = 90)
  5. Sort the lines you find according to the y coordinate 根据y坐标对找到的行进行排序
  6. Crop the image between the second and third line 在第二行和第三行之间裁剪图像

I completely agree with the post below, this is the best solution but unfortunately, I guess that @Mrug development will be targeted to smartphone devices, and canny edge detection and hough line transform are computationally very expensive from those platforms. 我完全同意下面的帖子,这是最好的解决方案,但遗憾的是,我认为@Mrug开发将针对智能手机设备,并且canny边缘检测和hough line变换在这些平台上的计算成本非常高。

May be you can use Sobel derivates which are designed to calculate horizontal and vertical derivates. 也许你可以使用Sobel衍生物来设计水平和垂直衍生物。

These links may help you: 这些链接可以帮助您:

Sobel Derivates http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html Sobel Derivates http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html

Canny edge detectors http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/canny_detector/canny_detector.html Canny边缘探测器http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/canny_detector/canny_detector.html

Hough transform: 霍夫变换:

http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM