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 Center および File ExchangeResizing 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