MATLAB Answers

How to extract RGB matrices from stacked TIFF file?

6 views (last 30 days)
iontrap
iontrap on 13 Aug 2020
Commented: hosein Javan on 14 Aug 2020
I have a (512x512x3xN) image array where 3 corresponds to the R,G,B channels, and N corresponds to the number of images. How can I extract the R,G,B channels from each image?
I will eventually want to compute mean and stdev for each R,G,B channel of each of the N images and put these into a txt file.
Thanks

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Aug 2020
Edited: Walter Roberson on 13 Aug 2020
squeeze(mean(YourArray, [1 2]))
squeeze(std(YourArray, [], [1 2]))
These should give you a 3 x N array of values that are the means or std over each color plane in each image . There is no need to extract each color plane individually.
If your MATLAB is too old to support multiple dimensions, then you can always do
squeeze( mean(reshape(YourArray, [], 3, size(YourArray,4))) )
squeeze( std(reshape(YourArray, [], 3, size(YourArray,4))) )
This reshapes each color plane of each image into a single column and processes each column.

  3 Comments

iontrap
iontrap on 14 Aug 2020
ans =
78.5000 64.7500 106.2500
ans =
36.5194 12.5797 6.6521
>>
Here is my output for an example tiff file. This seems to be the R,G,B measurements from only one of the N images. I would like to get a matrix with these measurements for all N images. How can I do this?
Walter Roberson
Walter Roberson on 14 Aug 2020
N = 5;
YourArray = randi([0 255], 512, 512, 3, N, 'uint8');
squeeze(mean(YourArray, [1 2]))
squeeze(std(double(YourArray), [], [1 2]))
ans =
127.132061004639 127.566165924072 127.579082489014 127.507698059082 127.727783203125
127.585601806641 127.300945281982 127.553031921387 127.218353271484 127.638935089111
127.607196807861 127.465267181396 127.508388519287 127.246562957764 127.548515319824
ans =
73.8924076662466 73.8391486006418 73.8645348408521 73.9474347695904 73.833758311485
73.9576801440049 73.9752296400623 73.9673059233171 73.8467401920338 73.8984297781487
73.8960669386365 73.9675846490088 73.7850595136846 73.8375167219068 73.986046349694
So that is 3 x 5 output arrays when there were 5 panes.
... In other words the code already does what you are asking for.

Sign in to comment.

More Answers (1)

hosein Javan
hosein Javan on 13 Aug 2020
Edited: hosein Javan on 13 Aug 2020
not sure.
% A = your matrix after reading the image file
im = cell(1,n);
for i = 1:N
im{i} = A(:,:,:,i) % im{i} = individual image number(i)
end

  6 Comments

Show 3 older comments
iontrap
iontrap on 14 Aug 2020
Thanks Hosein. Walter's answer above worked well.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!