使用Android中的OpenCV提取按颜色检测到的区域
用于Android的openCV SDK随附的名为"color-blob-detection"的示例项目可用于标识特定颜色的区域.我需要提取该区域并将其作为位图保存到手机内存中.
The sample project named "color-blob-detection" that comes with the openCV SDK for android can be used to identify an area of a particular color. What I require is to extract that area and save it into the phone memory as a bitmap.
这是我到目前为止所了解的:
This is what I have understood so far:
有一个轮廓列表:
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
可以使用以下方式找到轮廓:
Contours are found using:
Imgproc.findContours(mDilatedMask, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
这将找到最大轮廓区域:
This finds the max contour area:
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中.任何帮助,我们将不胜感激!
I would like to know how this largest area can be saved into the sdcard as a bitmap. Any help is greatly appreciated!
//////编辑
这用于绘制轮廓.我不确定这是否正确:
This is used to draw Contours. I am not sure whether this is the correct way:
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();
}
以下是在C ++中实现的代码,用于查找最大轮廓并将其绘制为图像,我对OpenCV JAVA并不熟悉,但是您可以拥有类似的东西在JAVA中.
Here is the code that implemented in C++ to find largest contour and draw it as an image, I am not familiar with OpenCV JAVA but you can have something like similar in 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);
}
希望这些帮助....