I have an image_data
cell array contains 400 images:
image_data = {img1, img2, ... img400}
each image, such as img1
, is a 128 x 128
array of pixel values:
img1 = [0.2, 0.5, .....]
I want to get all the i_th pixel of all images stored in image_data
, but not using a for-loop.
For Example:
image_data = {img1, img2, img3}
img1 = [0.1, 0.2, 0.3]
img2 = [0.4, 0.5, 0.6]
img3 = [0.7, 0.8, 0.9]
I want to calculate the variance the following quickly:
var([img1(1),img2(1),img3(1)]) = var([0.1,0.4,0.7])
var([img1(2),img2(2),img3(2)]) = var([0.2,0.5,0.8])
var([img1(3),img2(3),img3(3)]) = var([0.3,0.6,0.9])
Try to avoid using a loop, because it is slow.
The first step is to convert that unwieldy cell array into a 3-dimensional array by concatenating the images along the 3rd dimension:
image_array = cat(3, image_data{:});
Now, all we have to do is use a subscript to get the vector of the ith pixel from every image into a vector and pass that to var
:
pixel_var = var(image_array(1,1,:)); % var for pixel 1
Or just do the whole image at once and index into the result:
image_var = var(image_array, 0, 3); % take var along 3rd dimension