How to vectorize this function

3 ビュー (過去 30 日間)
Faizan Tahir
Faizan Tahir 2016 年 2 月 17 日
コメント済み: Andrei Bobrov 2016 年 2 月 18 日
Hi.
I have a series of images all with the same background but varying foregrounds. I'm looping through all these images and taking median pixel values to calculate the background so I can perform background subtraction. It works well but the code is slow because it contains a double for loop. How can I vectorize the following code?
for x = 1:480
for y = 1:640
median_bg_RedValues(x,y) = median(bg_RedValues(x,y,1:actualNumOfImages));
median_bg_GreenValues(x,y) = median(bg_GreenValues(x,y,1:actualNumOfImages));
median_bg_BlueValues(x,y) = median(bg_BlueValues(x,y,1:actualNumOfImages));
end
end
median_bg_Red/Green/BlueValues is a 480*640 matrix, and bg_Red/Green/BlueValues is a 480x640x40 image, and I want to take the median of each cell's 40 elements (these 40 elements are the different pixel values for the 40 images).
How can I make this faster using vectorization?
Thanks.

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 2 月 17 日
median_bg_RedValues = median(bg_RedValues, 3);
median_bg_GreenValues = median(bg_GreenValues,3);
median_bg_BlueValues = median(bg_BlueValues, 3);
  4 件のコメント
Faizan Tahir
Faizan Tahir 2016 年 2 月 17 日
Done, thanks again.
Andrei Bobrov
Andrei Bobrov 2016 年 2 月 18 日
Thank you Faizan!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Image Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by