I'm trying to make it can detect color blob more than one rectangle. But it just show me one rectangle. Can anybody tell me what's the problem?
public static void detectMultipleBlob(Mat src, Mat image, String text, Mat dst){ List<matofpoint> contours = new ArrayList<matofpoint>(); //vector<vector<point> > contours; Mat hierarchy = new Mat(); src.copyTo(dst);
Imgproc.findContours(image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
int k = getBiggestContourIndex(contours); Rect boundRect = setContourRect(contours, k);
Point center = new Point(); getCenterPoint(boundRect.tl(), boundRect.br(), center); Core.rectangle(dst, boundRect.tl(), boundRect.br(), new Scalar(255, 255, 0), 2, 8, 0 );
Core.putText(dst, text, boundRect.tl(), 0/font/, 1, new Scalar(255, 0, 0, 255), 3); }
public static void getCenterPoint(Point tl, Point br, Point dst){ dst.x = (tl.x + br.x)/2; dst.y = (tl.y + br.y)/2; }
public static Rect setContourRect(List<matofpoint> contours,int k){ Rect boundRect = new Rect(); Iterator<matofpoint> each = contours.iterator(); int j = 0; while (each.hasNext()){ MatOfPoint wrapper = each.next(); if (j==k){ return Imgproc.boundingRect( wrapper ); } j++; } return boundRect; }
public static int getBiggestContourIndex(List<matofpoint> contours){ double maxArea = 0; Iterator<matofpoint> each = contours.iterator(); int j = 0; int k = -1; while (each.hasNext()) { MatOfPoint wrapper = each.next(); double area = Imgproc.contourArea(wrapper); if (area > maxArea){ maxArea = area; k = j; } j++; } return k; }