how to draw bounding box to a object in a video after finding its centroid basing on its color ?

11 views (last 30 days)
clc;
clear all;
close all;
%----------------video input--------------------------------------%
vid=uigetfile('.avi','select an video');
info=mmfileinfo(vid);
t=info.Duration;
vid=mmreader(vid);
numFrames = vid.NumberOfFrames;
n=numFrames;
get(vid);
framerate=vid.FrameRate;
for i =1:50
frames = read(vid,i);
imwrite(frames,strcat([int2str(i) '.tif']));
end
for i= 1:50
if(i<50)
greenBall1=imread([int2str(i),'.tif']);
greenball2=imread([int2str(i+1),'.tif']);
%------------------------------------------------------------------%
figure;imshow(greenBall1);figure;
r = greenBall1(:, :, 1);
g = greenBall1(:, :, 2);
b = greenBall1(:, :, 3);
justGreen = g - r/2 - b/2;
bw = justGreen > 50;
figure;imagesc(greenBall1);
figure;
% imagesc(bw);
% colormap(gray);
ball1 = bwareaopen(bw, 30);
% figure;
% imagesc(ball1);
s1 = regionprops(ball1, {'centroid','Area'});
if isempty(s1)
error('No ball found!');
else
[~, id] = max([s1.Area]);
hold on, plot(s1(id).Centroid(1),s1(id).Centroid(2),'wp','MarkerSize',10,'MarkerFaceColor','b'), hold off
disp(['Center location is (',num2str(s1(id).Centroid(1),4),', ',num2str(s1(id).Centroid(2),4),')']);
end
%--------------------------------------------------------------------------
%-------------------------------------------------------------------------------------------------------%
r1 = greenball2(:, :, 1);
g1 = greenball2(:, :, 2);
b1 = greenball2(:, :, 3);
justGreen2 = g1 - r1/2 - b1/2;
bw2 = justGreen2 > 50;
figure;imagesc(greenball2);
% figure;imagesc(bw2);
% colormap(gray);
ball2 = bwareaopen(bw2, 30);
%figure;imagesc(ball2);
s2 = regionprops(ball2, {'centroid','area'});
if isempty(s2)
error('No ball found!');
else
[~, id] = max([s2.Area]);
hold on, plot(s2(id).Centroid(1),s2(id).Centroid(2),'wp','MarkerSize',10,'MarkerFaceColor','r'), hold off
disp(['Center location is (',num2str(s2(id).Centroid(1),4),', ',num2str(s2(id).Centroid(2),4),')'])
end
differ(i,1)=(s2(id).Centroid(1)-s1(id).Centroid(1)).^2;
differ(i,2)=(s2(id).Centroid(2)-s1(id).Centroid(2)).^2;
%disp(differ);
figure;
% imshow(differ);
% hline = imdistline(differ,s1(id).Centroid,s2(id).Centroid);
% api = iptgetapi(hline);
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);
% 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','b','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
distance(i,:)=sqrt(differ(i,1)+differ(i,2));
disp(distance);
velocity(i,:)=(distance(i,:)/framerate);
disp('velocity=');
end
end
It is the code we worked on please help me to draw bounding box we left with only 2 days to submit our project .any one plz help me i have placed the source code and the video we working on by zipping the files run the code by placing the video and help me to draw bounding box around the ball

Accepted Answer

Image Analyst
Image Analyst on 4 Apr 2014
Does rectangle() not draw anything?
  2 Comments
anji venkat
anji venkat on 4 Apr 2014
sir this is all we have with us i zipped the files and attached here plz any one help me to draw bounding to the green ball in the video i hav attached here i made up to plot centroid of the ball all need is to draw a bounding box around that green ball ?
Image Analyst
Image Analyst on 4 Apr 2014
Edited: Image Analyst on 4 Apr 2014
Nothing is attached. Though I give you no guarantees that I will be able to help you over the weekend. And you don't need to mark as Accepted if I didn't get it working for you yet.

Sign in to comment.

More Answers (1)

Shivani Gupta
Shivani Gupta on 20 Jul 2019
Hello All
I have written a code for Hand detection and tracking through a video in KLT algorithm, i am facing difficulty in getting the bounding box in whole video, can anyone help:
code :
clear all
close all
clc
%mov = VideoReader(filename);
mov = VideoReader('F:\VID_20190711_182526.mp4');
vidFrames = read(mov);
for frame = 1 : size(vidFrames, 4)
outputBaseFileName = sprintf('%3.3d.png', frame);
outputFullFileName = fullfile('F:\Framextractionoutput', outputBaseFileName);
imwrite( vidFrames(:,:,:,frame), outputFullFileName, 'png');
end
%% HAND DETECTION
%%
folder=('F:\Framextractionoutput\');
baseFileName=('001.png');
fullFileName=fullfile(folder,baseFileName);
format long g;
format compact;
fontSize = 20;
%IMAGE SEGMENTATION
img=imread(fullFileName);
img=rgb2ycbcr(img);
for i=1:size(img,1)
for j= 1:size(img,2)
cb = img(i,j,2);
cr = img(i,j,3);
if(~(cr > 132 && cr < 173 && cb > 76 && cb < 126))
img(i,j,1)=235;
img(i,j,2)=128;
img(i,j,3)=128;
end
end
end
img=ycbcr2rgb(img);
figure,
image1=imshow(img);
title('Skin Segmentation', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% SEGMENTED IMAGE TO GRAYIMAGE
grayImage=rgb2gray(img);
figure,
image2=imshow(grayImage);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% GRAY TO BINARY IMAGE
binaryImage = grayImage < 245;
BW=bwareaopen(binaryImage, 1500);
figure,
image3=imshow(BW, []);
hold on;
st = regionprops(BW, 'BoundingBox' );
for k = 1 : length(st)
thisBB = st(k).BoundingBox;
bbox = rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
%% KLT
videoFileReader = vision.VideoFileReader('F:\VID_20190711_182526.mp4');
videoFrame = step(videoFileReader);
videoFrame = insertShape(videoFrame, 'Rectangle', thisBB);
figure; imshow(videoFrame); title('Detected hand');
%%
% Convert the first box into a list of 4 points
% This is needed to be able to visualize the rotation of the object.
bboxPoints = bbox2points(thisBB(1, :));
%%
points = detectMinEigenFeatures(rgb2gray(videoFrame), 'ROI', thisBB);
% Display the detected points.
figure, imshow(videoFrame), hold on, title('Detected features');
plot(points);
%%
pointTracker = vision.PointTracker('MaxBidirectionalError', 2);
% Initialize the tracker with the initial point locations and the initial
% video frame.
points = points.Location;
initialize(pointTracker, points, videoFrame);
%%
videoPlayer = vision.VideoPlayer('Position',...
[100 100 [size(videoFrame, 2), size(videoFrame, 1)]+30]);
%%
oldPoints = points;
while ~isDone(videoFileReader)
% get the next frame
videoFrame = step(videoFileReader);
% Track the points. Note that some points may be lost.
[points, isFound] = step(pointTracker, videoFrame);
visiblePoints = points(isFound, :);
oldInliers = oldPoints(isFound, :);
if size(visiblePoints, 1) >= 2 % need at least 2 points
% Estimate the geometric transformation between the old points
% and the new points and eliminate outliers
[xform, oldInliers, visiblePoints] = estimateGeometricTransform(...
oldInliers, visiblePoints, 'similarity', 'MaxDistance', 4);
% Apply the transformation to the bounding box points
bboxPoints = transformPointsForward(xform, bboxPoints);
% Insert a bounding box around the object being tracked
bboxPolygon = reshape(bboxPoints', 1, []);
videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, ...
'LineWidth', 2);
% Display tracked points
videoFrame = insertMarker(videoFrame, visiblePoints, '+', ...
'Color', 'white');
% Reset the points
oldPoints = visiblePoints;
setPoints(pointTracker, oldPoints);
end
% Display the annotated video frame using the video player object
step(videoPlayer, videoFrame);
end
% Clean up
release(videoFileReader);
release(videoPlayer);
%%
release(pointTracker);

Community Treasure Hunt

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

Start Hunting!