Undefined function or variable in tracking green ball example
이전 댓글 표시
I'm attempting to follow the tracking a green ball example found here:
using my built in webcam. When I attempt to run the code I am getting an error undefined function or variable bwbw. I am using the following code for the trackball function:
function [img, bw] = trackball(img, thresh)
r = img(:, :, 1);
g = img(:, :, 2);
b = img(:, :, 3);
justGreen = g - r/2 - b/2;
bw = justGreen > thresh;
[x, y] = find(bw);
if ~isempty(x) && ~isempty(y)
xm = round(mean(x));
ym = round(mean(y));
xx = max(1, xm-5):min(xm+5, size(bw, 1));
yy = max(1, ym-5):min(ym+5, size(bw, 2));
bwbw = zeros(size(bw), 'uint8');
bwbw(xx, yy) = 255;
end
imagesc(justGreen + bwbw);
and am calling it using this code (I've tried running it both from the command window and by creating a script and running it):
function [img, bw] = trackball(img, thresh)
r = img(:, :, 1);
g = img(:, :, 2);
b = img(:, :, 3);
justGreen = g - r/2 - b/2;
bw = justGreen > thresh;
[x, y] = find(bw);
if ~isempty(x) && ~isempty(y)
xm = round(mean(x));
ym = round(mean(y));
xx = max(1, xm-5):min(xm+5, size(bw, 1));
yy = max(1, ym-5):min(ym+5, size(bw, 2));
bwbw = zeros(size(bw), 'uint8');
bwbw(xx, yy) = 255;
end
imagesc(justGreen + bwbw);
I was able to run it successfully once last week without obtaining this error, but now I cannot fix it. I do make sure to create a camera object (using cam = webcam) before I run the loop that captures the snapshots. Any help would be greatly appreciated. Thanks!
채택된 답변
추가 답변 (1개)
John D'Errico
2016년 3월 22일
Um, think about how and when the variable bwbw is defined.
[x, y] = find(bw);
if ~isempty(x) && ~isempty(y)
...
bwbw = zeros(size(bw), 'uint8');
bwbw(xx, yy) = 255;
end
Is it possible that the if statement never triggers? Better, is it a fact?
Under what circumstances will that the clause in the if statement not be true?
When you have a problem like this in a function, LEARN TO USE THE DEBUGGER. The debugger is your friend.
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!