Main Content

raw2rgb

Transform Color Filter Array (CFA) image in RAW file into RGB image

    Description

    example

    rgbimage = raw2rgb(filename) transforms the CFA image in the RAW file specified by filename into an RGB image.

    rgbimage = raw2rgb(filename,Name,Value) specifies additional options with name-value pair arguments.

    Examples

    collapse all

    Convert the Color Filter Array (CFA) image in the file into a 16-bit RGB image in the sRGB colorspace.

    rgb = raw2rgb("colorCheckerTestImage.NEF");
    imshow(rgb)

    Figure contains an axes. The axes contains an object of type image.

    Convert the CFA image into a 8-bit RGB image in the Adobe RGB 1998 colorspace.

    rgb = raw2rgb("colorCheckerTestImage.NEF", "BitsPerSample", 8, "ColorSpace", "adobe-rgb-1998");
    imshow(rgb)

    Figure contains an axes. The axes contains an object of type image.

    Convert the CFA image into a 16-bit image, in the camera's native colorspace, white-balanced against a D65 illuminant.

    rgb = raw2rgb("colorCheckerTestImage.NEF", "ColorSpace", "camera", "WhiteBalanceMultipliers", "D65");
    imshow(rgb)

    Figure contains an axes. The axes contains an object of type image.

    Input Arguments

    collapse all

    Name of RAW file, specified as a string scalar or char vector. Specify filename as a full path, containing the file name and extension, or as a relative path from the current folder or from any folder on the MATLAB path.

    Data Types: char | string

    Name-Value Pair Arguments

    Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

    Example: rgb = raw2rgb("colorCheckerTestImage.NEF", "BitsPerSample",8,"ColorSpace","adobe-rgb-1998");

    Apply contrast stretching when rendering the RGB image, specified as a logical 0 (false) or 1 (true).

    Data Types: logical

    Bits per sample of the output RGB image, specified as the numeric scalar 8 or 16.

    Color space of the output RGB values, specified as 'srgb','camera', or 'adobe-rgb-1998'. For more information about the 'srgb' and 'adobe-rgb-1998' color spaces, see Understanding Color Spaces and Color Space Conversion. The 'camera' color space is native to the device.

    Data Types: char | string

    White balance adjustment for rendering the RGB image, specified as one of the strings in this list, or as a 1-by-N vector of class double.

    ValueDescription
    'AsTaken'White balance multipliers used by the camera to capture the image
    'D65'White balance multipliers required to balance image using the D65 illuminant
    'ComputeFromImage'White balance multipliers determined by analyzing the CFA image
    1-by-N vectorCustom white balance multipliers specified as a 1-by-N vector of class single or double. For Bayer sensor images, N must be 4 and the order of the coefficients should match the CFALayout field reported by rawinfo. For non-Bayer sensors, N should match the SamplesPerPixel field reported by rawinfo.

    Data Types: double | char | string

    Output Arguments

    collapse all

    RGB image, returned as an M-by-N-by-3 numeric array. The values of M and N correspond to the first and second elements of the RenderedImageSize field reported by rawinfo, respectively.

    rgbimage can be either uint8 or uint16, depending on the value of the BitsPerSample name-value pair.

    Limitations

    • The raw2rgb function does not support RAW file formats that employ JPEG compression.

    Tips

    • The function uses LibRaw version 0.20.0 for reading the CFA image data.

    Introduced in R2021a