gamma correction in opencv hog.cpp

2019-08-31 00:00发布

问题:

I dont understand the code of the gamma correction in hog.cpp in opencv, i went through some links here which doesnt match with the code in opencv hog.cpp

Mat_<float> _lut(1, 256);

const float* lut = &_lut(0,0);

if( gammaCorrection )
    for( i = 0; i < 256; i++ )
        _lut(0,i) = std::sqrt((float)i);
else
    for( i = 0; i < 256; i++ )
        _lut(0,i) = (float)i;

All i understood from the code is it creates 2 dimensional array of 1x256, if gamma correction is true it will calculate the square root of data.I tried to debug going through all the files related to this code but dint understood. Can anyone briefly tell whats happening here.

Please help
Thanks in advance.

回答1:

All you're doing there is building a lookup table; you know the incoming data is chars, so all pixels can only have values from 0-255, so if you're doing gamma correction you precalculate the square-root. This gets used later on, inside the per-pixel gradient calculation. Later on in computeGradients(), you get the row pointer:

const uchar* imgPtr  = img.data + img.step*ymap[y];

Then index into the lookup table to get the pixel values for the [ -1 0 1 ] gradients:

        for( x = 0; x < width; x++ )
        {
            int x1 = xmap[x];

            dbuf[x] = (float)(lut[imgPtr[xmap[x+1]]] - lut[imgPtr[xmap[x-1]]]);
            dbuf[width + x] = (float)(lut[nextPtr[x1]] - lut[prevPtr[x1]]);
        }