samedi 27 juin 2015

Floating Point Exception C

I keep getting this error while testing the following function. It's supposed to normalize the values of an image that contains pixels of colors 0 to 255. I found the min and max, changed them to 0 and 255, then changed all inbetween values accordingly to accomodate the same previous ratio of difference:

    void normalize( uint8_t array[],
        unsigned int cols,
        unsigned int rows )
{
         uint8_t minValue = 255;
     uint8_t maxValue = 0; 

    for (int i = 0; i < cols*rows ; ++i)
    {
            if(array[i] < minValue) minValue = array[i];
    }



    for (int i = 0; i < cols*rows; ++i)
    {
     if(array[i] < maxValue) maxValue = array[i];
    }

        int difference1 = maxValue - minValue;

    uint8_t ratios[cols*rows];


    for (int i = 0; i < cols*rows ; ++i){
        ratios[i] = 0;
    }

    //find the ratios
    for (int i = 0; i < cols*rows ; ++i){
        ratios[i] = (array[i] / difference1) - 1;
    }

    for (int i = 0; i < cols*rows ; ++i){

        if(array[i] == minValue){
            array[i] = 0;
        }else if(array[i] == maxValue){
            array[i] = 255;
        }else{
            array[i] = round(ratios[i] * 255);
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire