Error in matrix assignment

2 views (last 30 days)
Anthony Knighton
Anthony Knighton on 19 May 2022
Answered: Chunru on 20 May 2022
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

Accepted Answer

Chunru
Chunru on 20 May 2022
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.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!