0% found this document useful (0 votes)
123 views

Detection and Tracking of RED Color in Video Streaming by Using MATLAB

The document describes a MATLAB code for detecting and tracking red color in a video stream. It captures video frames using the video input function and detects red color by subtracting the red component from the grayscale image. It then uses median filtering, thresholding and connected component labeling to track red objects in real time. Rectangular boxes and labels are drawn around detected objects. The code provides contact information for the author, Engr Rana Muhammad Shakeel, and links to related projects and websites.

Uploaded by

Akhirul Hajri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

Detection and Tracking of RED Color in Video Streaming by Using MATLAB

The document describes a MATLAB code for detecting and tracking red color in a video stream. It captures video frames using the video input function and detects red color by subtracting the red component from the grayscale image. It then uses median filtering, thresholding and connected component labeling to track red objects in real time. Rectangular boxes and labels are drawn around detected objects. The code provides contact information for the author, Engr Rana Muhammad Shakeel, and links to related projects and websites.

Uploaded by

Akhirul Hajri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

for quire contact: engnr.shakeel@gamil.

com

Detection and Tracking of RED color in Video


Streaming by Using MATLAB
% Capture the video frames using the video input function
% You have to replace the resolution & your installed adaptor name.
% Set the properties of the video object
%start the video aquisition here
%detect RED colour and Track

BEST OF LUCK.
Engr Rana Muhammad Shakeel
[email protected]
+92-333-4962507
https://www.facebook.com/EngnrShakeel

FOR MY MORE SHORT PROJECTS:


https://uettaxila.academia.edu/EngrRanaMShakeel

visit my site
http://electro-technolgy.blogspot.com

Engr Rana M Shakeel, +92-333-4962507 | [email protected]

for quire contact: [email protected]

Function CODE:
function [camera_name, camera_id, resolution] = getCameraInfo(a)
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));

Main CODE:
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);

% Capture the video frames using the videoinput function


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

% Set the properties of the video object


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

Engr Rana M Shakeel, +92-333-4962507 | [email protected]

for quire contact: [email protected]

%start the video aquisition here


start(vid)

% Set a loop that stop after 100 frames of aquisition


while(vid.FramesAcquired<=200)

% 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 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);

Engr Rana M Shakeel, +92-333-4962507 | [email protected]

for quire contact: [email protected]

% 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 rectangular 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', 'FontSize', 12, 'Color',
'yellow');
end

hold off
end
% Both the loops end here.

Engr Rana M Shakeel, +92-333-4962507 | [email protected]

for quire contact: [email protected]

% Stop the video aquisition.


stop(vid);

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


flushdata(vid);

% Clear all variables


clear all
sprintf('%s','That was all about Image tracking, Guess that was pretty easy :) ')

Engr Rana M Shakeel, +92-333-4962507 | [email protected]

You might also like