Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

use a vector<RotatedRect> instead of your float coords[][]. this will avoid your current indexing / bookkeeping problems :

    void printBox(const RotatedRect & box)
    {
        std::cout << "Mittelpunkt " << box.center << std::endl;
        std::cout << "Winkel " << box.angle << std::endl;
        std::cout << "Größe " << box.size << std::endl;
    }


    // in main() ...
    //    
    vector<RotatedRect> koord;  
    // ...
    for(size_t i = 0; i < contours.size(); i++)
    {
        size_t count = contours[i].size();
        if( count < 6 )
            continue;

        cv::Mat pointsf;
        cv::Mat(contours[i]).convertTo(pointsf, CV_32F);
        cv::RotatedRect box = fitEllipse(pointsf);

        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*1.5 )
            continue;

        // just save the current box:    
        koords.push_back(box);

        printBox(box);

        if(box.size.width > maxWidth)  maxWidth = box.size.width;
        if(box.size.width < minWidth)  minWidth = box.size.width;

        cv::drawContours(cimage, contours, (int)i, cv::Scalar::all(255), 1, 8);
        cv::ellipse(image, box, cv::Scalar(0,0,255), 1, CV_AA);
        cv::ellipse(image, box.center, box.size*0.5f, box.angle, 0, 360, cv::Scalar(0,255,255), 1, CV_AA);

        cv::Point2f vtx[4];
        box.points(vtx);
        for( int j = 0; j < 4; j++ ) 
            cv::line(cimage, vtx[j], vtx[(j+1)%4], cv::Scalar(0,255,0), 1, CV_AA);}
        }
    }

    // later iterate over found boxes:
    for (size_t k = 0; k < koords.size(); k++ )
    {
        std::cout.precision(6);
        std::cout << "------ Objekt "<< k <<" ---------" << std::endl;
        printBox(koord[k]);
    }
    std::cout << "Größte Breite: " << maxWidth << std::endl;
    std::cout << "Kleinste Breite: " << minWidth << std::endl;