Home > Back-end >  Image values with rgb2gray python
Image values with rgb2gray python

Time:02-11

I'm a beginner in image processing.

I work with an RGB image image.shape = (4512,3000,3)

I saw the value of the the first pixel: image[0][0] = [210 213 220]

When I use the rgb2gray function the result is rgb2gray(image[0][0]) = 0.8347733333333334

But I saw that the relation used by the function is Y = 0.2125 * R 0.7454 * G 0.0721 * B. I did the calculation, I should have Y = im[0,0,0] * 0.2125 im[0,0,1] * 0.7154 im[0,0,2] * 0.0721 = 212.8672

It seems my result is 212.8672/255 = 0.8347733333333334

Why is the result between 0 and 1 and not between 0 and 255?

CodePudding user response:

I assume you are using scikit-image's rgb2gray. In that case, you can see in the code from https://github.com/scikit-image/scikit-image/blob/main/skimage/color/colorconv.py that every color conversion in the color module starts with the _prepare_colorarray methods which converts to floating point representation.

def _prepare_colorarray(arr, force_copy=False, *, channel_axis=-1):
    """Check the shape of the array and convert it to
    floating point representation.
    """
    arr = np.asanyarray(arr)

    if arr.shape[channel_axis] != 3:
        msg = (f'the input array must have size 3 along `channel_axis`, '
               f'got {arr.shape}')
        raise ValueError(msg)

    float_dtype = _supported_float_type(arr.dtype)
    if float_dtype == np.float32:
        _func = dtype.img_as_float32
    else:
        _func = dtype.img_as_float64
return _func(arr, force_copy=force_copy)

The module does (thankfully) support 8-bit int representation as an input, but converts the image array to float representation and uses that representation all along.

  • Related