在Android中使用OpenCV提取颜色检测的区域

在Android中使用OpenCV提取颜色检测的区域,android,opencv,bitmap,area,contour,Android,Opencv,Bitmap,Area,Contour,android版openCV SDK附带的名为“颜色斑点检测”的示例项目可用于识别特定颜色的区域。我需要的是提取该区域并将其作为位图保存到手机内存中 这就是我迄今为止所理解的: 有一个等高线列表: List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 这将查找最大轮廓面积: double maxArea = 0; Iterator<MatOfPoint> each = contour

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

这就是我迄今为止所理解的:

有一个等高线列表:

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
这将查找最大轮廓面积:

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;
        }

这里是用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);
}
#包括
#包括“opencv2\highgui\highgui.hpp”
#包括“opencv\cv.h”
使用名称空间cv;
使用名称空间std;
int main()
{
int最大面积=0;
int最大轮廓指数=0;
矩形边界;
Mat src=imread(“src.jpg”);//加载源映像
材料厚度(src.行、src.列、CV_8UC1);
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
CVT颜色(src、thr、CVBGr2灰色);//转换为灰色
阈值(thr,thr,25255,THRESH_BINARY);//灰度阈值
矢量轮廓;//用于存储轮廓的矢量
向量层次;
查找轮廓(thr、轮廓、层次、CV_RETR_CCOMP、CV_CHAIN_APPROX_SIMPLE);//在图像中查找轮廓
对于(int i=0;i最大面积){
最大面积=a;
最大轮廓指数=i;//存储最大轮廓指数
bounding_rect=boundingRect(轮廓[i]);//查找最大轮廓的边界矩形
}
}
标量颜色(255255);
drawContours(dst、等高线、最大等高线索引、颜色、CV填充、8、层次);//使用以前存储的索引绘制最大等高线。
矩形(src,边界矩形,标量(0255,0),1,8,0);
imshow(“src”,src);
imshow(“最大轮廓”,dst);
等待键(0);
}

希望这些帮助……< /P> < P>这是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);
}
#包括
#包括“opencv2\highgui\highgui.hpp”
#包括“opencv\cv.h”
使用名称空间cv;
使用名称空间std;
int main()
{
int最大面积=0;
int最大轮廓指数=0;
矩形边界;
Mat src=imread(“src.jpg”);//加载源映像
材料厚度(src.行、src.列、CV_8UC1);
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
CVT颜色(src、thr、CVBGr2灰色);//转换为灰色
阈值(thr,thr,25255,THRESH_BINARY);//灰度阈值
矢量轮廓;//用于存储轮廓的矢量
向量层次;
查找轮廓(thr、轮廓、层次、CV_RETR_CCOMP、CV_CHAIN_APPROX_SIMPLE);//在图像中查找轮廓
对于(int i=0;i最大面积){
最大面积=a;
最大轮廓指数=i;//存储最大轮廓指数
bounding_rect=boundingRect(轮廓[i]);//查找最大轮廓的边界矩形
}
}
标量颜色(255255);
drawContours(dst、等高线、最大等高线索引、颜色、CV填充、8、层次);//使用以前存储的索引绘制最大等高线。
矩形(src,边界矩形,标量(0255,0),1,8,0);
imshow(“src”,src);
imshow(“最大轮廓”,dst);
等待键(0);
}

希望这些帮助….

此代码只允许识别区域还是识别和提取?在上面的示例中,代码找到最大轮廓并绘制它。在您的例子中,我认为您已经获得了最大的轮廓,然后使用Java中的drawContouer()简单地绘制它;当我保存图像时,我只能看到一个带绿色边框的黑色矩形。我看不到矩形内的信息。我已经编辑了我的代码该代码只允许识别区域还是识别和提取?在上面的示例中,代码找到最大轮廓并绘制它。在您的例子中,我认为您已经获得了最大的轮廓,然后使用Java中的drawContouer()简单地绘制它;当我保存图像时,我只能看到一个带绿色边框的黑色矩形。我看不到矩形内的信息。我已经编辑了我的代码
#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);
}