Info

此问题已关闭。 请重新打开它进行编辑或回答。

I rewrote my moving object detection code and tried integrating a toggle button with it still to no result.

1 次查看(过去 30 天)
i used the toggle button to start the video input and the processing and this should carry on till i press the toggle button again. But i get an error of the following
??? Undefined function or variable "bg".
Error in ==> Untitled>togglebutton1_Callback at 89 fr_size = size(bg);
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> Untitled at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)untitled('togglebutton1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
here is my code
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*. %
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
% % UNTITLED('Property','Value',...) creates a new UNTITLED or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before untitled_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to untitled_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled
% Last Modified by GUIDE v2.5 05-Jan-2013 15:43:02
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @untitled_OpeningFcn, ... 'gui_OutputFcn', @untitled_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible. function untitled_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = untitled_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure varargout{1} = handles.output;
% --- Executes on button press in togglebutton1. function togglebutton1_Callback(hObject, eventdata, handles) % hObject handle to togglebutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1
while get(hObject,'Value')
source = videoinput('winvideo');
set(source,'FramesPerTrigger',Inf);
thresh = 15;
start(source);
fr_size = size(bg);
width = fr_size(2);
height = fr_size(1);
f = zeros(height, width);
%f2 = zeros(height, width);
bg =getdata(source(1)); % read in 1st frame as background frame bg_bw = rgb2gray(bg); % convert background to greyscale
for i = 2:3:(length(source)-1);
fr1 = getdata(source(i-1)); % read in frame-i-1
fr_bw1 = rgb2gray(fr1); % convert frame-i-1 to grayscale
fr2 = getdata(source(i)); % read in frame-i
fr_bw2 = rgb2gray(fr2); % convert frame-i to grayscale
fr3 = getdata(source(i+1)); % read in frame-i+1
fr_bw3 = rgb2gray(fr3); % convert frame-i+1 to grayscale
fr_diff1 = abs(double(fr_bw1) - double(fr_bw2)); % First frame Difference cast operands as double to avoid negative overflow
fr_diff2 = abs(double(fr_bw2) - double(fr_bw3)); % Second frame difference cast operands as double to avoid negative overflow
for j=1:width % if fr_diff > thresh pixel in foreground
for k=1:height
if ((fr_diff1(k,j) > thresh) && (fr_diff2(k,j) > thresh))
f(k,j) = 255;
else
f(k,j) = 0;
end
end
subplot(1,2,2);
mot=getdatea(uint8(f));
imagesc(mot);
title('MOVING OBJECT')
end
end
end stop(source);

回答(1 个)

Image Analyst
Image Analyst 2013-4-5
It doesn't know what bg is yet because you haven't defined it yet. Make this line:
bg =getdata(source(1));
the first line in togglebutton1_Callback and see what happens.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by