1

I've imported a set of images in MATLAB, and I've converted them to grayscale. I must now create an image stack, "a 3D matrix of grayscale images". After this, I must create a 1D image intensity array by taking the "double mean of each layer of the image stack". Here is my code thus far (I import just a few images):

for i=139:141

string2 = num2str(i);

% Concatenate several strings, produce file name
str = [string, string2, string3];

% Read image
a = imread(str);

% Get image dimensions
size(a)

% Convert to grayscale
b = rgb2gray(a);

'size(a)' yields '1728 x 2592 x 3'. This is true for all images. I'm wondering how I can create the 3D matrix of grayscale images, and I'm wondering how I can create the 1D image array mentioned above. I'm assuming, perhaps incorrectly, that "double mean" means

mean(mean(...)).

For the 3D matrix, I have

% Pre-allocate 3D matrix
ImStack = zeros(1728, 2592, 3, class(b));

% Add images to ImStack
ImStack(:,:,1) = b;

This follows a template I found on the MathWorks help forum,

b= zeros(2000,2000,number_of_images,class(a));

b(:,:,1) = a;

However, I am unsure how to proceed with creating the 1D image intensity array. Your advice would be greatly appreciated. Thank you.

4

1 回答 1

5

您的代码大部分都在那里。但是,这一行有一个问题:

ImStack(:,:,1) = b;

这会将每个图像放置在图像堆栈的第一个平面中,并且它将覆盖同一位置的最后一个。您需要为每个图像使用不同的索引,如下所示:

ImStack(:,:,i-138) = b;  % subtract 138 because i starts at 139 in your code

完成后,您可以通过沿第三维平均轻松找到平均值:

ImMean = mean(ImStack,3);

另一个注意事项:如果您有太多图像,创建一个同时保存所有图像的堆栈可能会导致您的内存不足。得出平均值的另一种方法是将每个图像添加到运行总和中,最后除以图像总数。

于 2013-06-05T02:50:19.907 回答