MATLAB Answers

Convert grayscale CMYK to RGB

10 views (last 30 days)
Rob Campbell
Rob Campbell on 13 Feb 2020
Commented: Rob Campbell on 15 Feb 2020
I have some images of fish scales that are CMYK gray that I would like to convert to RGB. They read in as a m x n x 4 sheet array with imread. They do not have a color profile, using iccread on them returns an error ('No Icc profile found in TIFF file'). The original looks like this:
When I try to convert with the following:
im=imread('example_img.TIF');
C = makecform('cmyk2srgb')
im2=applycform(im,C);
It returns a m x n x 3 sheet array, but when I look at it in imshow (or write to a file) the color is wrong (it is way too dark):
Some of the features are visible, but the background is wonky. I've checked, it's not just that it's inverted (i.e. imcomplement doesn't fix it). I have tried many color profiles (reading in with iccread), like so:
inprof = iccread('BlackWhite.icc');
outprof = iccread('sRGB.icm');
C = makecform('icc',inprof,outprof);
im2=applycform(im,C);
but none result in an appropriate looking image. Does anyone have any ideas? I have attached the example image.

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 13 Feb 2020
I have some images of fish scales that are CMYK gray
Not according to the metadata:
>> imfinfo('example_img.TIF')
ans =
struct with fields:
[..] %fields elided for brevity
ColorType: 'truecolor'
[..]
PhotometricInterpretation: 'RGB'
StripOffsets: [1×4426 double]
SamplesPerPixel: 4
[..]
ExtraSamples: 2
It's a RGB image with 4 samples per pixel which is indeed one too many but ExtraSamples of 2 tells you that the extra channel is (unnassociated) alpha data.
So, you can display your image as is just using the first 3 channels. Optionally, you can use the 4th channel to add some transparency to your image but considering that the transparency is all 255, it's not going to change anything.
img = imread('example_img.TIF');
himg = imshow(img(:, :, 1:3)); %display RGB values
himg.AlphaData = im2double(img(:, :, 4)); %add transparency but every pixel is fully opaque anyway

  1 Comment

Rob Campbell
Rob Campbell on 15 Feb 2020
Aha! Thanks! I did not know that the alpha data might be encoded in there.
Cheers, Rob

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019a