필터 지우기
필터 지우기

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