forked from januhlenberg/Signa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 529a193
Showing
4 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
classdef BoundingBox < handle | ||
properties | ||
ID | ||
Untouched | ||
X | ||
Y | ||
Width | ||
Height | ||
Center | ||
identified | ||
history = []; | ||
end | ||
methods | ||
function obj = BoundingBox(bbox) | ||
obj.Untouched = 0; | ||
obj.X = bbox(1); | ||
obj.Y = bbox(2); | ||
obj.Width = bbox(3); | ||
obj.Height = bbox(4); | ||
obj.Center = [obj.X + obj.Width/2, obj.Y + obj.Height/2]; | ||
obj.identified = false; | ||
end | ||
function AddNext(obj,bbox) | ||
obj.history = [obj.history, bbox]; | ||
end | ||
function lc = GetLastCenter(obj) | ||
lc = obj.Center; | ||
if length(obj.history) > 0 | ||
lc = obj.history(length(obj.history)).Center; | ||
end | ||
end | ||
function [x y w h] = GetLastRect(obj) | ||
x = obj.X; | ||
y = obj.Y; | ||
w = obj.Width; | ||
h = obj.Height; | ||
if length(obj.history) > 0 | ||
x = obj.history(length(obj.history)).X; | ||
y = obj.history(length(obj.history)).Y; | ||
w = obj.history(length(obj.history)).Width; | ||
h = obj.history(length(obj.history)).Height; | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
classdef BoxTracker < handle | ||
properties | ||
idCounter = 1; | ||
boxes = []; | ||
identificationDistance = 50; | ||
deleteAfterUntouched = 10; | ||
track1 = [0 0 0]; | ||
track2 = [0 0 0]; | ||
track3 = [0 0 0]; | ||
ignoreStartsAfter = 200; | ||
end | ||
methods | ||
function obj = BoxTracker(track1, track2, track3) | ||
obj.track1 = track1; | ||
obj.track2 = track2; | ||
obj.track3 = track3; | ||
end | ||
|
||
function toIdentify = NewFrame(obj, bboxes) | ||
toIdentify = []; | ||
boxCount = size(bboxes,1); | ||
|
||
% Increment untouched | ||
for i=1:length(obj.boxes) | ||
obj.boxes(i).Untouched = obj.boxes(i).Untouched + 1; | ||
end | ||
|
||
for i=1:boxCount | ||
b = BoundingBox(bboxes(i,:)); | ||
|
||
found = false; | ||
minIndex = 0; | ||
minDistance = 100000000; | ||
% Is bin list? | ||
for j=1:length(obj.boxes) | ||
n = norm(double(obj.boxes(j).GetLastCenter() - b.Center)); | ||
if n < minDistance && n < obj.identificationDistance | ||
minIndex =j; | ||
minDistance = n; | ||
end | ||
end | ||
|
||
if minIndex > 0 | ||
obj.boxes(minIndex).AddNext(b); | ||
obj.boxes(minIndex).Untouched = 0; | ||
found = true; | ||
end | ||
|
||
if found == false && b.Y < obj.ignoreStartsAfter | ||
b.ID = obj.idCounter; | ||
obj.idCounter = obj.idCounter + 1; | ||
obj.boxes = [obj.boxes b]; | ||
end | ||
|
||
|
||
end | ||
|
||
keep = []; | ||
del = []; | ||
idenfify = []; | ||
for i=1:length(obj.boxes) | ||
[a,b,c,d] = obj.boxes(i).GetLastRect(); | ||
|
||
bottom = b + d; | ||
centerX = a+c/2; | ||
|
||
% check of bottom | ||
trackNo = 0; | ||
isLeftTrack = centerX > obj.track1(2) && centerX < obj.track1(3); | ||
isCenterTrack = centerX > obj.track2(2) && centerX < obj.track2(3); | ||
isRightTrack = centerX > obj.track3(2) && centerX < obj.track3(3); | ||
checkBottom = 100000; | ||
|
||
if isLeftTrack | ||
trackNo = 1; | ||
checkBottom = obj.track1(1); | ||
elseif isCenterTrack | ||
trackNo = 2; | ||
checkBottom = obj.track2(1); | ||
else | ||
trackNo = 3; | ||
checkBottom = obj.track3(1); | ||
end | ||
|
||
if(bottom > checkBottom && ... | ||
bottom < 470 && ... | ||
obj.boxes(i).Untouched < obj.deleteAfterUntouched) | ||
if(obj.boxes(i).identified == false) | ||
v = [trackNo a b c d]; | ||
obj.boxes(i).identified = true; | ||
toIdentify = vertcat(toIdentify,v); | ||
end | ||
keep = [keep i]; | ||
elseif obj.boxes(i).Untouched >= obj.deleteAfterUntouched | ||
del = [del i]; | ||
else | ||
keep = [keep i]; | ||
end | ||
end | ||
obj.boxes = obj.boxes(keep); | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
saveRoot = "C:\Users\Jan\Desktop\Training\CarImages\"; | ||
files = getAllFiles('C:\Users\Jan\Desktop\Training\17343\04\41'); | ||
%files = getAllFiles('C:\Users\Jan\Desktop\Training\17343'); | ||
%files = getAllFiles('C:\Users\Jan\Desktop\42'); | ||
laneSepMask = rgb2gray(imread('seplanes.jpg')) < 100; | ||
|
||
% --------- Track Identification ----------- | ||
trackLeftY = 380; | ||
trackleftX1= 0; | ||
trackleftX2 = 250; | ||
|
||
trackMidY = 370; | ||
trackMidX1= 250; | ||
trackMidX2 = 520; | ||
|
||
trackRightY = 220; | ||
trackRightX1= 520; | ||
trackRightX2 = 680; | ||
% ------------------------------------ | ||
|
||
|
||
|
||
file_count = length(files); | ||
findex = 1; | ||
next = imread(fullfile(char(files(findex)))); | ||
findex = findex+1; | ||
|
||
|
||
opticFlow = opticalFlowFarneback; | ||
opticFlow.NeighborhoodSize=5; | ||
opticFlow.FilterSize = 25; | ||
|
||
flow = estimateFlow(opticFlow,rgb2gray(next)); | ||
|
||
% ---------- Shadow ----------- | ||
mgp = [0.000204345345141426]; | ||
SigmaInv = [0.104952610092160,-0.119012800639940,0.0224817048646620;-0.119012800639940,0.204757787612976,-0.0862513240676164;0.0224817048646620,-0.0862513240676164,0.0591157280007801]; | ||
% ---------------------------- | ||
|
||
blob = vision.BlobAnalysis(... | ||
'CentroidOutputPort', false, ... | ||
'AreaOutputPort', false, ... | ||
'BoundingBoxOutputPort', true, ... | ||
'MinimumBlobAreaSource', 'Property', ... | ||
'MinimumBlobArea', 5000, ... | ||
'MaximumBlobArea', 640*480/4); | ||
shapeInserter = vision.ShapeInserter('BorderColor','White'); | ||
|
||
hdinterlacer = vision.Deinterlacer; | ||
|
||
tracker = BoxTracker([trackLeftY trackleftX1 trackleftX2], ... | ||
[trackMidY trackMidX1 trackMidX2], ... | ||
[trackRightY trackRightX1 trackRightX2]); | ||
carImg = []; | ||
|
||
while findex <= file_count | ||
% progression | ||
findex / file_count * 100 | ||
|
||
nextFile = fullfile(char(files(findex))); | ||
prev = next; | ||
|
||
next = imread(nextFile); | ||
%next = hdinterlacer(next); | ||
nextGray = rgb2gray(next); | ||
findex = findex+1; | ||
|
||
flow = estimateFlow(opticFlow,nextGray); | ||
|
||
myflow = flow.Magnitude; | ||
s = 2; | ||
myflow(flow.Magnitude < s) = 0; | ||
myflow(flow.Magnitude >= s) = 1; | ||
myflow(flow.Vy <0) = 0; | ||
myflow(laneSepMask) = 0; | ||
|
||
% --------Shadow ------------------- | ||
%{ | ||
b = size(next,2); | ||
h = size(next,1); | ||
shadowMask = zeros(h,b); | ||
for y=1:h | ||
for x=1:b | ||
px = double([next(y,x,1) next(y,x,2) next(y,x,3)])'; | ||
pxm =px-m; | ||
gx = mgp*exp(-.5*(pxm)'*SigmaInv*pxm); | ||
if(gx > 0.00000001) | ||
%next(y,x,1) = 255; | ||
%next(y,x,2) = 0; | ||
%next(y,x,3) = 0; | ||
shadowMask(y,x) = 1; | ||
end | ||
end | ||
end | ||
% next(shadowMask == 1) = 255; | ||
%} | ||
%------------------------------------------------------------- | ||
|
||
%imshow(shadowMask) ; | ||
%myflow(shadowMask == 1) = 0; | ||
%imshow(myflow); | ||
|
||
bbox = step(blob, myflow > 0); | ||
|
||
|
||
% Analyse boxes | ||
cti = tracker.NewFrame(bbox); | ||
|
||
|
||
for i=1:size(cti,1) | ||
carImg = imcrop(next,[cti(i,2) cti(i,3) cti(i,4) cti(i,5)]); | ||
imwrite(carImg,fullfile(char( strcat(strcat(saveRoot, strrep(nextFile(end-15:end-4),"\","_"), "-" + string(cti(i,1)) + ".jpg"))))); | ||
end | ||
|
||
% ============== Visualisation =========== | ||
|
||
out = step(shapeInserter, next, bbox); | ||
|
||
% Redraw frames | ||
for i=1:length(tracker.boxes) | ||
out = insertShape(out,'Line',[tracker.boxes(i).Center tracker.boxes(i).GetLastCenter()],'LineWidth',5,'Color','Red'); | ||
end | ||
|
||
% Draw bounding rects | ||
out = insertShape(out,'Line',[trackleftX1 trackLeftY trackleftX2 trackLeftY; | ||
trackMidX1 trackMidY trackMidX2 trackMidY; | ||
trackRightX1 trackRightY trackRightX2 trackRightY;],'LineWidth',5,'Color','Blue'); | ||
|
||
subplot(1,3,1), imshow(out); | ||
|
||
%hold on | ||
%plot(flow,'DecimationFactor',[5 5],'ScaleFactor',2); | ||
%hold off | ||
subplot(1,3,2), imshow(myflow); | ||
subplot(1,3,3), imshow(carImg); | ||
pause(0.05); | ||
|
||
end | ||
|
||
|
||
|
||
function fileList = getAllFiles(dirName) | ||
|
||
dirData = dir(dirName); %# Get the data for the current directory | ||
dirIndex = [dirData.isdir]; %# Find the index for directories | ||
fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files | ||
if ~isempty(fileList) | ||
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files | ||
fileList,'UniformOutput',false); | ||
end | ||
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories | ||
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories | ||
%# that are not '.' or '..' | ||
for iDir = find(validIndex) %# Loop over valid subdirectories | ||
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path | ||
fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles | ||
end | ||
end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.