MATLAB Videotracking code

From Control Systems Technology Group

Jump to: navigation, search

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.

Personal tools