can any one help me i have placed my queries of the code as qsn mark plz answer the qsns and plz help me to draw the bounding around the ball after plotting centeroid ? the below gives output up to plotting a centroid.

2 次查看(过去 30 天)
if true
% code
endclc;
clear all;
close all;
%----------------video input--------------------------------------%
vid=uigetfile('.avi','select an video'); % to read file to matlab work space
info=mmfileinfo(vid); % to get the multimedia info
t=info.Duration; % to get the duration of video
vid=mmreader(vid);
numFrames = vid.NumberOfFrames; %to calculate number of frames
n=numFrames;
get(vid);
framerate=vid.FrameRate; % to calculate frame rate
for i =1:200 % to read first 200 frames
frames = read(vid,i);
imwrite(frames,strcat([int2str(i) '.tif'])); %y strcat%
end
for i= 1:200
if(i<50) %? Why 50 %
greenBall1=imread([int2str(i),'.tif']); % y int2str%
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); % why %
% 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'}); % how centroid is placed %
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);
distance(i,:)=sqrt(differ(i,1)+differ(i,2));
disp(distance);
velocity(i,:)=(distance(i,:)/framerate);
disp('velocity=');
disp(velocity);
end
end
velocity1=mean(distance)/framerate;
disp(velocity1);_
can any one help me i have placed my queries as qsn mark plz answer the qsns and plz help me to draw the bounding around the ball after plotting centeroid ? the code plotted centeroid and calculated velocity of the ball in a video i just want to place a bounding box around the ball so that we complete our project object detection basing on color ?

采纳的回答

Image Analyst
Image Analyst 2014-4-5
The lines you marked are:
imwrite(frames,strcat([int2str(i) '.tif'])); %?%
ball1 = bwareaopen(bw, 30); %?%
disp(['Center location is (',num2str(s1(id).Centroid(1),4),', ',num2str(s1(id).Centroid(2),4),')']); %?%
The first line writes the array out to a file called 1.tif, 2.tif, 3.tif, etc.
The second line takes a binary image (true/false or 1/0) called bw, and removes all regions that are 30 or smaller in area, and gives that back to you in a new binary image called ball1.
The third line displays a sentence in the command window telling you where the centroid is.
  16 个评论
Image Analyst
Image Analyst 2014-4-6
Well that's the art of designing robust algorithms. You have to keep tweaking things until it works. It usually works for your first image and then doesn't for other images, so you keep fine tuning your algorithm until it's as robust as you need it to be for all the images you expect to encounter. I'm glad I could help.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by