Why does the size of third dimension of a 3D array change?

15 views (last 30 days)
I have an image with dimension 124x1422x3 which needs to be binarized. When i access it with a simple imread() function and see the dimension and size of the third dimension, it shows 3 in both cases which is true. But when i try to access it with imread() function again but this time to binarize it with the im2bw() function, it throws an error saying "Error using im2bw>parse_inputs (line 97) IM2BW: Truecolor RGB image has to be an M-by-N-by-3 array." When i checked the dimension of the array it shows 124x1422x4 and i am clueless as to why the size of 3rd dimension is changing when all i am doing is the same except for sending the image for binarization.Any help is appreciated. Thanks in advance.
  9 Comments
Walter Roberson
Walter Roberson on 28 Apr 2018
If the .tif is the original and you are converting it to jpg and the tif is x4 and the jpg is x3, then that would suggest that the issue is with the original tif. I would need to have a look at the original tif.
deena hijam
deena hijam on 28 Apr 2018
Since .tif format is not supported, i am sending a .zip file

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Apr 2018
Okay, so there are two ways you can handle these:
1) make some assumptions that are probably valid:
im0027 = imread('0027.tif');
if size(im0027,3) == 4; im0027 = im0027(:,:,3); end
2) don't make assumptions
The MATLAB documentation tends to imply that USSheetfedCoated.icc should be available without that, but if you read it carefully really you have to download it, and the Mathworks documentation gives the wrong location for it.
Then:
filename = '0027.tif';
info = imfinfo(filename);
if isfield(info, 'PhotometricInterpretation') && strcmpi(info.PhotometricInterpretation, 'CMYK')
outprof = iccread('sRGB.icm');
inprof = iccread('USSheetfedCoated.icc');
C = makecform('icc',inprof,outprof);
im0027 = applycform(imread(filename), C); %CMYK input
im0027_alpha = [];
elseif isfield(info, 'ExtraSamples') && info.ExtraSamples ~= 0 %1 -- associated alpha; 2 -- unassociated alpha
[im0027, im0027_alpha] = readRGBAImage(Tiff(filename, 'r'));
elseif isfield(info, 'BitsPerSample')
chans = length(info.BitsPerSample);
if chans == 1
[im0027, im0027_map] = imread(filename); %grayscale or pseudocolor
if ~isempty(im0027_map); im0027 = ind2rgb(im0027, im0027_map); end %pseudocolor
im0027_alpha = [];
elseif chans == 2
error('file "%s" has two channels, which is probably a broken implementation of a binary image with an alpha channel. Giving up on it.', filename);
elseif chans == 3
im0027 = imread(filename); %rgb
im0027_alpha = [];
elseif chans == 4
warning('file "%s" has four channels but is not CMYK or correct RGBA, using first three', filename);
im0027 = imread(filename); %rgb+something
im0027 = im0027(:,:,1:3);
elseif chans == 5
warning('file "%s" has five channels but is not CMYK or correct RGBA, using first three even though it is probably hyperspectral', filename);
im0027 = imread(filename); %rgb+something+something
im0027 = im0027(:,:,1:3);
else
error('file "%s" unexpectedly has %d channels, must be hyperspectral, giving up on it', filename, chans);
end
else %hmmm, what _is_ it anyhow?
[im0027, im0027_map] = imread(filename); %unmarked RGBm, or grayscale or pseudocolor
if ~isempty(im0027_map); im0027 = ind2rgb(im0027, im0027_map); end %pseudocolor
im0027_alpha = [];
if ndims(im0027) > 3 || ~ismember(size(im0027,3), [1, 3])
error('file "%s" does not have expected headers describing it, and is not grayscale or RGB, giving up on it', filename);
end
end
The above covers RGB, RGBA with proper headers, CMYK, RGBA without proper headers (under protest), grayscale, pseudocolor, RGB plus two hyperspectral channels (under protest)
  4 Comments
Walter Roberson
Walter Roberson on 28 Apr 2018
The easiest way to convert grayscale to RGB is to repmat(TheArray, [1 1 3])

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!