问题
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:
There is a list of contours:
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;
}
I would like to know how this largest area can be saved into the sdcard as a bitmap. Any help is greatly appreciated!
//////EDIT
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();
}
回答1:
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);
}
Hope these help....
来源:https://stackoverflow.com/questions/18853838/extract-area-detected-by-color-using-opencv-in-android