@Override
public Mat onCameraFrame(Mat inputFrame) {
source =inputFrame;
contours = new ArrayList<MatOfPoint>();
hierarchy = new Mat();
Imgproc.cvtColor(source, mGray, Imgproc.COLOR_RGB2GRAY);
Imgproc.GaussianBlur(mGray, mGray1, new Size(9, 9), 1);
segImage=imageSegmentation(mGray1);
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,new Size(11,11));
Imgproc.dilate(segImage, mGray2, kernel);
Imgproc.findContours(mGray2, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
hierarchy.release();
//Imgproc.drawContours(source, contours, -1, new Scalar(Math.random()*255, Math.random()*255, Math.random()*255));
double maxVal = 0;
maxValIdx = 0;
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
{
double contourArea = Imgproc.contourArea(contours.get(contourIdx));
if (maxVal < contourArea)
{
maxVal = contourArea;
maxValIdx = contourIdx;
}
}
Imgproc.drawContours(source, contours, maxValIdx, new Scalar(0,255,0), 5);
//extract the maximum region
Rect rect = Imgproc.boundingRect(contours.get(maxValIdx));
cropped = source.submat(rect);
Log.i("dd",cropped+"");
return cropped;
}