Rectangle points and area

asked 2013-02-14 09:45:57 -0500

Tomazi gravatar image

updated 2013-02-16 11:38:15 -0500

Hi peeps

I am trying to find points and compute the area of my bounding boxes that i draw around my ROI in a each frame of a video sequence.

It seems to work it compiles and my program cout's some numbers but they do not seem to be right to me i.e. smaller boxes have larger area than bigger ones.

Here is the code that i have written so far for this functionality:

vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;
        findContours(mBlur, contours, hierarchy, CV_RETR_EXTERNAL,  CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
        //----------------------------------------------------------------------------->

        vector<vector<Point> > contours_poly( contours.size() );
        vector<Rect> boundRect( contours.size() );
         vector<Point> boundingBoxPoint(contours.size());
        //----------------------------------------------------------------------------->
      for( int i = 0; i < contours.size(); i++ )
         { 
           approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
           boundRect[i] = boundingRect( Mat(contours_poly[i]) );

         }

        /// Draw polygonal contour + bonding rects
       Mat drawing = Mat::zeros( range_out.size(), CV_8UC3 );

       if(contours.size() < 100){
        for( int i = 0; i< contours.size(); i++ )
            {

        boundingBoxPoint.push_back(Point2f(0, 0));
        boundingBoxPoint.push_back(Point2f(10, 0));
        boundingBoxPoint.push_back(Point2f(10, 10));
        boundingBoxPoint.push_back(Point2f(0, 10));

        double area0 = contourArea(boundingBoxPoint);
        vector<Point> approx;
        approxPolyDP(boundingBoxPoint, approx, 5, true);
        double area1 = contourArea(approx);

        cout << "area0 =" << area0 << endl <<
            "area1 =" << area1 << endl <<
            "approx poly vertices" << approx.size() << endl;

             Scalar color = Scalar(255,0,255);
           drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
           rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );


         }

      }



        imshow(window_Input, frame);
        imshow(window_Output, drawing);
        if(waitKey(30) >= 0) break;

Could someone point out where am going wrong or suggest a solution perhaps provide some web site where i can find my answers

Regards

edit retag flag offensive close merge delete