How to display single input webcam in one axes and then use it in next 3 axes for output

1 view (last 30 days)
Hii I am making gui with 4 axes (axes1,axes2,axes3,axes4)and a button to start live video in axes1 after loading original video i want to use it in another 3 axes for output like color and motion detecion but when I used it in axes2 it never show output for axes3 please help me out
  2 Comments
nikita rane
nikita rane on 15 May 2015
Edited: Walter Roberson on 15 May 2015
thanks for the reply
This is my code
function varargout = fdm(varargin)
% FDM MATLAB code for fdm.fig
% FDM, by itself, creates a new FDM or raises the existing
% singleton*.
%
% H = FDM returns the handle to a new FDM or the handle to
% the existing singleton*.
%
% FDM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FDM.M with the given input arguments.
%
% FDM('Property','Value',...) creates a new FDM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before fdm_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to fdm_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 fdm
% Last Modified by GUIDE v2.5 05-May-2015 20:22:22
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @fdm_OpeningFcn, ...
'gui_OutputFcn', @fdm_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 fdm is made visible.
function fdm_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 fdm (see VARARGIN)
% Choose default command line output for fdm
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes fdm wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = fdm_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid
error=50;
s=25;
vid=videoinput('winvideo',2,'YUY2_640x480');% take video input
triggerconfig(vid,'manual'); % after triggering only we get video
set(vid,'FramesPerTrigger',1); %only 1 frame per trigger
set(vid,'TriggerRepeat', Inf); %repeating trigger infinite timf
set(vid,'ReturnedColorSpace','rgb');% colour space used is rgb
i=0;
start(vid);% video is started
%adjusting for background noise
for n=1:50
trigger(vid);
temp=getdata(vid,1);
f(:,:,mod(n,2)+1)=temp(2);
g=f(:,:,1)~=f(:,:,2);
imshow(g);
%sm(1,n)=backgroundcheck(g);
end
clear g;
clear temp;
%f=zeros(480,640,2);
%s=max(sm(25:50));
if (nargin==0)
error=2;
end
s=(s*(error+100))/100;% calculate sensitivity
clear sm;
clc
disp('backbround sensitivity=');%display sensitivity
disp(s);
disp('MOTION DETECTOR ON ->');
prev=getsnapshot(vid);%take video snpshot as previous one
while(1)
% for mk=1:100
j=0;
trigger(vid);
temp=getdata(vid,1);% take video data
axes(handles.axes1); %point to the axes where u want to display the frame
imshow(temp); %show the frame on axes
g=getsnapshot(vid); %get current frame
%f(:,:,mod(i,2)+1)=temp;
%g=f(:,:,1)~=f(:,:,2);
%g=rgb2gray(g);
a=prev-g; %take difference between 2 frames
a=rgb2gray(a);
%compp=sum(sum(a));
%disp(compp);
%err=((s/10)*100*50);
%disp(err);
prev=g; % store current frame as previous frame
if((sum(sum(a))/100)>(s/10)*100*50)%check if difference between frames is greater than threshold
% axes(handles.axes5);
% imshow('abc.jpg');%yes than motion detected
axes(handles.axes2);
imshow(a);
%show difference as snapshot
% imwrite(g,'captured.jpeg');%store difference to folder
j=j+1;% continue the loop
else
% axes(handles.axes5);
% imshow('pqr.jpg');
pause(1)
end
if(mod(j,6)==0) %check if motion detected 6 times
clc;
disp('MOTION DETECTOR ON ->');
% y=y(1:3000); % to play the alarm
% 0 for a=1:4
% wavplay(y,4500,'sync');
end
i=i+1;
% disp('difference');
% disp(sum(sum(sum(a))));
%continue the loop
end
while(vid1.FramesAcquired<=100000)
data = getsnapshot(vid1);
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(diff_im)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','y','LineWidth',2)
% plot(bc(1),bc(2), '-m+')
% a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
% set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
axes(handles.axes3);
hold off
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid
stop(vid);
  • *this is my output
but it is displaying only motion detection in axes2 but not showing color detection in axes3 which is given in code**

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 15 May 2015
Do not rely on the active axes() call to display your output. Always Parent your graphics operations. See this discussion.
Amongst other things doing so will help catch assumptions about which axes is active that turn out not to be true.

Tags

Community Treasure Hunt

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

Start Hunting!