0
votes

I have 3 480 row x 640 column images. Each pixel holds an intensity value.

I read them in matlab with this:

   object_intensities = cell(1,6);
   for pat=1:3
       file1 = sprintf('image%d.pgm',pat-1);
       dummy = double(imread(file1));
       object_intensities{pat} = dummy;
   end

Now for pat = 1 to 3, object_intensities is a cell array such that each cell element of object_intensities{pat} holds each image in a 480x640 matrix array.

Now what I need is a single structure that is 480 row x 640 column such that each cell element contains the 3 intensities of each image pixel in a single vector like this:

structure{row 1, column 1} = [image1_intensity @ row1 column1 image2_intensity @ row1 column1 image3_intensity @ row1 column1]
structure{row 1, column2} = [image1_intensity @ row1 column2 image2_intensity @ row1 column2 image3_intensity @ row1 column2]
.
.
structure{row 480, column 640} = [image1_intensity @ row480 column640 image2_intensity @ row480 column640 image3_intensity @ row480 column640]

Any way to do this without using loops but through vectorization?

1
Are the three images intensities for RGB channels? or does each cell of object_intensities actually hold a 480-by-640-by-3 matrix? If it does actually hold a single channel image (which I doubt given your use of imread, you might need to convert it to grey scale first) then try cat(3,object_intensities{:}) and you'll get a nice 480-by-640-by-3 matrix. But then I guess you could have just done that when you are reading the images in,,, basically, why would you want the data structure you say you want? It's going to be really hard to work with compared to a regular matrix. - Dan
The images are already grayscale. I would like it in this data structure I described as I need the intensity sequence of each image pixel. - user1431515
A 3D matrix will still be better, cell arrays are much harder to work with. Also, I'm pretty sure imread will make your image RGB by default irrespective of if it's greyscale or not. You will still need to do the conversion. - Dan

1 Answers

1
votes

The data structure you want is asking for trouble. Rather use a 3D matrix where the third dimension maps your intensities (in order). It will be much easier to work with down the line.

I suggest you create it like this:

object_intensities = zeros(480,640,3);
for pat=1:3
    file1 = sprintf('image%d.pgm',pat-1);
    object_intensities(:,:,pat) = rgb2gray(imread(file1));
end

Now what would have been object_intensities{i,j} on your structure is just object_intensities(i,j,:) and if you really need to you can call squeeze or permute or (:)' to make it a row vector. But just think how much easier it is now to do something like finding the average intensity per pixel

mean(object_intensities,3)

compared with the cell array

cellfun(@mean,object_intensities,'uni',0)  %// Just a loop in disguise

And what if you wanted the average per row

mean(object_intensities,2)

Try do this for the cell array and you'll see why you want to avoid it.

But if you really really want your structure, then use my code as above and call mat2cell on the 3D matrix:

mat2cell(object_intensities, ones(size(object_intensities,1),1), ones(1,size(object_intensities,2)),3)