Last active
October 14, 2015 06:10
-
-
Save chenigodwin/7307e4b1fc1db5208ff9 to your computer and use it in GitHub Desktop.
Dedan Kimathi University,5th year project.Automatic Traffic Data Collection and Analysis Using Optical Camera and Sensor Image Recognition
This file contains hidden or 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
% PART A | |
%linking camera with rasperry pi | |
%creating a camera board object | |
clear rpi; | |
rpi= raspi(); | |
cam = cameraboard(rpi,'Resolution','640x480'); | |
%PART B | |
% code to start the camera and to capture image | |
Clear all | |
Close all | |
clc | |
%camera integration | |
webcam list | |
ans ='webcam 1'; | |
'webcam 2'; | |
cam= webcam(2); | |
%from the given list webcam is choosen | |
button=input('would you like to open the camera?(y\n)!','s'); | |
if strcmp(button,'v') | |
disp(button) | |
end | |
%on pressing the v button the camera and sensor are actuated to take photo | |
im = snapshot(cam); | |
preview (cam); | |
%PART C | |
%image filtering | |
I= imread('im'); | |
BW = imread('im'); | |
mask = BW(1:256,1:256); | |
f = @(x) imadjust(x,[],[],0.3); | |
img1 = roifilt2(I,mask,f); | |
figure,imshow(img1) | |
imwrite(img1,'C:\Users\GODWIN\Desktop\Project\1.jpg'); | |
%PART D | |
%calling comparitory images in the database | |
L = list_files(path_fn); | |
% get information about given path_fn | |
L = dir (path_fn); | |
% ... ignore . and .. | |
L = L(3: length (L)); | |
% ... turn into a cell array | |
L = struct2cell(L); | |
% ... and only keep the filenames . | |
L = L(1,:); | |
fn (X,y:width,height) = read_images(path_fn) | |
% get files for a given path | |
folder = list_files(path_fn); | |
% initialize the empty return values | |
X=[]; | |
y=[]; | |
width=0; | |
height=0; | |
% start counting with class index 1 | |
classIdx = 1; | |
% for each file ... | |
for i=1: length (folder) | |
subject = folder{i}; | |
% ... get files in this subdir | |
images = list_files([path_fn , filesep , subject]); | |
% ... ignore a file or empty folder | |
if ( length(images) == 0) | |
continue; | |
end | |
% ... for each image | |
for j=1: length (images) | |
% ... get the absolute path | |
filename = [path_fn , filesep , subject , filesep , images{j}]; | |
% ... read the image | |
T = double(imread(filename)); | |
% ... get the image information | |
[height width channels] = size (T); | |
% ... and grayscale if it ' s a color image | |
if (channels == 3) | |
T = 0.2989 * T(:,:,1) + 0.5870* T(:,:,2) + 0.1140 * T(:,:,3); | |
end | |
% ... reshape into a row vector and append to data matrix | |
X = [X; reshape (T,1,width*height)]; | |
% ... append the corresponding class to the class vector | |
y = [y, classIdx]; | |
end | |
% ... increase the class index | |
classIdx = classIdx + 1; | |
end | |
% ... for - each folder . | |
fn [W, mu] = pca(X, y, k) | |
[n,d] = size (X); | |
mu = mean (X); | |
Xm = X - repmat(mu, rows(X), 1); | |
if (n>d) | |
C = Xm'*Xm; | |
[W,D] = eig (C); | |
% sort eigenvalues and eigenvectors | |
[D, i] = sort ( diag (D), ' descend ' ); | |
W = W(:,i); | |
% keep k components | |
W = W(:,1:k); | |
else | |
C = 'Xm*Xm '; | |
% C = cov ( Xm ') ; | |
[W,D] = eig (C); | |
% multiply with data matrix | |
W = Xm'*W; | |
% normalize eigenvectors | |
for i=1:n | |
W(:,i) = W(:,i)/ norm (W(:,i)); | |
end | |
% sort eigenvalues and eigenvectors | |
[D, i] = sort ( diag (D), ' descend ' ); | |
W = W(:,i); | |
% keep k components | |
W = W(:,1:k); | |
end | |
fn Y = project(W, X, mu) | |
if ( nargin <3) | |
Y = X*W; | |
else | |
Y = (X-repmat(mu, rows(X), 1))*W; | |
end | |
fn X = reconstruct(W, Y, mu) | |
if ( nargin <3) | |
X = Y * W'; | |
else | |
X = Y * W' + repmat(mu, rows(Y), 1); | |
end | |
% load function files from subfolders aswell | |
addpath (genpath ( ' . ' )); | |
% read images | |
[X,y,w,h] = read_images('C:\Users\GODWIN\Desktop\Project\vehicle_database'); | |
% n - number of samples | |
% d - dimensionality | |
[n,d] = size (X); | |
% perform a full pca | |
[W,mu] = pca(X,y,n); | |
fn X = normalize(X, l, h) | |
minX = min (X(:)); | |
maxX = max (X(:)); | |
% % Normalize to [0...1]. | |
X = X - minX; | |
X = X ./ (maxX - minX); | |
% % Scale to [ low ... high ]. | |
X = X .* (h-l); | |
X = X + l; | |
fn Y = toGrayscale(X, width , height) | |
Y = normalize(X, 0, 255); | |
if ( nargin ==3) | |
Y = reshape (Y, height , width); | |
end | |
Y = uint8(Y); | |
% plot eigenvehicles | |
figure ; hold on; | |
title ( ' Eigenvehicles ( AT & T Vehicledatabase ) ' ); | |
for i=1: min (16,n) | |
subplot (4,4,i); | |
eigenface_i = toGrayscale(W(:,i), w, h); | |
imshow(eigenface_i); | |
colormap ( jet (256)); | |
title ( sprintf ( ' Eigenvehicle #% i ' , i)); | |
end | |
% plot eigenvehicles reconstruction | |
steps = 10:20: min (n,320); | |
Q = X(1,:); % first image to reconstruct | |
figure ; hold on; | |
title ( ' Reconstruction ( AT & T Vehicledatabase ) ' ); | |
for i=1: min (16, length (steps)) | |
subplot (4,4,i); | |
numEvs = steps(i); | |
P = project(W(:,1:numEvs), X(1,:), mu); | |
R = reconstruct(W(:,1:numEvs),P,mu); | |
comp = toGrayscale(R, w, h); | |
imshow(comp); | |
title ( sprintf ( ' % i Eigenvectors ' , numEvs)); | |
end | |
fn [W, mu] = lda(X,y,k) | |
% dimension of observation | |
[n,d] = size (X); | |
% number of classes | |
labels = unique(y); | |
C = length (labels); | |
% allocate scatter matrices | |
Sw = zeros (d,d); | |
Sb = zeros (d,d); | |
% total mean | |
mu = mean (X); | |
% calculate scatter matrices | |
for i = 1:C | |
Xi = X( find (y == labels(i)) ,:); | |
% samples for current class | |
n = rows(Xi); | |
mu_i = mean (Xi); | |
% mean vector for current class | |
Xi = Xi - repmat(mu_i , n, 1); | |
Sw = Sw + Xi*Xi; | |
Sb = Sb + n * (mui - mu)*(mui - mu); | |
end | |
% solve general eigenvalue problem | |
[W, D] = eig (Sb, Sw); | |
% sort eigenvectors | |
[D, i] = sort ( diag (D), ' descend ' ); | |
W = W(:,i); | |
% keep at most ( c -1) eigenvectors | |
W = W(:,1:k); | |
fn [W, mu] = fishervehicles(X,y,k) | |
% number of samples | |
N = rows(X); | |
% number of classes | |
labels = unique(y); | |
c = length (labels); | |
if ( nargin < 3) | |
k = c-1; | |
end | |
k = min (k,(c-1)); | |
% get ( N - c ) principal components | |
[Wpca , mu] = pca(X, y, (N-c)); | |
[Wlda , mu_lda] = lda(project(Wpca , X, mu), y, k); | |
W = Wpca*Wlda; | |
% load function files from subfolders aswell | |
addpath (genpath ( ' . ' )); | |
% for plotting | |
db_name = ' lorry vehicledatabase ' ; | |
% read images | |
[X,y,w,h] = read_images( 'C:\Users\GODWIN\Desktop\Project\vehicledatabase' ); | |
% n - number of samples | |
% d - dimensionality | |
[n,d] = size (X); | |
% get the unique classes | |
c = unique(y); | |
% compute the fishervehicles | |
[W,mu] = fisherfaces(X,y); | |
% plot fishervehicle | |
figure ; hold on; | |
title ( sprintf ( ' Fishervehicle % s ' , db_name)); | |
for i=1: min (16, length (c) -1) | |
subplot (4,4,i); | |
fishervehicle_i = toGrayscale(W(:,i), w, h); | |
imshow(fishervehicle_i); | |
colormap ( jet (256)); | |
title ( sprintf ( ' Fishervehicle #% i ' , i)); | |
end | |
steps = 1: min (16, length (c) -1); | |
Q = X(1,:); % first image to reconstruct | |
figure ; hold on; | |
title ( sprintf ( ' Fishervehicle Reconstruction % s ' , db_name)); | |
for i=1: min (16, length (steps)) | |
subplot (4,4,i); | |
numEv = steps(i); | |
P = project(W(:,numEv), X(1,:), mu); | |
R = reconstruct(W(:,numEv),P,mu); | |
comp = toGrayscale(R, w, h); | |
imshow(comp); | |
title ( sprintf ( ' Fishervehicle #% i ' , numEv)); | |
end | |
ELSEIF strcmp(button,'n') | |
disp(button) | |
disp('obtain traffic analysis output in the spreadsheet') | |
Valid object handles must be supplied | |
==end | |
%Published with MATLAB® 7.12 |
Thank you for your comment @kenju254.I am working on that now.
@chenigodwin Could you add a Whitespace after each comment .
This is awesome, I'm looking forward to seeing the output of the code
Thank you guys,am looking into your recommendations.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add spacing after every commented line .