"Undefined variable" error even though variable is defined

9 ビュー (過去 30 日間)
LaraS
LaraS 2024 年 2 月 22 日
回答済み: Steven Lord 2024 年 2 月 23 日
I'm using part of a code I borrowed from @imageanalyst where I'm trying to run through frames of a video to look at how the intensity changes over time. I'd like to perform a background subtraction with the code below
if frame1 == 1
Background1 = thisFrame1;
else
for t=1:frame1
% Change background slightly at each frame
Background1 = (1-alpha1)* thisFrame1 + alpha1 * Background1;
end
end
I get an error stating:
"Unrecognized function or variable 'Background1'.
Background1 = (1-alpha1)* thisFrame1 + alpha1 * Background1;"
even though I have the function defined. when I run the same line of code with a different file, it works fine, even though they're the same file type. any idea as to why this is happening? thanks!
  1 件のコメント
Stephen23
Stephen23 2024 年 2 月 22 日
編集済み: Stephen23 2024 年 2 月 22 日
""Undefined variable" error even though variable is defined"
But is it really defined? On the first loop iteration, what value does Background1 have?

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

採用された回答

Steven Lord
Steven Lord 2024 年 2 月 23 日
I'm guessing that you want to initialize Background1 when t is equal to 1 and to use the formula to update Background1 when t is greater than 1. In that case:
for t=1:frame1
if t == 1
Background1 = thisFrame1;
else
% Change background slightly at each frame
Background1 = (1-alpha1)* thisFrame1 + alpha1 * Background1;
end
end
Except that when t is 2, Background1 will be (1-alpha1)*thisFrame1 + alpha1*thisFrame1 = thisFrame1 (assuming there's no code later in the for loop after the if / else block that changes Background1. So perhaps if you describe in more detail what you want to do we can suggest some modifications to your code to achieve your goal.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 2 月 23 日
if frame1 == 1
Background1 = thisFrame1;
else
This does not define Background1 if frame1 ~= 1
  4 件のコメント
LaraS
LaraS 2024 年 2 月 23 日
編集済み: LaraS 2024 年 2 月 23 日
I actually tried correcting that while debugging (after posting this), but that didn't seem to help. Essentially what I'm trying to do is have the code update and adapt the background to each frame while it's running. It's really odd because it was running fine before when I was only running one video file. But I'm trying to update the script to run multiple files but it just throws this error about the undefined function now
DGM
DGM 2024 年 2 月 23 日
Well without knowing what the code or process is, there really isn't much I can suggest.

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

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by