Trying to stack an image array, error "Unable to perform assignment because the size of the left side is 220-by-220 and the size of the right side is 256-by-256"

1 view (last 30 days)
I recieved a code in a matlab file that went as follows:
input_dir = './data/';
imgs = dir([input_dir '*.png']);
for i = 1:length(imgs)
temp = imread([input_dir imgs(i).name]);
stack(:,:,i) = double(temp);
end
I get this error: "Index in position 3 exceeds array bounds. Index must not exceed 1."
I tried to simplify the code to see if I could limit the issues I had:
image_unknown = imread('.\Images\Greyscale\data_0001.png');
for i = 1:length(image_unknown)
stack(:,:,i) = double(image_unknown);
end
I got this error: "Unable to perform assignment because the size of the left side is 220-by-220 and the size of the right side is 256-by-256".
The image is 256X256 pixels, but I have no idea why it says 220X220. I tried reading about what the stack function does, but it wasn't clear to me. Could someone explain what is happening here?

Accepted Answer

KSSV
KSSV on 24 Jan 2022
If you RHS image is 3D and if the dimensions of each image are different, then you cannot save them into a matrix. So save them into a cell as shown below.
input_dir = './data/';
imgs = dir([input_dir '*.png']);
N = length(imgs) ;
stack = cell(N,1) ;
for i = 1:length(imgs)
temp = imread([input_dir imgs(i).name]);
stack{i} = double(temp);
end
You can access the cell array using stack{1}. stack{2}...stack{n}.
  2 Comments
David Raveh
David Raveh on 24 Jan 2022
I was hoping I could get an explanation for what stack(:,:,i) means. Later in my code, I run this:
stack = stack(:,:,id_1:id_2);
where id_1 = 1 and id_2 = 99. I get this error.
Index in position 3 exceeds array bounds. Index must not exceed 1.
Error in registration_main (line 43)
stack = stack(:,:,id_1:id_2);
Could you explain what exactly I am doing when I run the code? Thanks.
KSSV
KSSV on 24 Jan 2022
Edited: KSSV on 24 Jan 2022
That means you are trying to extract the elements which are not present in the array. So it seems, your stack is only a 2D matrix, not a 3D matrix.
EXample:
A = rand(2,2,5) ; % a 2x2x5 matrix for demo
A(:,:,1:2) ; % no error
A(:,:,2:6) ; % error as there is no 6th matrix, it has only 5 2D arrays
Index in position 3 exceeds array bounds. Index must not exceed 5.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!