Error in matrix assignment

1 次查看(过去 30 天)
I am attempting to create a MXNXP image stack, where M and N are the vertical and horizontal dimensions, and P is a third dimension that represents the number of images in the stack. For one particular set of images, I keep getting an error message that says "Unable to perform assignment because the size of the left side is 128-by-128 and the size of the right side is 256-by-256." Not sure what this means, but it looks like m and n are getting multiplied by 2.. Do not know why it would be doing this. This same code works for other sets of images. Here is the code:
function num_images = img_2_stack(path)
% path = '/path/to/image/directory'
% create image stack
dir_info = dir(fullfile(path, '*.png'));
num_images = numel(dir_info)
m = 128; % vertical pixels
n = 128; % horizontal pixels
img_stack = zeros(m,n,num_images); % initialize image stack
for i = 1:num_images
fn = dir_info(i).name;
img = imread(fullfile(path, fn));
img_stack(:,:,i) = img;
end
end

采纳的回答

Chunru
Chunru 2022-5-20
The image from the file "img" has a size of 256-by-256. However, you have defined the img_stack with size of 128-by-128-by-#of_images. So that you can not do the assignment operation "img_stack(:,:,i) = img" due to different sizes. You can either change m, n to be 256 (assuming all images have size of 256-by-256) or resize the image to be 128-by-128. "doc imresize" for more info.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by