Skip to content

Commit

Permalink
Merge pull request #1 from raviBhadeshiya/improvement
Browse files Browse the repository at this point in the history
Improvement
  • Loading branch information
Ravi Bhadeshiya authored May 15, 2017
2 parents 9069021 + 4e26413 commit 57c1dba
Show file tree
Hide file tree
Showing 35 changed files with 833 additions and 397 deletions.
Binary file added CarTracking/Report.pdf
Binary file not shown.
26 changes: 6 additions & 20 deletions CarTracking/code/main.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
clear all; close all; clc;

%%
cd ../input;
videoFReader = vision.VideoFileReader('simple.avi');
carDetector=vision.CascadeObjectDetector('cars.xml');
carDetector=vision.CascadeObjectDetector('cascadeClassifier.xml');
cd ../code;

videoFWriter = vision.VideoFileWriter('../Output/output.mp4','FileFormat','MPEG4');
%%
carDetector.MergeThreshold=1;
carDetector.MinSize=[50 50];
Expand All @@ -14,21 +13,14 @@
boundingBox=step(carDetector, videoFrame);
carAnotate=insertObjectAnnotation(videoFrame, 'rectangle', boundingBox, 'Car');
imshow(carAnotate);

%%
videoPlayer = vision.VideoPlayer('Position',...
[100 100 [size(videoFrame, 2), size(videoFrame, 1)]+30]);

%%
for i=1:size(boundingBox,1)

pointTracker{i} = vision.PointTracker('MaxBidirectionalError',2);
points = detectSURFFeatures(rgb2gray(videoFrame),'ROI', boundingBox(i,:), 'MetricThreshold', 300, 'NumOctaves', 20);

% points = detectHarrisFeatures(rgb2gray(videoFrame),'ROI', boundingBox(i,:), 'MinQuality', 0.00005);
% points = detectMSERFeatures(rgb2gray(videoFrame),'ROI', boundingBox(i,:),'MaxAreaVariation',0.8);
% points = detectBRISKFeatures(rgb2gray(videoFrame),'ROI', boundingBox(i,:));

initialize(pointTracker{i},points.Location,videoFrame);
bboxPoints{i} = bbox2points(boundingBox(i,:));

Expand All @@ -51,20 +43,18 @@
oldInliers = [oldInliers; oldPoints{j}(isFound, :)];

end
% if size(visiblePoints{j}, 1) >= 2 % need at least 2 points

% Estimate the geometric transformation between the old points
% and the new points and eliminate outliers
[xform, oldInliers, visiblePointsList] = estimateGeometricTransform(...
oldInliers, visiblePointsList, 'similarity', 'MaxDistance', 1);

for j=1:length(pointTracker)
% Apply the transformation to the bounding box points
bboxPoints{j} = transformPointsForward(xform, bboxPoints{j});

% Insert a bounding box around the object being tracked
bboxPolygon = reshape(bboxPoints{j}', 1, []);
videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, ...
'LineWidth', 2, 'Color', [255 255*(j-1) 255*(j-2)]);
'LineWidth', 2, 'Color', [255*(j-1) 255*(j-2) 255*(mod(j,2))]);

% Display tracked points
videoFrame = insertMarker(videoFrame, visiblePoints{j}, '+', ...
Expand All @@ -73,16 +63,12 @@
oldPoints{j} = visiblePoints{j};
setPoints(pointTracker{j}, oldPoints{j});
end

% Display the annotated video frame using the video player object
step(videoPlayer, videoFrame);
step(videoFWriter,videoFrame);
end



% Clean up
release(videoFReader);
release(videoPlayer);
release(videoFWriter);
for i=1:length(pointTracker)
release(pointTracker{i});
end
15 changes: 15 additions & 0 deletions CarTracking/code/modifyDataset.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
clc; clear all; close all;warning('off');

index = 0;
trainFolder = '..\Input\vehicles\myposFIles\';
negativeFolder = fullfile('..\Input\vehicles');

negativeImages = imageDatastore(negativeFolder,'IncludeSubfolders',true);

negativeStruct = struct(negativeImages);

for i =1:negativeStruct.NumFiles
img = imread(char(negativeStruct.Files(i)));
rimg=flip(img,2);
imwrite(rimg,fullfile(trainFolder,sprintf('file_%d.png',i)),'png');
end
19 changes: 19 additions & 0 deletions CarTracking/code/trainingClassifier.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
clc;clear all; close all; warning('off');
%%
negativeFolder = fullfile('..\Input\non-vehicles\Extras\');

positiveFolder = fullfile('..\Input\vehicles\');

negativeImages = imageDatastore(negativeFolder,'IncludeSubfolders',true);
positiveImages = imageDatastore(positiveFolder,'IncludeSubfolders',true);

value = [1 1 64 64];

positiveStruct = struct(positiveImages);
positiveImg(:,1) = cell2table(positiveStruct.Files);
for i =1:positiveStruct.NumFiles
positiveImg(i,2) = table(value);
end
%%
trainCascadeObjectDetector('cascadeClassifier.xml',positiveImg, ...
negativeFolder,'FalseAlarmRate',0.05,'NumCascadeStages',10);
Binary file added CarTracking/output/output.mp4
Binary file not shown.
Binary file added VisualOdometry/Report.pdf
Binary file not shown.
38 changes: 38 additions & 0 deletions VisualOdometry/code/DecomposeCameraMatrix.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function [k,r,c] = DecomposeCameraMatrix(p)
% Descompose the camera matrix P into K, R, C according P = K [R |-RC]
%
% Input:
% - p is a 3x4 camera matrix.
%
% Output:
% - k is a 3x3 calibration matrix.
% - r is a 3x3 rotation matrix.
% - c is a 3x1 camera coordinates.
%
%----------------------------------------------------------
%
% From
% Book: "Multiple View Geometry in Computer Vision",
% Authors: Hartley and Zisserman, 2006, [HaZ2006]
% Section: "Decomposition of the camera matrix",
% Chapter: 6
% Page: 163
%
%----------------------------------------------------------
% Author: Diego Cheda
% Affiliation: CVC - UAB
% Date: 03/06/2008
%----------------------------------------------------------

% Finding camera centre
% PC = 0, right null vector of P.
x = det([ p(:,2), p(:,3), p(:,4) ]);
y = -det([ p(:,1), p(:,3), p(:,4) ]);
z = det([ p(:,1), p(:,2), p(:,4) ]);
t = -det([ p(:,1), p(:,2), p(:,3) ]);

c = [ x/t; y/t; z/t ];

% Finding camera orientation and internal parameters
% P = [M | -MC] = K[R | -RC]
[k,r] = RQDecomposition(p(:,1:3));
39 changes: 39 additions & 0 deletions VisualOdometry/code/DepthOfPoints.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function d = DepthOfPoints(x3D,rot,t)
% Computes the depth of points
%
% Input:
% - x3D are the 3D points.
%
% - rot is a 3x3 rotation matrix
%
% - t is a 3x1 traslation matrix.
%
% Output:
% - d is a 1xn vector with the depth of points.
%
%----------------------------------------------------------
%
% From
% Book: "Multiple View Geometry in Computer Vision",
% Authors: Hartley and Zisserman, 2006, [HaZ2006]
% Section: "Depth of points",
% Chapter: 6
% Page: 162
%
%----------------------------------------------------------
% Author: Diego Cheda
% Affiliation: CVC - UAB
% Date: 11/06/2008
%----------------------------------------------------------

[k,r,c] = DecomposeCameraMatrix([rot t]);

x3D = HomogeneousCoordinates(x3D,'3D');

for i=1:size(x3D,2)
w = rot(3,:) * (x3D(1:3,i) - c(1:3,:));

depth = (sign(det(rot)) * w) / x3D(4,i) * norm(rot(3,:));

d(i) = depth(1,1);
end
12 changes: 12 additions & 0 deletions VisualOdometry/code/DisambiguateCameraPose.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function [C, R, X] = DisambiguateCameraPose(Cset, Rset, Xset)

for i = 1 : length(Cset)
D = Rset{i}*(Xset{i}'-Cset{i}*ones(1,size(Xset{i},1)));
n(i) = length(find(D(3,:)>0 & Xset{i}(:,3)'>0));
end

[~, maxi] = min(n);
C = Cset{maxi};
R = Rset{maxi}';
X = Xset{maxi};
end
79 changes: 79 additions & 0 deletions VisualOdometry/code/EssentialMatrixFrom2DPoints.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
function e = EssentialMatrixFrom2DPoints(p1,p2,k)
%
% Input:
% p1 and p2 are 2xN (or 3xN in homogeneous coordiantes) of
% correspondings corners.
%
% k is the camera matrix.
%
% Output:
% e is the essential matrix.
%
%----------------------------------------------------------
%
% From
% Book: "Multiple View Geometry in Computer Vision",
% Authors: Hartley and Zisserman, 2006, [HaZ2006]
% Section: "Computation of the Fundamental Matrix F", specifically "The
% normalised 8-point algorithm"
% Chapter: 11
% Page: 279
%
%----------------------------------------------------------
% Author: Diego Cheda
% Affiliation: CVC - UAB
% Date: 03/06/2008
%----------------------------------------------------------

% Points in Normalized Coordinates. p = inv(k) * p
p1 = NormalizedCoordinates(p1,k);
p2 = NormalizedCoordinates(p2,k);

% Normalization
% Transform the image coordinates according to x^_i = Tx_i and x'^_i =
% T'x'_i where T and T' are normalizing transformation consisting of a
% translation and scaling.
% [p1,t1] = Normalise2DPts(p1);
% [p2,t2] = Normalise2DPts(p2);
[p1,t1] = normalise2dpts(p1);
[p2,t2] = normalise2dpts(p2);


% (x,y)
x1 = p1(1,:)';
x2 = p2(1,:)';

y1 = p1(2,:)';
y2 = p2(2,:)';


% Number of points
numPts = size(p1,2);

% Af = 0
% Computes A, the constraint matrix of numpts x 9
a = [x2.*x1 x2.*y1 x2 y2.*x1 y2.*y1 y2 x1 y1 ones(numPts,1)];
%a = [x1.*x2 x1.*y2 x1 y1.*x2 y1.*y2 y1 x2 y2 ones(numPts,1)];

if (rank(a)<8)
disp('rank defficient');
end


% Singular Value Decomposition of A.
% [U,S,V] = SVD(A) produces a diagonal matrix S, of the same dimension as A
% and with nonnegative diagonal elements in decreasing order, and unitary
% matrices U and V so that A = U*S*V'.
[u, d, v] = svd(a);

% Linear solution. Obtain F from 9th column of V (the smallest singular
% value of A).
e = reshape(v(:,9),3,3)';

% Constraint enforcement.
[u, d, v] = svd(e);
d = diag([1 1 0]);
e = u * d * v';

% Desnormalization
e = t2' * e * t1;
16 changes: 0 additions & 16 deletions VisualOdometry/code/EssentialMatrixFromFundamentalMatrix.m

This file was deleted.

65 changes: 65 additions & 0 deletions VisualOdometry/code/EssentialMatrixToCameraMatrix.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
function [rot,t] = EssentialMatrixToCameraMatrix(e)
% Extracts the cameras from the essential matrix
%
% Input:
% - e is the 3x4 essential matrix.
%
% Output:
% - p1 = [I | 0] is the first canonic camera matrix.
%
% - rot and t are the rotation and translation of the
% second camera matrix from 4 possible solutions. One camera matrix
% must be selected. We test with a single point to determine if it is
% in front of both cameras is sufficient to decide between the four
% different solutions for the camera matrix (pag. 259).
%
%----------------------------------------------------------
%
% From
% Book: "Multiple View Geometry in Computer Vision",
% Authors: Hartley and Zisserman, 2006, [HaZ2006]
% Section: "Extraction of cameras from the essential matrix",
% Chapter: 9
% Page: 258
%
%----------------------------------------------------------
% Author: Diego Cheda
% Affiliation: CVC - UAB
% Date: 03/06/2008
%----------------------------------------------------------


% Decompose the matrix E by svd
[u, s, v] = svd(e);

%
w = [0 -1 0; 1 0 0; 0 0 1];
z = [0 1 0; -1 0 0; 0 0 1];

%
% E = SR where S = [t]_x and R is the rotation matrix.
% E can be factorized as:
%s = u * z * u';

% Two possibilities:
rot1 = u * w * v';
rot2 = u * w' * v';

% Two possibilities:
t1 = u(:,3) ./max(abs(u(:,3)));
t2 = -u(:,3) ./max(abs(u(:,3)));


% 4 possible choices of the camera matrix P2 based on the 2 possible
% choices of R and 2 possible signs of t.
rot(:,:,1) = rot1;
t(:,:,1) = t1;

rot(:,:,2) = rot2;
t(:,:,2) = t2;

rot(:,:,3) = rot1;
t(:,:,3) = t2;

rot(:,:,4) = rot2;
t(:,:,4) = t1;
Loading

0 comments on commit 57c1dba

Please sign in to comment.