ROS中的OpenCV:boundingRect wierd输出

时间:2016-01-26 04:05:59

标签: c++ opencv ros

我对OpenCv函数boundingRect有一些问题,我不确定是我的数据类型问题还是其他任何问题。

它给了我一些无意义的输出。

Iitialization: 
int x=0;
int z=0;
int nonzero=0;
vector<Rect> Real_Rect;
vector<float> real_mu_W;
vector<float> real_mu_H;
vector<Rect> boundRect;

void drawBox(Mat& image, CvRect box, Scalar color, int thick){
    rectangle(image, cvPoint(box.x, box.y), cvPoint(box.x + box.width, box.y + box.height), color, thick);
}

void Find_Contours(Mat ctemp){

    cv::Mat temp;
    ctemp.copyTo(temp);

    RNG rng(12345);
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Find contours
    findContours( ctemp, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

    nonzero=0;
    x=0;
    z=0;

    /// Get the moments
    vector<Moments> mu(contours.size() );

    //vector<vector<Point> > contours_poly( contours.size() );

    for( int i = 0; i < contours.size(); i++ ){ 
        if(contourArea(contours[i]) >= 100.0){
            mu[i] = moments( contours[i], false ); 
            //approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
            boundRect.push_back(boundingRect( Mat(contours[i]) ));

            nonzero++;

        }else{

        }
     }

     for( z = 0; z < nonzero ; z++ ){ 

         if(mu[x].m00 >= 100.00){

            Real_Rect.push_back(boundRect[x]);
            real_mu_W.push_back(boundRect[x].width);
            real_mu_H.push_back(boundRect[x].height);

         }
         if(mu[x].m00 < 100.0){

            z--;

         }

         x++;

     }

    for( int i = 0; i< nonzero; i++ ){

        cout << "realRect. X = " << Real_Rect[i].x << "realRect. Y = " << Real_Rect[i].y << endl; 
        cout << "realRect. W = " << Real_Rect[i].width << "realRect. H = " << Real_Rect[i].height << endl; 

// Because of the weird output, I have to make some filter.

        if(Real_Rect[i].x >=0 && Real_Rect[i].y >=0 && Real_Rect[i].width >0 && Real_Rect[i].height >0 
           && (Real_Rect[i].x+Real_Rect[i].width) <= 320 && (Real_Rect[i].y + Real_Rect[i].height) <= 240){

            drawBox(image, Real_Rect[i], 2, 2);

        }
     }

    cout << "nonzero = " << nonzero << endl;

}

这是输出,你可以看到一些奇怪的x和w。 有时它甚至给了我消极的,但那段时间我没有抓住它。

enter image description here

希望有人可以帮助我,谢谢。

0 个答案:

没有答案