How to extract RGB matrices from stacked TIFF file?

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

 Risposta accettata

Walter Roberson
Walter Roberson il 13 Ago 2020
Modificato: Walter Roberson il 13 Ago 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 Commenti

iontrap
iontrap il 14 Ago 2020
Modificato: iontrap il 14 Ago 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?
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.
Much appreciated, Walter.

Accedi per commentare.

Più risposte (1)

hosein Javan
hosein Javan il 13 Ago 2020
Modificato: hosein Javan il 13 Ago 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 Commenti

A = imread('My TIFF.tiff');
im = cell(1,4);
for i = 1:4
im{i} = A(:,:,:,i) % im{i} = individual image number(i)
end
I am getting the error "Index in position 4 exceeds array bounds (must not exceed 1)" - regarding line 4.
A = imread('My TIFF.tiff');
ndims(a) % check if it is 4
N = size(a,4) % number of frames
im = cell(1,N);
for i = 1:N
im{i} = A(:,:,:,i) % im{i} = individual image number(i)
end
a has not been defined.
Thanks Hosein. Walter's answer above worked well.
you're welcome, good luck.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by