Vous êtes sur la page 1sur 5

his code is used to track red colour objects in a Video.

the same code


can be used for tracking other colour

Matlab Gui for Red Colour Object Tracking

Matlab Code for Red colour object Tracking


function varargout = redtracking(varargin)
% REDTRACKING MATLAB code for redtracking.fig
% REDTRACKING, by itself, creates a new REDTRACKING or ra
ises the existing
% singleton*.
%
% H = REDTRACKING returns the handle to a new REDTRACKING
or the handle to
% the existing singleton*.
%
% REDTRACKING('CALLBACK',hObject,eventData,handles,...) c
alls the local
% function named CALLBACK in REDTRACKING.M with the given
input arguments.
%
% REDTRACKING('Property','Value',...) creates a new REDTR
ACKING or raises the
% existing singleton*. Starting from the left, property v
alue pairs are
% applied to the GUI before redtracking_OpeningFcn gets c
alled. An
% unrecognized property name or invalid value makes prope
rty application
% stop. All inputs are passed to redtracking_OpeningFcn v
ia varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI all
ows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help redt


racking

% Last Modified by GUIDE v2.5 03-Sep-2017 13:08:30

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @redtracking_OpeningFcn, ...
'gui_OutputFcn', @redtracking_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 redtracking is made visible.


function redtracking_OpeningFcn(hObject, eventdata, handl
es, 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 GUIDA
TA)
% varargin command line arguments to redtracking (see VAR
ARGIN)

% Choose default command line output for redtracking


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes redtracking wait for user response (see UI


RESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the comm


and line.
function varargout = redtracking_OutputFcn(hObject, event
data, handles)
% varargout cell array for returning output args (see VAR
ARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version
of MATLAB
% handles structure with handles and user data (see GUIDA
TA)

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on button press in StartTracking.


function StartTracking_Callback(hObject, eventdata, handl
es)
% hObject handle to StartTracking (see GCBO)
% eventdata reserved - to be defined in a future version
of MATLAB
% handles structure with handles and user data (see GUIDA
TA)

a = imaqhwinfo;
camera_name = char(a.InstalledAdaptors(end));
camera_info = imaqhwinfo(camera_name);
camera_id = camera_info.DeviceInfo.DeviceID(end);
resolution = char(camera_info.DeviceInfo.SupportedFormats
(end));

% Capture the video frames using the videoinput function


% You have to replace the resolution & your installed ada
ptor name.
vid = videoinput(camera_name, camera_id, resolution);

% Set the properties of the video object


set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;

%start the video aquisition here


start(vid)

% Set a loop that stop after 100 frames of aquisition


while(vid.FramesAcquired<=50)

% Get the snapshot of the current frame


data = getsnapshot(vid);

% Now to track red objects in real time


% we have to subtract the red component
% 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 im
age.
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(data)

hold on

%This is a loop to bound the red objects in a rectangula


r box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','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', 'FontS
ize', 12, 'Color', 'yellow');
end

hold off
end
% Both the loops end here.

% Stop the video aquisition.


stop(vid);

% Flush all the image data stored in the memory buffer.


flushdata(vid);

% Clear all variables


clear all

Vous aimerez peut-être aussi