Regarding Largest Contour, Centroid of Contour.

1,426 views
Skip to first unread message

Rushikesh Khatkhede

unread,
Mar 26, 2012, 1:38:39 PM3/26/12
to android...@googlegroups.com
and Some wonderful people of this group i have been able to hack and get to this point where i am able to get Multiple skin color contours with a rectangle bounding them regarding which i have basically 2 questions:

1.  How can i get only one continues contour from all the detected ones ( largest area contour would be awesome) ?
2.  How can i get the centroid of that contour ?


capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
 Imgproc.cvtColor(mRgba, mIntermediateMat, Imgproc.COLOR_RGB2HSV_FULL);
 Core.inRange(mIntermediateMat, lo, hi, mIntermediateMat2); // green
 contours = new ArrayList<Mat>();
 hierarchy = new Mat();
 Mat dkernal = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE, new Size(10,10), new Point(1,1));
 Imgproc.dilate(mIntermediateMat2, mIntermediateMat2, dkernal);
 Imgproc.findContours(mIntermediateMat2, contours, hierarchy,Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
 
 //mRgba.setTo(bl);
 for( int idx = 0; idx<contours.size();idx++ ){
List<Point> goodpoints = new ArrayList<Point>(5);
        Mat contour =contours.get(idx);
        double contourarea =Imgproc.contourArea(contour);
        Imgproc.drawContours( mRgba, contours, idx, new Scalar ( Math.random()*255,  200,  200 ), -1, 8, hierarchy );
        int num = (int) contour.total(); 
int buff[] = new int[num*2]; // [x1, y1, x2, y2, ...] 
contour.get(0, 0, buff);
                Log.d("testFindContours", "Rows Contour = " + contour.cols()+"ContourBUFF = " + Arrays.toString(buff));
for(int q = 0; q<num*2;q=q+2){
goodpoints.add(new Point(buff[q], buff[q+1]));
}
Core.rectangle(mRgba, Imgproc.boundingRect(goodpoints).br(), Imgproc.boundingRect(goodpoints).tl(), new Scalar(255, 255, 0, 255));
Core.putText(mRgba, "HAND", Imgproc.boundingRect(goodpoints).tl(), Core.FONT_HERSHEY_COMPLEX, 1, new Scalar(255, 200, 0, 255));
 }


--
Thank-you with Regards,
Rushikesh Khatkhede

Rui Marques

unread,
Mar 26, 2012, 6:37:22 PM3/26/12
to android...@googlegroups.com
Your code for max contour was mostly correct, i don't know what so troubling about it:

int maxArea = 0, maxAreaIdx = 0;

for( int idx = 0; idx<contours.size();idx++ ){
   Mat contour = contours.get(idx);
   double contourarea = Imgproc.contourArea(contour);
   if(contourarea>maxArea) {
      maxArea=contourarea;
       maxAreaIdx=idx;
   }
}
//at the end of this for, contours.get(maxAreaIdx) is your biggest contour

About the centroid, like i said before, you should look for threads at yahoo opencv group, they explain it there i can't do all the work for you ok ;)

Rushikesh Khatkhede

unread,
Mar 26, 2012, 8:16:18 PM3/26/12
to android...@googlegroups.com
Dear Rui,
  I am really sorry for troubling you. I know that you have other stuffs also. But please bear with me this time.
  Regarding my snippet for maxarea, You were correct that code was correct and i had also made changes that you pointed out. But the problem is that     
  i am not getting the contour continuously. It appears and then disappears for a moment and then again appears.

Rui Marques

unread,
Mar 27, 2012, 5:32:58 AM3/27/12
to android...@googlegroups.com
Can you post an image of what you are processing.
Maybe it disappears because the algorithm is too sensitive to detect the contour for every frame, or the max contour is not the expected for every frame.


On Tuesday, 27 March 2012 01:16:18 UTC+1, Rushikesh Khatkhede wrote:
Dear Rui,
  I am really sorry for troubling you. I know that you have other stuffs also. But please bear with me this time.
  Regarding my snippet for maxarea, You were correct that code was correct and i had also made changes that you pointed out. But the problem is that     
  i am not getting the contour continuously. It appears and then disappears for a moment and then again appears.
Reply all
Reply to author
Forward
0 new messages