MATLAB Videotracking code: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
mNo edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Onderstaande MATLAB code werkt volgens het principe dat beschreven staat in [[videotracking]]. De variabelen d, w en s aan het begin van de code kunnen op 0 of 1 gezet worden om respectievelijk de drone, een webcam en setup mode te gebruiken. Hierdoor kunnen makkelijk delen van de code uitgeschakeld worden, zonder gebruik te maken van een ingewikkelde user interface. | Onderstaande MATLAB code werkt volgens het principe dat beschreven staat in [[videotracking]]. De variabelen d, w en s aan het begin van de code kunnen op 0 of 1 gezet worden om respectievelijk de drone, een webcam en setup mode te gebruiken. Hierdoor kunnen makkelijk delen van de code uitgeschakeld worden, zonder gebruik te maken van een ingewikkelde user interface. | ||
<code style="font-size: | <code style="font-size:12px"> | ||
%% Drone Control | %% Drone Control | ||
Line 23: | Line 23: | ||
%outputPlayer2 = vision.VideoPlayer('Name','Filter 2','Position',[700,400, 500, 250]); | %outputPlayer2 = vision.VideoPlayer('Name','Filter 2','Position',[700,400, 500, 250]); | ||
end | end | ||
<br> | |||
release(outputPlayer); release(videoPlayer); | release(outputPlayer); release(videoPlayer); | ||
<br> | |||
nr = 0; %frame nr | nr = 0; %frame nr | ||
<br> | |||
%setup blob analyzer | %setup blob analyzer | ||
blob = vision.BlobAnalysis('BoundingBoxOutputPort', true, ... | blob = vision.BlobAnalysis('BoundingBoxOutputPort', true, ... | ||
'MajorAxisLengthOutputPort',true,'EccentricityOutputPort', true, 'CentroidOutputPort', true, ... | 'MajorAxisLengthOutputPort',true,'EccentricityOutputPort', true, 'CentroidOutputPort', true, ... | ||
'MinimumBlobArea', 500, 'MaximumBlobArea',8000); | 'MinimumBlobArea', 500, 'MaximumBlobArea',8000); | ||
<br> | |||
%% Open File / Stream | %% Open File / Stream | ||
<br> | |||
%%webcam | %%webcam | ||
if w video = webcam(2); | if w video = webcam(2); | ||
else videoReader = vision.VideoFileReader(vid_loc); end | else videoReader = vision.VideoFileReader(vid_loc); end | ||
<br> | |||
%tuneable vars | %tuneable vars | ||
i = 1; %intensity of green filter | i = 1; %intensity of green filter | ||
coeffO = 3; | coeffO = 3; | ||
coeffC = 15; | coeffC = 15; | ||
<br> | |||
%% Start video players before launch, for speed | %% Start video players before launch, for speed | ||
disp('Starting video players...'); | disp('Starting video players...'); | ||
Line 53: | Line 53: | ||
step(outputPlayer, frame); release(outputPlayer); | step(outputPlayer, frame); release(outputPlayer); | ||
%step(outputPlayer2, frame); release(outputPlayer2); | %step(outputPlayer2, frame); release(outputPlayer2); | ||
<br> | |||
%% Process frames one by one | %% Process frames one by one | ||
<br> | |||
if d | if d | ||
disp('Connecting to Drone...'); | disp('Connecting to Drone...'); | ||
%drone = ARDrone(); | %drone = ARDrone(); | ||
end | end | ||
<br> | |||
disp('Camera analysis is starting...'); | disp('Camera analysis is starting...'); | ||
<br> | |||
if d | if d | ||
disp('--WARNING: Drone is taking off--'); | disp('--WARNING: Drone is taking off--'); | ||
Line 68: | Line 68: | ||
Href = 1.5; | Href = 1.5; | ||
end | end | ||
<br> | |||
%Continue until window is closed | %Continue until window is closed | ||
while (isOpen(videoPlayer) || nr==0) | while (isOpen(videoPlayer) || nr==0) | ||
<br> | |||
%% 1 Read a single frame | %% 1 Read a single frame | ||
<br> | |||
%Height control | %Height control | ||
if d | if d | ||
<br> | |||
%every x frames, check if height value | %every x frames, check if height value | ||
h = drone.Altitude; | h = drone.Altitude; | ||
Altitude(drone, h, Href); | Altitude(drone, h, Href); | ||
<br> | |||
if (nr>100 && mod(nr,100)==0 && h==0) | if (nr>100 && mod(nr,100)==0 && h==0) | ||
disp('---WARNING: NO DATA FROM DRONE---'); | disp('---WARNING: NO DATA FROM DRONE---'); | ||
Line 86: | Line 86: | ||
disp('--Drone is landing--'); | disp('--Drone is landing--'); | ||
end | end | ||
<br> | |||
end | end | ||
<br> | |||
if w frame = snapshot(video); | if w frame = snapshot(video); | ||
else frame = videoReader.step(); end | else frame = videoReader.step(); end | ||
<br> | |||
%% 2 Filter image (frame) | %% 2 Filter image (frame) | ||
<br> | |||
%Filter out green (or anything like it) only | %Filter out green (or anything like it) only | ||
<br> | |||
%Formula: Mask is (G-i*B)<0 and (G-i*R)<0 | %Formula: Mask is (G-i*B)<0 and (G-i*R)<0 | ||
Mask = frame(:,:,2)<frame(:,:,3)*i & frame(:,:,2)<frame(:,:,1)*i; | Mask = frame(:,:,2)<frame(:,:,3)*i & frame(:,:,2)<frame(:,:,1)*i; | ||
Line 102: | Line 102: | ||
Out(:,:,2)=frame(:,:,2).*Mask; | Out(:,:,2)=frame(:,:,2).*Mask; | ||
Out(:,:,3)=frame(:,:,3).*Mask; | Out(:,:,3)=frame(:,:,3).*Mask; | ||
<br> | |||
%Filter lines with variable threshold | %Filter lines with variable threshold | ||
Ifr = rgb2gray(frame); %grayscale | Ifr = rgb2gray(frame); %grayscale | ||
Line 110: | Line 110: | ||
Lines = imopen(Lines, strel('rectangle', [coeffO,coeffO])); | Lines = imopen(Lines, strel('rectangle', [coeffO,coeffO])); | ||
Lines = imclose(Lines, strel('rectangle', [coeffC, coeffC])); | Lines = imclose(Lines, strel('rectangle', [coeffC, coeffC])); | ||
<br> | |||
%% 3 Detect objects | %% 3 Detect objects | ||
%Detect ball | %Detect ball | ||
Line 118: | Line 118: | ||
Red = imopen(Red, strel('rectangle', [coeffO,coeffO])); | Red = imopen(Red, strel('rectangle', [coeffO,coeffO])); | ||
Red = imclose(Red, strel('rectangle', [coeffC,coeffC])); | Red = imclose(Red, strel('rectangle', [coeffC,coeffC])); | ||
<br> | |||
%3a: detect blobs, return centroids, bounding boxes, eccentricity and | %3a: detect blobs, return centroids, bounding boxes, eccentricity and | ||
%diameter | %diameter | ||
[~,centr,bbox,diam,ecc] = step(blob,Red); | [~,centr,bbox,diam,ecc] = step(blob,Red); | ||
<br> | |||
%3b: maximize for most round object | %3b: maximize for most round object | ||
if ~isempty(centr) | if ~isempty(centr) | ||
Line 137: | Line 137: | ||
diam = []; | diam = []; | ||
end | end | ||
<br> | |||
%Reconstruction | %Reconstruction | ||
s = size(frame); | s = size(frame); | ||
Reconstruction = zeros(s(1),s(2)); | Reconstruction = zeros(s(1),s(2)); | ||
<br> | |||
if ~isempty(centr) | if ~isempty(centr) | ||
m = [round(centr(2)),round(centr(1))]; %midpoint, note: centr = [x y], size = [y x] | m = [round(centr(2)),round(centr(1))]; %midpoint, note: centr = [x y], size = [y x] | ||
Line 148: | Line 148: | ||
Reconstruction(m(1),m(2))=1; | Reconstruction(m(1),m(2))=1; | ||
%Reconstruction = imdilate(Reconstruction,strel('disk', round(diam(1)/2*0.8),8)); | %Reconstruction = imdilate(Reconstruction,strel('disk', round(diam(1)/2*0.8),8)); | ||
%DOESNT WORK %Reconstruction = bwdist(Reconstruction) <= round(diam/2); | %DOESNT WORK CURRENTLY %Reconstruction = bwdist(Reconstruction) <= round(diam/2); | ||
%diam(1) | %diam(1) | ||
end | end | ||
end | end | ||
<br> | |||
%% 4 Send the frame to the video players | %% 4 Send the frame to the video players | ||
<br> | |||
Red = insertShape(1.*Red, 'Circle', [centr diam./2]); | Red = insertShape(1.*Red, 'Circle', [centr diam./2]); | ||
Red = insertShape(1.*Red, 'Line', [s(2)/2,0,s(2)/2,s(1)]); | Red = insertShape(1.*Red, 'Line', [s(2)/2,0,s(2)/2,s(1)]); | ||
<br> | |||
%step(videoPlayer, frame); | %step(videoPlayer, frame); | ||
%outputPlayer2.Name = 'Reconstruction'; | %outputPlayer2.Name = 'Reconstruction'; | ||
Line 164: | Line 164: | ||
step(outputPlayer, Red); | step(outputPlayer, Red); | ||
nr = nr+1; %increment frame nr | nr = nr+1; %increment frame nr | ||
<br> | |||
%% 5 Output | %% 5 Output | ||
<br> | |||
if ~isempty(centr) | if ~isempty(centr) | ||
Out_vector = [centr(1,:) diam(1)]; | Out_vector = [centr(1,:) diam(1)]; | ||
Line 172: | Line 172: | ||
Out_vector = [0 0 0]; | Out_vector = [0 0 0]; | ||
end | end | ||
<br> | |||
if (d && h>0.8*Href) | if (d && h>0.8*Href) | ||
%Call | %Call Dronecontrol functie | ||
dronecamdata(drone,Out_vector(1),Out_vector(2),Out_vector(3)); | dronecamdata(drone,Out_vector(1),Out_vector(2),Out_vector(3)); | ||
end | end | ||
<br> | |||
end | end | ||
<br> | |||
release(videoPlayer); | release(videoPlayer); | ||
videoPlayer.hide(); outputPlayer.hide(); %outputPlayer2.hide(); | videoPlayer.hide(); outputPlayer.hide(); %outputPlayer2.hide(); | ||
close all; | close all; | ||
<br> | |||
%done | %done | ||
disp('Stopping video analysis...'); | disp('Stopping video analysis...'); | ||
<br> | |||
if d | if d | ||
disp('--Drone is landing--'); | disp('--Drone is landing--'); | ||
Line 192: | Line 192: | ||
land(drone); | land(drone); | ||
end | end | ||
<br> | |||
</code> | </code> | ||
Latest revision as of 12:45, 17 January 2016
Onderstaande MATLAB code werkt volgens het principe dat beschreven staat in videotracking. De variabelen d, w en s aan het begin van de code kunnen op 0 of 1 gezet worden om respectievelijk de drone, een webcam en setup mode te gebruiken. Hierdoor kunnen makkelijk delen van de code uitgeschakeld worden, zonder gebruik te maken van een ingewikkelde user interface.
%% Drone Control
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Toolboxes: Computer Vision, Image Acquisition, Webcam Support if webcam is used
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all;
d=1; %turn drone on/off
w=1; %switch file/live input
vid_loc = 'POV_camera_test.wmv'; %video input file
s=1; %setup mode: on/off, switch this off for speed for multiple tests after each other
%% Setup Objects
clear video;
if s
disp('Creating system objects...');
videoPlayer = vision.VideoPlayer('Name','Input','Position', [10, 400, 500, 250]);
outputPlayer = vision.VideoPlayer('Name','Filter 1','Position',[700,50, 500, 250]);
%outputPlayer2 = vision.VideoPlayer('Name','Filter 2','Position',[700,400, 500, 250]);
end
release(outputPlayer); release(videoPlayer);
nr = 0; %frame nr
%setup blob analyzer
blob = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'MajorAxisLengthOutputPort',true,'EccentricityOutputPort', true, 'CentroidOutputPort', true, ...
'MinimumBlobArea', 500, 'MaximumBlobArea',8000);
%% Open File / Stream
%%webcam
if w video = webcam(2);
else videoReader = vision.VideoFileReader(vid_loc); end
%tuneable vars
i = 1; %intensity of green filter
coeffO = 3;
coeffC = 15;
%% Start video players before launch, for speed
disp('Starting video players...');
if w frame = snapshot(video);
else frame = step(videoReader); end
s = size(frame);
Reconstruction = zeros(s(1),s(2));
step(videoPlayer, frame);
step(outputPlayer, frame); release(outputPlayer);
%step(outputPlayer2, frame); release(outputPlayer2);
%% Process frames one by one
if d
disp('Connecting to Drone...');
%drone = ARDrone();
end
disp('Camera analysis is starting...');
if d
disp('--WARNING: Drone is taking off--');
takeoff(drone);
Href = 1.5;
end
%Continue until window is closed
while (isOpen(videoPlayer) || nr==0)
%% 1 Read a single frame
%Height control
if d
%every x frames, check if height value
h = drone.Altitude;
Altitude(drone, h, Href);
if (nr>100 && mod(nr,100)==0 && h==0)
disp('---WARNING: NO DATA FROM DRONE---');
drone.land();
disp('--Drone is landing--');
end
end
if w frame = snapshot(video);
else frame = videoReader.step(); end
%% 2 Filter image (frame)
%Filter out green (or anything like it) only
%Formula: Mask is (G-i*B)<0 and (G-i*R)<0
Mask = frame(:,:,2)<frame(:,:,3)*i & frame(:,:,2)<frame(:,:,1)*i;
if w Mask = uint8(Mask); end
Out(:,:,1)=frame(:,:,1).*Mask;
Out(:,:,2)=frame(:,:,2).*Mask;
Out(:,:,3)=frame(:,:,3).*Mask;
%Filter lines with variable threshold
Ifr = rgb2gray(frame); %grayscale
thres = max(Ifr(:))*0.8;
Lines = (Ifr>thres);
%Morph
Lines = imopen(Lines, strel('rectangle', [coeffO,coeffO]));
Lines = imclose(Lines, strel('rectangle', [coeffC, coeffC]));
%% 3 Detect objects
%Detect ball
if w Red = frame-50;
else Red = frame*255; Ifr = Ifr*255; end
Red = Red(:,:,1)>Red(:,:,2)*1.5 & Red(:,:,1)>Red(:,:,3)*1.5 & Ifr>100 & Ifr<210;
Red = imopen(Red, strel('rectangle', [coeffO,coeffO]));
Red = imclose(Red, strel('rectangle', [coeffC,coeffC]));
%3a: detect blobs, return centroids, bounding boxes, eccentricity and
%diameter
[~,centr,bbox,diam,ecc] = step(blob,Red);
%3b: maximize for most round object
if ~isempty(centr)
[~,I] = min(ecc,[],1);
bbox = bbox(I,:);
centr = centr(I,:);
diam = diam(I)
end
%check if max is indeed round (i.e. if anything useful detected)
if ecc > 1
ecc = [];
centr = [];
bbox = [];
diam = [];
end
%Reconstruction
s = size(frame);
Reconstruction = zeros(s(1),s(2));
if ~isempty(centr)
m = [round(centr(2)),round(centr(1))]; %midpoint, note: centr = [x y], size = [y x]
if (m(1)>0 && m(2)>0 && m(1)<s(1) && m(2)<s(2))
%set center pixel to 1, create circle around it
Reconstruction(m(1),m(2))=1;
%Reconstruction = imdilate(Reconstruction,strel('disk', round(diam(1)/2*0.8),8));
%DOESNT WORK CURRENTLY %Reconstruction = bwdist(Reconstruction) <= round(diam/2);
%diam(1)
end
end
%% 4 Send the frame to the video players
Red = insertShape(1.*Red, 'Circle', [centr diam./2]);
Red = insertShape(1.*Red, 'Line', [s(2)/2,0,s(2)/2,s(1)]);
%step(videoPlayer, frame);
%outputPlayer2.Name = 'Reconstruction';
%step(outputPlayer2, Reconstruction);
outputPlayer.Name = 'Red Filter';
step(outputPlayer, Red);
nr = nr+1; %increment frame nr
%% 5 Output
if ~isempty(centr)
Out_vector = [centr(1,:) diam(1)];
else
Out_vector = [0 0 0];
end
if (d && h>0.8*Href)
%Call Dronecontrol functie
dronecamdata(drone,Out_vector(1),Out_vector(2),Out_vector(3));
end
end
release(videoPlayer);
videoPlayer.hide(); outputPlayer.hide(); %outputPlayer2.hide();
close all;
%done
disp('Stopping video analysis...');
if d
disp('--Drone is landing--');
%Land the drone
land(drone);
end
Terug naar: PRE2015_2_Groep2, videotracking.