
Subhabrata Bhattacharya CAP 5415 MIDTERM
1. In this problem we are required to create an edge detector and train it using the various edge patches we have
obtained from the Berkeley Segmentation Database [1]. We begin by creating oriented first and second order
derivative of the Gaussian 2D filter for different scales (σ= 1,2) and orientations measures (θ=π/4, π/2,3π/4).
In addition to these 12 Gaussian filters we also create another filter that is a square matrix of all 1s. Next we
convolve each of training image patches (with edges and non-edges) obtained from [1] to generate responses. These
responses are stored in a 13 dimensional feature vector.
1clear all;
2close all;
3clc;
4
5basedir = '/home/subh/courses/cap5415/ps3/edge ex amples s ubset/';
6etrain = 'EDGE TRAIN/';
7netrain = 'NOT EDGE TRAIN/';
8
9dirinfo = [basedir etrain];
10 fmetrain = buildFeatureMatrix (dirinfo, '*.png');
11
12 dirinfo = [basedir netrain];
13 fmnetrain = buildFeatureMatrix (dirinfo, '*.png');
14
15 pts = [fmetrain; fmnetrain];
16 labels = [fmetrain(:,13); −fmnetrain(: ,13)];
17 save('PtsLblsWts.mat','pts','labels');
Listing 1: Matlab script for Problem 1
1function fmatrix = buildFeatureMatrix (dirinfo, files)
2% This function creates a feature matrix for all files in the training
3% directory. number of theta and sigma could be increased/decreased to
4% increase the size of the feature matrix
5pi = 3.1415;
6theta = [pi/4 pi/2 3*(pi/4)];
7sigma = [1 2];
8
9flidx = dir([dirinfo files]);
10 nImg = length(flidx);
11
12 for icnt = 1:nImg
13 flname = [dirinfo flidx(icnt).name];
14 fvector = computeFeatureVector(flname, theta, sigma);
15 fmatrix(icnt,:) = fvector(:);
16 end
17 return;
Listing 2: Matlab script for Building Feature Matrices from the feature vectors
1function fvector = computeFeatureVector(flname, theta, sigma)
2% This function computes a feature vector of 12 dimensions for an image
3% patch. For each scale value and orientation value, the response of the
4% given image patch to both first and second directional derivative of
5% Gaussian filters is computed. flname is the absolute path of the file
6% containing the image patch.
7
8% img is 13 x 13 matrix
9img = imread (flname);
10 img = im2double (img);
11 fvector = ones(1,2*length(theta) *length(sigma) +1);
12 % Counting features
13 lcnt = 1;
14 % Create feature vector for each image
15 for jcnt = 1:length(theta) % for 3 theta (orientation)
16 for kcnt = 1:length(sigma) % for 2 sigma (scales)
17 [x y] = meshgrid(−6: 6); % Create a 13 x 13 template
18
19 var = sigma(kcnt)ˆ2;
20 tcos = cos(theta(jcnt));
1