Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Save box parameter in an array

Hej, I want to classify an image with circle objects. Before classification I use contourising and fitellipse. For the classification I want to save the box parameters (size, angle, center) in an array. But when I test with std::cout the saved data, I get different values. I don't know what I'm doing wrong. Anybody have an idea? Thanks in advance!

cv::Mat image = cv::imread(argv[1],1);
if (!image.data)
    return 0;


cv::Mat grayImage;
cv::cvtColor( image, grayImage, CV_BGR2GRAY );
cv::blur( grayImage, grayImage, cv::Size(3,3) );

cv::vector<cv::vector<cv::Point> > contours;
cv::Mat bimage = grayImage >= 40;

findContours(bimage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
cv::Mat cimage = cv::Mat::zeros(bimage.size(), CV_8UC3);

int countEllipses = 8;
float koord[countEllipses][5];  
double maxWidth;
double minWidth;


    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;

    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);

    float x = box.center.x;
    float y = box.center.y;
    float height = box.size.height;
    float width = box.size.width;

    std::cout << "Mittelpunkt " << box.center << std::endl;
    std::cout << "Winkel " << box.angle << std::endl;
    std::cout << "Größe " << box.size << std::endl;
    std::cout << "Anzahl " << i << std::endl;
    std::cout << "Test Breite " << box.size.width << std::endl;
    std::cout << "Test Höhe " << box.size.height << std::endl;
    countEllipses = i;

    koord[i][0] = x;
    koord[i][1] = y;
    koord[i][2] = (float)box.angle;
    koord[i][4] = height;
    koord[i][5] = width;

    if(koord[i][5] > maxWidth){
        maxWidth = koord[i][5];
            }
    if(box.size.width < minWidth){
               minWidth = box.size.width;
    }


    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);}

}

for (int k = 0; k < countEllipses; k++ )
{
    std::cout.precision(6);
    std::cout << "------ Objekt "<< k <<" ---------" << std::endl;
    std::cout << "Mittelpunkt x: " << koord[k][0] << std::endl;
    std::cout << "Mittelpunkt y: " <<koord[k][1] << std::endl;
    std::cout << "Winkel: " << koord[k][2] << std::endl;
    std::cout << "Box Höhe: " << koord[k][3] << std::endl;
    std::cout << "Box Breite: " <<koord[k][4] << std::endl;
}
std::cout << "Größte Breite: " << maxWidth << std::endl;
std::cout << "Kleinste Breite: " << minWidth << std::endl;