android - OpenCV Android 使用最大轮廓的边缘创建新图像

标签 android opencv edge-detection

我能够检测图像中最大的正方形/矩形(绿色)。但是,我想将图像中检测到的最大正方形/矩形转换为新图像(存储在新的 Mat 中)。

这是具有最大矩形/正方形的函数的返回图像:http://img153.imageshack.us/img153/9308/nn4w.png

到目前为止,这是我的代码:

private Mat findLargestRectangle(Mat original_image) {
    Mat imgSource = original_image;

    //convert the image to black and white
    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);

    //convert the image to black and white does (8 bit)
    Imgproc.Canny(imgSource, imgSource, 50, 50);

    //apply gaussian blur to smoothen lines of dots
    Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);

    //find the contours
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = -1;
    int maxAreaIdx = -1;
    MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    Mat largest_contour = contours.get(0);
    List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
    for (int idx = 0; idx < contours.size(); idx++) {
        temp_contour = contours.get(idx);
        double contourarea = Imgproc.contourArea(temp_contour);
        //compare this contour to the previous largest contour found
        if (contourarea > maxArea) {
            //check if this contour is a square
            MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
            int contourSize = (int)temp_contour.total();
            Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);
            if (approxCurve.total() == 4) {
                maxArea = contourarea;
                maxAreaIdx = idx;
                largest_contours.add(temp_contour);
                largest_contour = temp_contour;
            }
        }
    }
    MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1);
    largest_contours = new ArrayList<MatOfPoint>();
    largest_contours.add(temp_largest);

    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
    Imgproc.drawContours(imgSource, largest_contours, -1, new Scalar(0, 255, 0), 1);

    //create the new image here using the largest detected square

    Toast.makeText(getApplicationContext(), "Largest Contour: ", Toast.LENGTH_LONG).show();

    return imgSource;
}

largest_contours 变量是一个 MatOfPoint 列表,但只包含最大的轮廓,也存储在 largest_contour 变量中。我怎样才能从最大的轮廓制作新图像?

我在 Android 中使用 OpenCV,检测图像的教程很少,但不完全是关于如何使用 Imgproc.warpPerspective()

谢谢!

最佳答案

您只需要找到该轮廓的角即可。您可以使用 extreme points方法。

您应该简单地找出具有最小 x 和最小 y(这是您的左上角)、最小 x 和最大 y(这是您的左下角)等的点。

在 C++ 中,有一个名为 algoritm 的库,它具有最小/最大方法。例如min_element将帮助您找到具有最小 x 或 y 的点。不要忘记包含标题。

有了你的4个点之后,你就可以使用透视变换了。

先将积分输入this method .目的地应该是

Point2f dest[4] = {(0,0),(image.width,0),(0,image.height),(image.height,image.width)}

针对您的情况。您从此方法获得的矩阵 (M) 将使用 another method 将您的点转换为目的地.

祝你好运。

编辑:再三考虑;由于您的轮廓不是平行四边形,因此您的极值点为 min x = bottomleft,min y = totopleft,等等。这样就更容易找到最小元素了。

关于android - OpenCV Android 使用最大轮廓的边缘创建新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17611494/

相关文章:

ios - 边缘检测 --> CGPath --> SpriteKite 碰撞

android: double onclicklistener 退出

android - 谷歌地图在应用程序内部不起作用

python - 使用 Python Opencv 在图像中查找问题文本 block

python - 如何从文档中删除所有肖像图片

matlab - MATLAB 中的 Canny 边缘检测器

android - 使用cocos2d-android在android中添加EditText

android - 如何在Android项目中从头开始设置DAGGER依赖注入(inject)?

OpenCV cvMatchTemplate 相同的图像大小

image - 选择霍夫变换的参数