colour-science / colour-demosaicing

CFA (Colour Filter Array) demosaicing algorithms for Python
https://www.colour-science.org
BSD 3-Clause "New" or "Revised" License
285 stars 58 forks source link

How to make demosaicing function return RGB values in [0,1] range? #19

Closed Jiazheng411 closed 3 years ago

Jiazheng411 commented 3 years ago

Hi, @KelSolaar , thanks a lot for the lib. \ I noticed that the demosaicing function does not return the output in [0,1] range, how can I make it in [0,1] range?

def demosaicing_CFA_Bayer_bilinear(CFA, pattern='RGGB'):
Notes
    -----
    -   The definition output is not clipped in range [0, 1] : this allows for
        direct HDRI / radiance image generation on *Bayer* CFA data and post
        demosaicing of the high dynamic range data as showcased in this
        `Jupyter Notebook <https://github.com/colour-science/colour-hdri/\
blob/develop/colour_hdri/examples/\
examples_merge_from_raw_files_with_post_demosaicing.ipynb>`__.
sobotka commented 3 years ago

You will want to find well saturation maximums and minimum for each photosite plane via numpy.amax and then normalize. (photosite - photosite_min) / (photosite_max - photosite_min) should work to normalize to 1.0, for example.

Jiazheng411 commented 3 years ago

Thanks a lot. \ By the way, I found the images showed using colour.plotting.plot_image(colour.cctf_decoding(IMG)) is darker than the original image.\ for example, this is the original image,\ image

after I read the image using img = colour.read_image(img_path)\ this is what colour.plotting.plot_image(colour.cctf_decoding(img)) looks like image And I do a mosaicing followed by demosaicing, the image looks much lighter,\

CFA_BGGR = mosaicing_CFA_Bayer(img, 'BGGR')
reinter_CFA_BGGR = demosaicing_CFA_Bayer_bilinear(CFA_BGGR, 'BGGR')
colour.plotting.plot_image(colour.cctf_encoding(reinter_CFA_BGGR))

image is this expected? Or what can I do to eliminate this effects?

sobotka commented 3 years ago

Most sensor data is linearly encoded. That is, it is not encoded for display. The subject of preparing colour data for display is likely a wholly other matter.

The linear data then would require some sort of nonlinear curvature to scale and prepare the data for output.

The most simple transform would be to use the display’s inverse EOTF to encode the sensor-linear values for display linear output. For example, using the sRGB inverse EOTF, or more likely, a generic inversion of a 2.2 power function, would prepare linear encoded colour data for projection out of a commodity sRGB-like display.

What you are likely to discover after that unfolds further down the rabbit hole...