Skip to content

Commit 71f8d4e

Browse files
committed
added vision code, exercises and reports
1 parent c0fc397 commit 71f8d4e

File tree

207 files changed

+4344
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+4344
-1
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
# vision
1+
# Computer Vision 2016
22
Code and reports for the Computer Vision course of 2016 at ETH Zurich
3+
4+
## Disclaimer
5+
Please be advised that I don't guarantee completeness or correctness of anything found here.

ex01/Exercise1.pdf

1.69 MB
Binary file not shown.

ex01/code/exercise2.m

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
% Exercise 3
2+
%
3+
close all;
4+
5+
%
6+
% COMMENT/UNCOMMENT following lines to select image
7+
%
8+
9+
%default images
10+
IMG_NAME1 = 'images/I1.jpg';
11+
IMG_NAME2 = 'images/I2.jpg';
12+
13+
%own images1
14+
% IMG_NAME1 = 'images/cv1.jpg';
15+
% IMG_NAME2 = 'images/cv2.jpg';
16+
17+
%own images2
18+
% IMG_NAME1 = 'images/cv1.jpg';
19+
% IMG_NAME2 = 'images/cv2.jpg';
20+
21+
22+
%threshold dependent on image
23+
%I1/I2=0.02,SW1/SW2=0.3, CV1/CV2=0.45
24+
switch IMG_NAME1
25+
case 'images/I1.jpg'
26+
thresh = 0.02;
27+
case 'images/sw1.jpg'
28+
thresh = 0.3;
29+
case 'images/cv1.jpg'
30+
thresh = 0.45;
31+
end
32+
33+
% read in image
34+
img1 = im2double(imread(IMG_NAME1));
35+
img2 = im2double(imread(IMG_NAME2));
36+
37+
img1 = imresize(img1, 0.5);
38+
img2 = imresize(img2, 0.5);
39+
40+
% convert to gray image
41+
imgBW1 = rgb2gray(img1);
42+
imgBW2 = rgb2gray(img2);
43+
44+
45+
% Task 3.1 - extract Harris corners
46+
[corners1, H1, allcorners1] = extractHarrisCorner(imgBW1');
47+
[corners2, H2, allcorners2] = extractHarrisCorner(imgBW2');
48+
49+
% show images with Harris corners
50+
showImageWithCorners(img1, allcorners1, 8);
51+
showImageWithCorners(img2, allcorners2, 9);
52+
53+
% show images with Harris corners using non maximum suppression
54+
showImageWithCorners(img1, corners1, 10);
55+
showImageWithCorners(img2, corners2, 11);
56+
57+
% Task 3.2 - extract your own descriptors
58+
descr1 = extractDescriptor(corners1, imgBW1');
59+
descr2 = extractDescriptor(corners2, imgBW2');
60+
61+
% Task 3.3 - match the descriptors
62+
matches = matchDescriptors(descr1, descr2, thresh);
63+
64+
% In case match descriptors does not find any matches with the given
65+
% threshold just plot the features
66+
if size(matches) == 0
67+
showNoFeatureMatches(img1, corners1, img2, corners2, 15);
68+
else
69+
showFeatureMatches(img1, corners1(:, matches(1,:)), img2, corners2(:, matches(2,:)), 20);
70+
end
71+
72+
% Task 1.4 SIFT features
73+
sift(img1, img2, imgBW1, imgBW2, 0.02, 7, 40);

ex01/code/extractDescriptor.m

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
% extract descriptor
2+
%
3+
% Input:
4+
% keyPoints - detected keypoints in a 2 x n matrix holding the key
5+
% point coordinates
6+
% img - the gray scale image
7+
%
8+
% Output:
9+
% descr - w x n matrix, stores for each keypoint a
10+
% descriptor. m is the size of the image patch,
11+
% represented as vector
12+
function descr = extractDescriptor(corners, img)
13+
[~,n] = size(corners);
14+
descr = zeros(9*9,n);
15+
16+
%descriptor of 9*9 patch organized [1 2 3; 4 5 6; 7 8 9] into column
17+
%vector
18+
for i=1:n
19+
pos = corners(:,i);
20+
w = img(pos(1)+(-4:4),pos(2)+(-4:4));
21+
descr(:,i) = w(:);
22+
end
23+
24+
25+
end

ex01/code/extractHarrisCorner.m

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
% extract harris corner
2+
%
3+
% Input:
4+
% img - n x m gray scale image
5+
% thresh - scalar value to threshold corner strength
6+
%
7+
% Output:
8+
% corners - 2 x k matrix storing the keypoint coordinates
9+
% H - n x m gray scale image storing the corner strength
10+
function [corners, H, allcorners] = extractHarrisCorner(img)
11+
[n,m] = size(img);
12+
13+
%blur to get rid of noise
14+
blur = fspecial('gaussian');
15+
img = imfilter(img,blur);
16+
17+
%calculate gradients
18+
gx = zeros(n-1, m-1);
19+
gy = zeros(n-1, m-1);
20+
21+
for i=2:m-1
22+
gx(1:(n-1),i) = (img(1:(n-1),i+1) - img(1:(n-1),i-1))/2;
23+
end
24+
for i=2:n-1
25+
gy(i,1:(m-1)) = (img(i+1,1:(m-1)) - img(i-1,1:(m-1)))/2;
26+
end
27+
28+
%calculate for every pixel 2x2 matrix [I_x^2 I_x*I_y; I_x*I_y I_y^2]
29+
Ix2 = zeros(n-1, m-1);
30+
Ixy = zeros(n-1, m-1);
31+
Iy2 = zeros(n-1, m-1);
32+
33+
Ix2 = gx.^2;
34+
Ixy = gx.*gy;
35+
Iy2 = gy.^2;
36+
37+
38+
% calculate harris response in 3x3 patch around every pixel, the sum
39+
% represents Harris matrix. K is the Harris response for every pixel.
40+
K = zeros(n,m);
41+
for i=2:n-2
42+
for j=2:m-2
43+
I11 = [Ix2(i-1,j-1) Ixy(i-1,j-1); Ixy(i-1,j-1) Iy2(i-1,j-1)];
44+
I12 = [Ix2(i-1,j) Ixy(i-1,j); Ixy(i-1,j) Iy2(i-1,j)];
45+
I13 = [Ix2(i-1,j+1) Ixy(i-1,j+1); Ixy(i-1,j+1) Iy2(i-1,j+1)];
46+
I21 = [Ix2(i,j-1) Ixy(i,j-1); Ixy(i,j-1) Iy2(i,j-1)];
47+
I22 = [Ix2(i,j) Ixy(i,j); Ixy(i,j) Iy2(i,j)];
48+
I23 = [Ix2(i,j+1) Ixy(i,j+1); Ixy(i,j+1) Iy2(i,j+1)];
49+
I31 = [Ix2(i+1,j-1) Ixy(i+1,j-1); Ixy(i+1,j-1) Iy2(i+1,j-1)];
50+
I32 = [Ix2(i+1,j) Ixy(i+1,j); Ixy(i+1,j) Iy2(i+1,j)];
51+
I33 = [Ix2(i+1,j+1) Ixy(i+1,j+1); Ixy(i+1,j+1) Iy2(i+1,j+1)];
52+
53+
H = I11 + I12 + I13 + I21 + I22 + I23 + I31 + I32 + I33;
54+
55+
K(i,j) = det(H)/trace(H);
56+
57+
end
58+
end
59+
60+
% set threshold at 99 percent quantile, the remaining values are the 1
61+
% percent highest responses
62+
thresh = quantile(K(:),0.99);
63+
check = true;
64+
corners = [];
65+
allcorners = [];
66+
67+
%only take features which lie more than 4 pixels in the image as to not
68+
%conflict with 9x9 patch of descriptors. Inner loop computes non-maximum
69+
%suppression.
70+
for i=4:n-3
71+
for j=4:m-3
72+
if K(i,j) > thresh
73+
allcorners = [allcorners [i;j]];
74+
for u=-1:1
75+
for v=-1:1
76+
if K(i,j) < K(i+u,j+v)
77+
check = false;
78+
end
79+
end
80+
end
81+
if(check == true)
82+
corners = [corners [i;j]];
83+
end
84+
check = true;
85+
end
86+
end
87+
end
88+
89+
H = K;
90+
end

ex01/code/images/I1.jpg

1.58 MB
Loading

ex01/code/images/I2.jpg

1.62 MB
Loading

ex01/code/images/cv1.jpg

434 KB
Loading

ex01/code/images/cv2.jpg

463 KB
Loading

ex01/code/images/sw1.jpg

610 KB
Loading

ex01/code/images/sw2.jpg

616 KB
Loading

ex01/code/matchDescriptors.m

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
% match descriptors
2+
%
3+
% Input:
4+
% descr1 - k x n descriptor of first image
5+
% descr2 - k x m descriptor of second image
6+
% thresh - scalar value to threshold the matches
7+
%
8+
% Output:
9+
% matches - 2 x w matrix storing the indices of the matching
10+
% descriptors
11+
function matches = matchDescriptors(descr1, descr2, thresh)
12+
%k = 9*9 height of descriptor vector
13+
[~,n] = size(descr1);
14+
[~,m] = size(descr2);
15+
matches = [];
16+
%check every combination of descr1 and descr2 for best matches
17+
for i=1:n
18+
d1 = descr1(:,i);
19+
for j=1:m
20+
d2 = descr2(:,j);
21+
d = d1 - d2;
22+
ssd = dot(d,d);
23+
if ssd < thresh %only add mapping if ssd below threshold
24+
matches = [matches [i;j]];
25+
end
26+
end
27+
end
28+
29+
end

ex01/code/showFeatureMatches.m

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
% show feature matches between two images
2+
%
3+
% Input:
4+
% img1 - n x m color image
5+
% corner1 - 2 x k matrix, holding keypoint coordinates of first image
6+
% img2 - n x m color image
7+
% corner1 - 2 x k matrix, holding keypoint coordinates of second image
8+
% fig - figure id
9+
function showFeatureMatches(img1, corner1, img2, corner2, fig)
10+
[sx, sy, sz] = size(img1);
11+
img = [img1, img2];
12+
13+
corner2 = corner2 + repmat([sy, 0]', [1, size(corner2, 2)]);
14+
15+
figure(fig), imshow(img, []);
16+
hold on, plot(corner1(1,:), corner1(2,:), '+r');
17+
hold on, plot(corner2(1,:), corner2(2,:), '+r');
18+
hold on, plot([corner1(1,:); corner2(1,:)], [corner1(2,:); corner2(2,:)], 'b');
19+
end

ex01/code/showImageWithCorners.m

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
% show image with key points
2+
%
3+
% Input:
4+
% img - n x m color image
5+
% corner - 2 x k matrix, holding keypoint coordinates of first image
6+
% fig - figure id
7+
function showImageWithCorners(img, corners, fig)
8+
figure(fig);
9+
imshow(img, []);
10+
11+
hold on, plot(corners(1,:), corners(2,:), '+r');
12+
13+
end

ex01/code/showNoFeatureMatches.m

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%this function is called if the matchDescriptor function was not able to
2+
%match any features given the threshold. It will plot the two images with
3+
%their respective features.
4+
function showNoFeatureMatches(img1, corner1, img2, corner2, fig)
5+
[sx, sy, sz] = size(img1);
6+
img = [img1, img2];
7+
8+
corner2 = corner2 + repmat([sy, 0]', [1, size(corner2, 2)]);
9+
10+
figure(fig), imshow(img, []);
11+
hold on, plot(corner1(1,:), corner1(2,:), '+r');
12+
hold on, plot(corner2(1,:), corner2(2,:), '+r');
13+
end

ex01/code/sift.m

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
%called with 2 grayscale images, computes sift features and descriptors
2+
%use threshold to filter for uniqueness, same as for Harris corners
3+
4+
function [matches] = sift(img1, img2, imgBW1, imgBW2, threshFeatures, threshMatch, fig)
5+
6+
%compute sift keypoints and descriptors
7+
[k1,d1] = vl_sift(single(imgBW1),'PeakThresh',threshFeatures);
8+
[k2,d2] = vl_sift(single(imgBW2),'PeakThresh',threshFeatures);
9+
10+
%find matches between the 2 images
11+
[matches, scores] = vl_ubcmatch(d1, d2, threshMatch);
12+
13+
%plot results of sift
14+
[sx, sy, sz] = size(img1);
15+
img = [img1, img2];
16+
17+
%only need x,y coordinates of keypoints
18+
k1 = k1(1:2, :);
19+
k2 = k2(1:2, :);
20+
k1 = k1(:, matches(1,:));
21+
k2 = k2(:, matches(2,:));
22+
23+
k2 = k2 + repmat([sy, 0]', [1, size(k2, 2)]);
24+
figure(fig), imshow(img, []);
25+
hold on, plot(k1(1,:), k1(2,:), '+r');
26+
hold on, plot(k2(1,:), k2(2,:), '+r');
27+
28+
figure(fig+1), imshow(img, []);
29+
hold on, plot(k1(1,:), k1(2,:), '+r');
30+
hold on, plot(k2(1,:), k2(2,:), '+r');
31+
hold on, plot([k1(1,:); k2(1,:)], [k1(2,:); k2(2,:)], 'b');
32+
end

ex01/pollefeys-script.pdf

4.99 MB
Binary file not shown.

ex01/vision_report_ex1.pdf

4.3 MB
Binary file not shown.

ex02/Exercise2.pdf

489 KB
Binary file not shown.

ex02/code/clickedPoints.mat

387 Bytes
Binary file not shown.

ex02/code/decompose.m

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function [ K, R, C ] = decompose(P)
2+
%decompose P into K, R and t
3+
M = P(:,1:3);
4+
[Rinv,Kinv] = qr(inv(M));
5+
R = inv(Rinv);
6+
K = inv(Kinv);
7+
8+
R = R*K(end,end);
9+
K = K/K(end,end);
10+
11+
[~,~,V] = svd(P);
12+
C = V(:,end);
13+
C = C(1:3)./C(4);
14+
end

ex02/code/dlt.m

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function [P] = dlt(xy, XYZ)
2+
%computes DLT, xy and XYZ should be normalized before calling this function
3+
4+
[~, n] = size(xy);
5+
A = [];
6+
z = zeros(1,4);
7+
for i=1:n
8+
tmp = XYZ(:,i)';
9+
top = -xy(1,i)*tmp;
10+
bot = xy(2,i)*tmp;
11+
%A = [A; [XYZ(:,i)' zeros(1,4) -xy(1,i)*XYZ(:,i)'; zeros(1,4) -XYZ(:,i) xy(2,i)*XYZ(:,i)]];
12+
A = [A; [tmp z top; z -tmp bot]];
13+
end
14+
15+
[~,~,V] = svd(A);
16+
P = V(:,end);
17+
P = reshape(P,4,3)';

ex02/code/exercise2.m

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
% Exervice 2
2+
%
3+
close all;
4+
5+
%IMG_NAME = 'images/image001.jpg';
6+
IMG_NAME = 'img/ex2_1.jpg';
7+
8+
%This function displays the calibration image and allows the user to click
9+
%in the image to get the input points. Left click on the chessboard corners
10+
%and type the 3D coordinates of the clicked points in to the input box that
11+
%appears after the click. You can also zoom in to the image to get more
12+
%precise coordinates. To finish use the right mouse button for the last
13+
%point.
14+
%You don't have to do this all the time, just store the resulting xy and
15+
%XYZ matrices and use them as input for your algorithms.
16+
[xy XYZ] = getpoints(IMG_NAME);
17+
18+
xy = cart2hom(xy')';
19+
XYZ = cart2hom(XYZ')';
20+
21+
% === Task 2 DLT algorithm ===
22+
23+
[K, R, t, error] = runDLT(xy, XYZ);
24+
25+
% === Task 3 Gold Standard algorithm ===
26+
27+
[K, R, t, error] = runGoldStandard(xy, XYZ);
28+
29+
% === Bonus: Gold Standard algorithm with radial distortion estimation ===
30+
31+
%[K, R, t, error] = runGoldStandardRadial(xy, XYZ);

0 commit comments

Comments
 (0)