Vertcat error when concatenating images in loop

조회 수: 1 (최근 30일)
Siegmund Nuyts
Siegmund Nuyts 2022년 9월 29일
댓글: Jan 2022년 9월 30일
I am reading around 1000 images showing progress over time. I want to concatenate them to show daily average.
However, the vertcat shows error "Dimensions of arrays being concatenated are not consistent." after 126 images.
All images are same size (1680 x 965) so I'm not really sure why the erros occurs after 126 images.
Anyone an idea why and how to resolve it?
Thanks in advance!
Img = dir([path,strcat('**/S_1_*')]);
t = [];
Sav = [];
for i = 1:length(Img)
t = [t; datenum(Img(i).name(5:end-4),'yyyymmddHHMM')];
Sro=imread(Img(i).name);
Sav = [Sav; mean(vertcat(Sro),1,'omitnan')];
end

답변 (1개)

Jan
Jan 2022년 9월 29일
편집: Jan 2022년 9월 29일
vertcat(Sro) concatenates Sro with nothing. What is the purpose of this command?
Ask Matlab, what the problem is:
for i = 1:length(Img)
t = [t; datenum(Img(i).name(5:end-4),'yyyymmddHHMM')];
Sro = imread(Img(i).name);
try
Sav = [Sav; mean(Sro, 1, 'omitnan')];
catch ME
size(Sav)
size(mean(Sro, 1, 'omitnan'))
error(ME.message)
end
end
By the way, a pre-allocation is more efficient thanletting an array grow iteratively:
Sav = zeros(numel(Img), 965);
for i = 1:numel(Img)
t = [t; datenum(Img(i).name(5:end-4),'yyyymmddHHMM')];
Sro = imread(Img(i).name);
try
Sav(i, :) = mean(Sro, 1, 'omitnan');
catch ME
size(Sav)
size(mean(Sro, 1, 'omitnan'))
error(ME.message)
end
end
  댓글 수: 2
Siegmund Nuyts
Siegmund Nuyts 2022년 9월 30일
Thanks for the suggestion.
The error gives: "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
The pictures show change over time so concatenating them will show "average" change.
The x-axis will then be "time" and y-axis distance.
The outcome is exactly what I need. I just don't understand why/how it gives an error after a certain amount of pictures...?
Jan
Jan 2022년 9월 30일
@Siegmund Nuyts: Please post the complete output of the error message: In which line does Matlab stop? Which sizes are displayed? Did you understand, why the sizes of the arrays are displayed before the error message? This information will show you, why the sizes are not matching. Maybe some of the images are RGB images (3D) and others in gray scale (2D)?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by