【问题标题】:Extract area detected by color using OpenCV in Android在Android中使用OpenCV提取颜色检测区域
【发布时间】:2013-09-22 03:03:02
【问题描述】:

openCV SDK for android 附带的名为“color-blob-detection”的示例项目可用于识别特定颜色的区域。我需要的是提取该区域并将其作为位图保存到手机内存中。

这是我目前所理解的:

有一个轮廓列表:

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

使用以下方法找到轮廓:

Imgproc.findContours(mDilatedMask, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

这会找到最大轮廓区域:

double maxArea = 0;
        Iterator<MatOfPoint> each = contours.iterator();
        while (each.hasNext()) {
            MatOfPoint wrapper = each.next();
            double area = Imgproc.contourArea(wrapper);
            if (area > maxArea)
                maxArea = area;
        }

我想知道如何将这个最大的区域作为位图保存到 sdcard 中。非常感谢任何帮助!

//////编辑

这用于绘制轮廓。我不确定这是否是正确的方法:

 Imgproc.cvtColor(mDilatedMask, mDilatedMask, Imgproc.COLOR_GRAY2BGR);
            Imgproc.drawContours(mDilatedMask, contours, -1, new Scalar(0, 255, 0), 1);
Toast.LENGTH_LONG).show();
            Bitmap bmpOut = Bitmap.createBitmap(mDilatedMask.cols(), mDilatedMask.rows(), Bitmap.Config.ARGB_8888);

            Utils.matToBitmap(mDilatedMask, bmpOut);

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

【问题讨论】:

    标签: android opencv bitmap area contour


    【解决方案1】:

    这是用 C++ 实现的代码,用于查找最大轮廓并将其绘制为图像,我不熟悉 OpenCV JAVA,但您可以在 JAVA 中使用类似的东西。

    #include <iostream>
    #include "opencv2\highgui\highgui.hpp"
    #include "opencv\cv.h"
    
    using namespace cv;
    using namespace std;
    int main()
    {
     int largest_area=0;
     int largest_contour_index=0;
     Rect bounding_rect;
    
     Mat src = imread("src.jpg"); //Load source image
     Mat thr(src.rows,src.cols,CV_8UC1);
     Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
     cvtColor(src,thr,CV_BGR2GRAY); //Convert to gray
     threshold(thr, thr,25, 255,THRESH_BINARY); //Threshold the gray
    
        vector<vector<Point>> contours; // Vector for storing contour
        vector<Vec4i> hierarchy;
    
        findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
    
         for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
          {
           double a=contourArea( contours[i],false);  //  Find the area of contour
           if(a>largest_area){
           largest_area=a;
           largest_contour_index=i;                //Store the index of largest contour
           bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
           }
    
          }
    
     Scalar color( 255,255,255);
     drawContours( dst, contours,largest_contour_index, color, CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
     rectangle(src, bounding_rect,  Scalar(0,255,0),1, 8,0); 
     imshow( "src", src );
     imshow( "largest Contour", dst );
     waitKey(0);
    }
    

    希望这些帮助....

    【讨论】:

    • 此代码是否允许仅识别区域或识别和提取?
    • 在上面的例子中,代码找到最大的轮廓并绘制它。在您的情况下,我认为您已经获得了最大的轮廓,然后在 Java 中使用 drawContours(docs.opencv.org/java/2.4.6/org/opencv/imgproc/…) 简单地绘制它。
    • 我使用了这样的 drawContouer() 方法: Imgproc.drawContours(mDilatedMask, contours, -1, new Scalar(0, 255, 0), 1);当我保存图像时,我只能看到一个带有绿色边框的黑色矩形。我看不到矩形内的信息。我已经编辑了我的代码
    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    相关资源
    最近更新 更多