MatLabscript uitbaldetectie: Difference between revisions
Jump to navigation
Jump to search
(Created page with '<code style=fontsize:18px> close all; clear camObj; w=1; d=0; v=1 ; %% Setup clear camObj if w camObj=gigecam; end coeffO = 5; coeffC = 5; centr_drone = []; angle=0; videoPlaye…') |
No edit summary |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<code style= | <code style="font-size:14px"> | ||
close all; clear camObj; | close all; clear camObj; <br> | ||
<br> | |||
w=1;<br> | |||
d=0;<br> | |||
v=1<br> | |||
; | |||
%% Setup<br> | |||
clear camObj<br> | |||
if w camObj=gigecam; end<br> | |||
coeffO = 5;<br> | |||
coeffC = 5;<br> | |||
centr_drone = [];<br> | |||
angle=0;<br> | |||
videoPlayer=vision.VideoPlayer('Name','Video','Position', [10, 10, 720, 480]);<br> | |||
foregrounddetect = vision.ForegroundDetector('NumGaussians', 3, ...<br> | |||
'NumTrainingFrames', 10, 'MinimumBackgroundRatio', 0.7);<br> | |||
nr=0;<br> | |||
<br> | |||
%setup blob analyzer<br> | |||
blob_ball = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...<br> | |||
'MajorAxisLengthOutputPort',true,'EccentricityOutputPort', true, 'CentroidOutputPort', true, ...<br> | |||
'MinimumBlobArea', 100, 'MaximumBlobArea',1000);<br> | |||
blob_drone = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...<br> | |||
'CentroidOutputPort', true, 'MajorAxisLengthOutputPort',true,'MinorAxisLengthOutputPort',true,'OrientationOutputPort',true, ...<br> | |||
'MinimumBlobArea', 1000, 'MaximumBlobArea',4000); <br> | |||
if ~w videoReader = vision.VideoFileReader('topcam_drone.mp4'); end<br> | |||
<br> | |||
%Coordinates of field corners and goal points | |||
%setup blob analyzer | |||
blob_ball = vision.BlobAnalysis('BoundingBoxOutputPort', true, ... | |||
'MajorAxisLengthOutputPort',true,'EccentricityOutputPort', true, 'CentroidOutputPort', true, ... | |||
'MinimumBlobArea', 100, 'MaximumBlobArea',1000); | |||
blob_drone = vision.BlobAnalysis('BoundingBoxOutputPort', true, ... | |||
'CentroidOutputPort', true, 'MajorAxisLengthOutputPort',true,'MinorAxisLengthOutputPort',true,'OrientationOutputPort',true, ... | |||
'MinimumBlobArea', 1000, 'MaximumBlobArea',4000); | |||
if ~w videoReader = vision.VideoFileReader('topcam_drone.mp4'); end | |||
%Coordinates of field corners and goal points | |||
bottom_left=[168, 1049]; %[x, y] | bottom_left=[168, 1049]; %[x, y] | ||
bottom_right=[1699, 1008]; %[x, y] | bottom_right=[1699, 1008]; %[x, y] | ||
Line 51: | Line 52: | ||
cheering = audioplayer(cheering, 44100) | cheering = audioplayer(cheering, 44100) | ||
%% Start Analysis | %% Start Analysis<br> | ||
<br> | |||
if ~v | if ~v<br> | ||
img=snapshot(camObj); | img=snapshot(camObj);<br> | ||
step(videoPlayer,img); | step(videoPlayer,img);<br> | ||
end | end<br> | ||
centr_prev = [0;0]; | centr_prev = [0;0];<br> | ||
angle_prev = 0; | angle_prev = 0;<br> | ||
<br> | |||
if d | if d<br> | ||
drone.takeoff(); | drone.takeoff(); | ||
end | end<br> | ||
<br> | |||
while (isOpen(videoPlayer)) || nr==0 | while (isOpen(videoPlayer)) || nr==0 | ||
nr=nr+1; | nr=nr+1; | ||
if nr==5 | if nr==5 | ||
Line 121: | Line 122: | ||
end | end | ||
%% Line Detection | %% Line Detection | ||
down_line=(bottom_left(2)+(ydiff_up/xdiff_down)*bottom_left(1))-(ydiff_up/xdiff_down)*centr_ball(1); | down_line=(bottom_left(2)+(ydiff_up/xdiff_down)*bottom_left(1))-(ydiff_up/xdiff_down)*centr_ball(1); | ||
left_line=(upper_left(1)+(xdiff_left/ydiff_left)*upper_left(2))+(xdiff_left/ydiff_left)*centr_ball(2); | left_line=(upper_left(1)+(xdiff_left/ydiff_left)*upper_left(2))+(xdiff_left/ydiff_left)*centr_ball(2); | ||
right_line=(bottom_right(1)+(xdiff_right/ydiff_right)*bottom_right(2))+(xdiff_right/ydiff_right)*centr_ball(2); | right_line=(bottom_right(1)+(xdiff_right/ydiff_right)*bottom_right(2))+(xdiff_right/ydiff_right)*centr_ball(2); | ||
%IF loop (if the ball is above the top line, play the whistle) | |||
if ball_prev(1)>0 && ball_prev(2)>0 && ball_prev(2)<200 && centr_ball(1)==0 && centr_ball(2)==0 | if ball_prev(1)>0 && ball_prev(2)>0 && ball_prev(2)<200 && centr_ball(1)==0 && centr_ball(2)==0 | ||
if ~isplaying(my_player) | if ~isplaying(my_player) | ||
Line 169: | Line 138: | ||
if centr_ball(1)~=0 && centr_ball(2)~=0 | if centr_ball(1)~=0 && centr_ball(2)~=0 | ||
%IF loop (if the ball is above the bottom line, play the whistle) | |||
if centr_ball(2)>=down_line | if centr_ball(2)>=down_line | ||
if ~isplaying(my_player) | if ~isplaying(my_player) | ||
Line 177: | Line 147: | ||
end %out of field down line | end %out of field down line | ||
%IF loop (if the ball is in the left goal, play the GOLGOLGOL-tune) | |||
if (centr_ball(1)<=left_line && centr_ball(2)>=gl_up && centr_ball(1)<=left_line && centr_ball(2)<=gl_down) | if (centr_ball(1)<=left_line && centr_ball(2)>=gl_up && centr_ball(1)<=left_line && centr_ball(2)<=gl_down) | ||
if ~isplaying(cheering) | if ~isplaying(cheering) | ||
Line 186: | Line 157: | ||
end %goal left half | end %goal left half | ||
%IF loop (if the ball is in the right goal, play the GOLGOLGOL-tune) | |||
if (centr_ball(1)>=right_line && centr_ball(2)>=gr_up && centr_ball(1)>=right_line && centr_ball(2)<=gr_down) | if (centr_ball(1)>=right_line && centr_ball(2)>=gr_up && centr_ball(1)>=right_line && centr_ball(2)<=gr_down) | ||
if ~isplaying(cheering) | if ~isplaying(cheering) | ||
Line 195: | Line 167: | ||
end %goal right half | end %goal right half | ||
%IF loop (if the ball is right of the right line, play the whistle) | |||
if (centr_ball(1)>=right_line && centr_ball(2)<=gr_up) || (centr_ball(1)>=right_line && centr_ball(2)>=gr_down) | if (centr_ball(1)>=right_line && centr_ball(2)<=gr_up) || (centr_ball(1)>=right_line && centr_ball(2)>=gr_down) | ||
if ~isplaying(my_player) | if ~isplaying(my_player) | ||
Line 203: | Line 176: | ||
[]; | []; | ||
end %out of field right | end %out of field right | ||
%IF loop (if the ball is left of the left line, play the whistle) | |||
if (centr_ball(1)<=left_line && centr_ball(2)<=gl_up) || (centr_ball(1)<=left_line && centr_ball(2)>=gl_down) | |||
if ~isplaying(my_player) | if ~isplaying(my_player) | ||
play(my_player); | play(my_player); | ||
Line 213: | Line 185: | ||
[]; | []; | ||
end %out of field left | end %out of field left | ||
end | end | ||
test = [0 0]; | test = [0 0]; | ||
Line 224: | Line 195: | ||
%foreground = insertShape(1.*foreground, 'Line', [centr_drone,test]); | %foreground = insertShape(1.*foreground, 'Line', [centr_drone,test]); | ||
%Displaying the yellow lines for clarification | |||
img = insertShape(1.*img, 'Line',[bottom_left,bottom_right],'LineWidth', 5); | img = insertShape(1.*img, 'Line',[bottom_left,bottom_right],'LineWidth', 5); | ||
img = insertShape(1.*img, 'Line',[upper_left,bottom_left],'LineWidth', 5); | img = insertShape(1.*img, 'Line',[upper_left,bottom_left],'LineWidth', 5); | ||
Line 230: | Line 202: | ||
end | end | ||
end | end<br> | ||
<br> | |||
if d<br> | |||
drone.land(); | |||
end<br> | |||
release(videoPlayer);<br> | |||
</code> | |||
---- | |||
Terug naar: [[PRE2015_2_Groep2]] | |||
Latest revision as of 21:01, 14 January 2016
close all; clear camObj;
w=1;
d=0;
v=1
;
%% Setup
clear camObj
if w camObj=gigecam; end
coeffO = 5;
coeffC = 5;
centr_drone = [];
angle=0;
videoPlayer=vision.VideoPlayer('Name','Video','Position', [10, 10, 720, 480]);
foregrounddetect = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 10, 'MinimumBackgroundRatio', 0.7);
nr=0;
%setup blob analyzer
blob_ball = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'MajorAxisLengthOutputPort',true,'EccentricityOutputPort', true, 'CentroidOutputPort', true, ...
'MinimumBlobArea', 100, 'MaximumBlobArea',1000);
blob_drone = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'CentroidOutputPort', true, 'MajorAxisLengthOutputPort',true,'MinorAxisLengthOutputPort',true,'OrientationOutputPort',true, ...
'MinimumBlobArea', 1000, 'MaximumBlobArea',4000);
if ~w videoReader = vision.VideoFileReader('topcam_drone.mp4'); end
%Coordinates of field corners and goal points
bottom_left=[168, 1049]; %[x, y]
bottom_right=[1699, 1008]; %[x, y]
upper_left=[155, 5]; %[x,y]
upper_right=[1685, 5]; %[x,y]
gl_up=370; %[y]
gl_down=670; %[y]
gr_up=350; %[y]
gr_down=630; %[y]
%Creating a formula for y, dependent on x
xdiff_down=bottom_right(1)-bottom_left(1);
ydiff_up=bottom_left(2)-bottom_right(2);
xdiff_left=bottom_left(1)-upper_left(1);
ydiff_left=bottom_left(2)-upper_left(2);
xdiff_right=upper_right(1)-bottom_right(1);
ydiff_right=upper_right(2)-bottom_right(2);
%Loading in sound file
my_sound = audioread('whistle.wav');
cheering=audioread('cheering.wav');
my_player = audioplayer(my_sound, 44100);
cheering = audioplayer(cheering, 44100)
%% Start Analysis
if ~v
img=snapshot(camObj);
step(videoPlayer,img);
end
centr_prev = [0;0];
angle_prev = 0;
if d
drone.takeoff();
end
while (isOpen(videoPlayer)) || nr==0
nr=nr+1;
if nr==5
release(foregrounddetect);
elseif nr>5 && nr<8 && d
moveForward(drone,0.5);
end
%Read one frame
if w
img=snapshot(camObj);
else
img=rgb2gray(step(videoReader));
end
%Filter out parts outside of the field
img = img(:,1:end-100);
img(img>200)=90;
foreground = foregrounddetect.step(img);
%foreground = imopen(foreground, strel('rectangle', [coeffO,coeffO]));
foreground = imclose(foreground, strel('rectangle', [coeffC, coeffC]));
if ~isempty(centr_drone)
centr_prev=centr_drone;
ball_prev = centr_ball;
angle_prev = angle;
else
centr_prev = [0;0];
angle_prev = angle;
ball_prev = [0;0];
end
%3a: detect blobs, return centroids, bounding boxes, eccentricity and
%diameter
[~,centr_ball,bbox_ball,diam,ecc] = step(blob_ball,foreground); %For ball
[~,centr_drone,bbox_drone,Ma,Mi,angle] = step(blob_drone,foreground); %For drone
%3b: maximize for most round object for ball
if ~isempty(centr_ball)
[ecmin,I] = min(ecc,[],1);
centr_ball = centr_ball(I,:);
diam = diam(I);
else
centr_ball = [0 0]; diam = 0;
ecmin=1e10;
end
if ecmin>0.7
centr_ball = [0 0]; diam = 0;
%disp('Noise!');
else
%disp('Ball detected');
end
%% Line Detection
down_line=(bottom_left(2)+(ydiff_up/xdiff_down)*bottom_left(1))-(ydiff_up/xdiff_down)*centr_ball(1);
left_line=(upper_left(1)+(xdiff_left/ydiff_left)*upper_left(2))+(xdiff_left/ydiff_left)*centr_ball(2);
right_line=(bottom_right(1)+(xdiff_right/ydiff_right)*bottom_right(2))+(xdiff_right/ydiff_right)*centr_ball(2);
%IF loop (if the ball is above the top line, play the whistle)
if ball_prev(1)>0 && ball_prev(2)>0 && ball_prev(2)<200 && centr_ball(1)==0 && centr_ball(2)==0
if ~isplaying(my_player)
play(my_player);
disp('Out! Top ');
end
end
if centr_ball(1)~=0 && centr_ball(2)~=0
%IF loop (if the ball is above the bottom line, play the whistle)
if centr_ball(2)>=down_line
if ~isplaying(my_player)
play(my_player);
disp('Out! Down');
end
end %out of field down line
%IF loop (if the ball is in the left goal, play the GOLGOLGOL-tune)
if (centr_ball(1)<=left_line && centr_ball(2)>=gl_up && centr_ball(1)<=left_line && centr_ball(2)<=gl_down)
if ~isplaying(cheering)
play(cheering);
disp('GOAL! - Left');
end
elseif centr_ball(1)==0
[];
end %goal left half
%IF loop (if the ball is in the right goal, play the GOLGOLGOL-tune)
if (centr_ball(1)>=right_line && centr_ball(2)>=gr_up && centr_ball(1)>=right_line && centr_ball(2)<=gr_down)
if ~isplaying(cheering)
play(cheering);
disp('GOAL! - Right');
end
elseif centr_ball(1)==0
[];
end %goal right half
%IF loop (if the ball is right of the right line, play the whistle)
if (centr_ball(1)>=right_line && centr_ball(2)<=gr_up) || (centr_ball(1)>=right_line && centr_ball(2)>=gr_down)
if ~isplaying(my_player)
play(my_player);
disp('Out! Right');
end
elseif centr_ball(1)==0
[];
end %out of field right
%IF loop (if the ball is left of the left line, play the whistle)
if (centr_ball(1)<=left_line && centr_ball(2)<=gl_up) || (centr_ball(1)<=left_line && centr_ball(2)>=gl_down)
if ~isplaying(my_player)
play(my_player);
disp('Out! Left')
end
elseif centr_ball(1)==0
[];
end %out of field left
end
test = [0 0];
test(1) = centr_drone(1) + 200*cosd(angle);
test(2) = centr_drone(2) + 200*sind(angle);
if v
foreground = insertShape(1.*foreground, 'Rectangle', bbox_drone);
foreground = insertShape(1.*foreground, 'FilledCircle', [centr_ball diam./2],'SmoothEdges',false,'Color', [255,0,0]);
%foreground = insertShape(1.*foreground, 'Line', [centr_drone,test]);
%Displaying the yellow lines for clarification
img = insertShape(1.*img, 'Line',[bottom_left,bottom_right],'LineWidth', 5);
img = insertShape(1.*img, 'Line',[upper_left,bottom_left],'LineWidth', 5);
img = insertShape(1.*img, 'Line',[upper_right,bottom_right],'LineWidth', 5);
step(videoPlayer,foreground);
end
end
if d
drone.land();
end
release(videoPlayer);
Terug naar: PRE2015_2_Groep2