Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why my array had different data?

I'm debugging my code to get the largest and 2nd largest contour area using this code, but my output is very weird for the arrayarea. I put the data into the array which i had declared as float. The output as under the code. Which part i got it wrong?

float arrayarea[5];

// iterate through each contour.
for( int i = 0; i< contours.size(); i++ )
{
    //  Find the area of contour
    double a=contourArea( contours[i],false); 
    arrayarea[i]=a; 
    cout<<" \narea="<<a<<endl; 

    if(a>largest_area)

    {
        largest_area=a;
        // Store the index of largest contour
        largest_contour_index=i;               

        // Find the bounding rectangle for biggest contour
        //bounding_rect=boundingRect(contours[i]);
    }

}

cout<<" \nlargest_area="<<largest_area<<endl;
cerr << "\nlargest_index=" <<largest_contour_index  << endl;
std::cout << "\narray_area= " <<arrayarea<< std::endl;
waitKey(0);

first = second = INT_MIN;
for (i = 0; i < arrayarea[i] ; i ++)
{
    /* If current element is smaller than first
       then update both first and second */
    if (arrayarea[i] > first)
    {
        second = first;
        first = arrayarea[i];
    }

    /* If arr[i] is in between first and 
       second then update second  */
    else if (arrayarea[i] > second && arrayarea[i] != first)
        second = arrayarea[i];
        second_largest_contour_index=i; 


}
if (second == INT_MIN)
    printf("There is no second largest element\n");
else
    printf("The second largest element is %dn", second);
    printf("The second largest index is %dn", second_largest_contour_index);


waitKey(0);

Result [259.084, 55.9251] point [143.648, 32.9001] point area=2512.5 area=1346.5 largest_area=2512 largest_index=0 array_area= 00007FF7D7C12A88 The second largest element is 1346 The second largest index is 2

click to hide/show revision 2
None

Why my array had different data?

I'm debugging my code to get the largest and 2nd largest contour area using this code, but my output is very weird for the arrayarea. I put the data into the array which i had declared as float. The output as under the code. Which part i got it wrong?

float arrayarea[5];

// iterate through each contour.
for( int i = 0; i< contours.size(); i++ )
{
    //  Find the area of contour
    double a=contourArea( contours[i],false); 
    arrayarea[i]=a; 
    cout<<" \narea="<<a<<endl; 

    if(a>largest_area)

    {
        largest_area=a;
        // Store the index of largest contour
        largest_contour_index=i;               

        // Find the bounding rectangle for biggest contour
        //bounding_rect=boundingRect(contours[i]);
    }

}

cout<<" \nlargest_area="<<largest_area<<endl;
cerr << "\nlargest_index=" <<largest_contour_index  << endl;
std::cout << "\narray_area= " <<arrayarea<< std::endl;
waitKey(0);

first = second = INT_MIN;
for (i = 0; i < arrayarea[i] ; i ++)
{
    /* If current element is smaller than first
       then update both first and second */
    if (arrayarea[i] > first)
    {
        second = first;
        first = arrayarea[i];
    }

    /* If arr[i] is in between first and 
       second then update second  */
    else if (arrayarea[i] > second && arrayarea[i] != first)
        second = arrayarea[i];
        second_largest_contour_index=i; 


}
if (second == INT_MIN)
    printf("There is no second largest element\n");
else
    printf("The second largest element is %dn", second);
    printf("The second largest index is %dn", second_largest_contour_index);


waitKey(0);

Result Result:

[259.084, 55.9251] point
[143.648, 32.9001] point
area=2512.5
area=1346.5
largest_area=2512
largest_index=0
array_area= 00007FF7D7C12A88 **array_area= 00007FF7D7C12A88**
The second largest element is 1346
The second largest index is 2

2