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 次查看(过去 30 天)
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?

采纳的回答

KSSV
KSSV 2022-1-24
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 个评论
David Raveh
David Raveh 2022-1-24
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 2022-1-24
编辑:KSSV 2022-1-24
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.

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by