【问题标题】:Extracting information from the largest rectangle in an image using openCV in Android在Android中使用openCV从图像中最大的矩形中提取信息
【发布时间】:2013-09-02 23:54:26
【问题描述】:

我使用以下代码找出从 android 相机捕获的图像的最大矩形:

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

        Bitmap bmpOriOut = Bitmap.createBitmap(imgSource.cols(), imgSource.rows(), Bitmap.Config.ARGB_8888);

        Utils.matToBitmap(imgSource, bmpOriOut);

        try {
            bmpOriOut.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/original.jpg"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //convert the image to black and white, commenting this wont crash
        Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2RGB);

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

        //apply gaussian blur to smoothen lines of dots, commenting this crashes
        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();
        Bitmap bmpOut = Bitmap.createBitmap(imgSource.cols(), imgSource.rows(), Bitmap.Config.ARGB_8888);

        Utils.matToBitmap(imgSource, bmpOut);

        try {
            bmpOut.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/bigrect.jpg"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return imgSource;
    }

成功检测到图像中的最大矩形,如下所示:http://img153.imageshack.us/img153/9308/nn4w.png

但我的问题是,如何将检测到的最大矩形保存为位图(没有黑白) 非常感谢任何帮助。

【问题讨论】:

    标签: android opencv image-processing contour rectangles


    【解决方案1】:

    而不是将其绘制在imgSource 矩阵上:

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

    创建一个空的黑色/白色Mat 并在其上绘图。像这样的东西(黑色初始矩阵):

    emptyMat = Mat.zeros(imgSource.size(), imgSource.channels());
    ...
    Imgproc.drawContours(emptyMat, largest_contours, -1, new Scalar(0, 255, 0), 1);
    

    当然,您可以使用Mat 构造函数来创建您想要的初始背景矩阵。

    【讨论】:

    • 我现在正在尝试,你能告诉我如何将它保存为RGB图像而不是黑白图像。
    • 你的代码中已经有了它。您可以将cvtColorImgproc.COLOR_GRAY2RGB 一起使用;或者在初始化时,使用 3 个通道而不是 imgSource.channels(),后者有 1 个。
    • openCV SDK 提供的名为“OpenCV Sample - color-blob-detection”的示例项目根据颜色识别区域。但不可能提取该区域。你能指导我吗?
    • 我记不清那个例子了。你能告诉我它是如何“识别”该区域的吗?如果它适用于该区域的轮廓,那么它已经在进行提取,剩下要做的唯一工作就是如上所述在空矩阵上绘制轮廓。
    • 请运行名为“OpenCV Sample - color-blob-detection”的 openCV SDK 可用的项目...我没有经验来说明如何检测该区域。我只想提取检测到的那个区域。请帮忙!
    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 2020-07-24
    • 2011-02-28
    • 1970-01-01
    • 2012-05-10
    • 2020-03-05
    • 2014-12-22
    • 2023-03-22
    相关资源
    最近更新 更多