Computer Vision Course Udacity
2A-L1 Image as functions
% Load and display an image
img = imread('dolphin.png');
imshow(img);
disp(size(img));
disp(class(img));
disp(img(101:103,201:203));
81 77 77
81 78 76
82 79 77
% Color planes
img = imread('fruit.png');
imshow(img);
disp(size(img));
% TODO: Select a color plane, display it, inspect values from a
row
im_red = img(:,:,1);
imshow(im_red);
% Blend two images
function output = blend(a, b, alpha)
% TODO: Your code here; finally assign: output =
<something>:
output = alpha * a + (1-alpha) * b ;
endfunction
% Test code:
dolphin = imread('dolphin.png');
bicycle = imread('bicycle.png');
result = blend(dolphin, bicycle, 0.75);
imshow(result); % note: will result in an error if blend()
returns empty or incorrect value
2A-L2 Filtering
% Apply a Gaussian filter to remove noise
img = imread('saturn.png');
imshow(img);
% TODO: Add noise to the image
noise_sigma = 25;
noise = randn(size(img)).* noise_sigma;
noisy_img = img + noise;
imshow(img);
% TODO: Now apply a Gaussian filter to smooth out the noise
filter_size = 11;
filter_sigma = 2;
pkg load image;
filter = fspecial('gaussian', filter_size, filter_sigma);
smoothed = imfilter(noisy_img,filter);
imshow(smoothed);
% Note: You may need to pkg load image;
% Explore edge options
pkg load image;
%% Load an image
img = imread('fall-leaves.png'); % also available:
peppers.png, mandrill.png
imshow(img);
%% TODO: Create a Gaussian filter
filter_sigma = 2;
filter_size = 21;
filter = fspecial('gaussian',filter_size,filter_sigma);
%% TODO: Apply it, specifying an edge parameter (try
different parameters)
smoothed = imfilter(img,filter,'replicate');
imshow(smoothed);
smoothed = imfilter(img,filter,0);
imshow(smoothed);
smoothed = imfilter(img,filter,'circular');
imshow(smoothed);
Median filtering
Some convolution related laws introduced in the last lecture.
2A-L4 Filters as Templates
pkg load image; % AFTER function definition
function index = find_template_1D(t, s)
c = normxcorr2(t,s);
[maxvalue index] = max(c);
index = index - size(t,2) + 1;
endfunction
% Test code:
s = [-1 0 0 1 1 1 0 -1 -1 0 1 0 0 -1];
t = [0 1 0];
disp('Signal:'), disp([1:size(s, 2); s]);
disp('Template:'), disp([1:size(t, 2); t]);
index = find_template_1D(t, s);
disp('Index:'), disp(index);
2ndfunction 2d version
pkg load image; % should always be before using it in code
function [yIndex xIndex] = find_template_2D(template, img)
c = normxcorr2(template,img);
[yraw xraw] = find(c == max(c(:))); % this will compute the
image wise maximum
yIndex = yraw - size(template,1) + 1;
xIndex = xraw - size(template,2) + 1;
% disp('correlation:'), disp([1:size(img, 2); img]);
endfunction
% Test code:
tablet = imread('tablet.png');
imshow(tablet);
glyph = tablet(75:165, 150:185);
imshow(glyph);
[y x] = find_template_2D(glyph, tablet);
disp([y x]); % should be the top-left corner of template in
tablet
colormap(‘gray’),imagesc(glyph);
hold on;
plot(x,y,’r+’,’markersize’,16);
hold off;
xy gradient of image
Differentiation is linear operation. All linear operations lets us
allow to use (Associate, Commutative and Distributive )
properties.
% Gradient Direction
pkg load image;
function result = select_gdir(gmag, gdir, mag_min,
angle_low, angle_high)
% TODO Find and return pixels that fall within the desired
mag, angle range
result = gmag >= mag_min & gdir >= angle_low & gdir <=
angle_high;
endfunction
%% Load and convert image to double type, range [0, 1] for
convenience
img = double(imread('octagon.png')) / 255.;
imshow(img); % assumes [0, 1] range for double images
%% Compute x, y gradients
[gx gy] = imgradientxy(img, 'sobel'); % Note: gx, gy are not
normalized
%gx = (gx+4)/8;
%gy = (gy+4)/8;
%% Obtain gradient magnitude and direction
[gmag gdir] = imgradient(gx, gy);
imshow(gmag / (4 * sqrt(2))); % mag = sqrt(gx^2 + gy^2), so
[0, (4 * sqrt(2))]
imshow((gdir + 180.0) / 360.0); % angle in degrees [-180, 180]
%% Find pixels with desired gradient direction
my_grad = select_gdir(gmag, gdir, 1, 30, 60); % 45 +/- 15
imshow(my_grad); % NOTE: enable after you've
implemented select_gdir
2A-L6 Edge Detector: 2D operators
Edge Function Matlab
% For Your Eyes Only
pkg load image;
frizzy = imread('frizzy.png');
froomer = imread('froomer.png');
imshow(frizzy);
imshow(froomer);
% TODO: Find edges in frizzy and froomer images
friz = rgb2gray(frizzy);
froom = rgb2gray(froomer);
frizzy_edge = edge(friz,'canny');
froomer_edge = edge(froom,'canny');
imshow(frizzy_edge);
imshow(froomer_edge);
% TODO: Display common edge pixels
imshow(frizzy_edge & froomer_edge); % common edge
pixels are obtained by doing and of the two images.
2B-L1 Hough transform lin
A line in the image space(x,y) corresponds to a point in
Hough space(b,m). and a point in image space(x,y)
corresponds to a line in Hough space (b,m). This is duality.
Point in image space is now sinusoid segment in hough space
x cos θ+ y sin θ=d
Hough transform theory presented with all displacement
vectors towards center of object. No major coding example
demonstrated.
2C-L1 Fourier Transform
3B-L3 Stereo correspondence
% TODO: Match two strips to compute disparity values
function disparity = match_strips(strip_left, strip_right, b)
% For each non-overlapping patch/block of width b in the left strip,
% find the best matching position (along X-axis) in the right strip.
% Return a vector of disparities (left X-position - right X-position).
% Note: Only consider whole blocks that fit within image bounds.
endfunction
% Find best match for a patch in a given strip (SSD)
% Note: You may use this or roll your own
function best_x = find_best_match(patch, strip)
min_diff = Inf;
best_x = 0; % column index (x value) of topleft corner; haven't found it yet
for x = 1:(size(strip)(2) - size(patch)(2))
other_patch = strip(:, x:(x + size(patch)(2) - 1));
diff = sumsq((patch - other_patch)(:));
if diff < min_diff
min_diff = diff;
best_x = x;
endif
endfor
endfunction
pkg load image;
% Test code:
%% Load images
left = imread('flowers-left.png');
right = imread('flowers-right.png');
figure, imshow(left);
figure, imshow(right);
%% Convert to grayscale, double, [0, 1] range for easier computation
left_gray = double(rgb2gray(left)) / 255.0;
right_gray = double(rgb2gray(right)) / 255.0;
%% Define strip row (y) and square block size (b)
y = 120;
b = 100;
%% Extract strip from left image
strip_left = left_gray(y:(y + b - 1), :);
figure, imshow(strip_left);
%% Extract strip from right image
strip_right = right_gray(y:(y + b - 1), :);
figure, imshow(strip_right);
%% Now match these two strips to compute disparity values
disparity = match_strips(strip_left, strip_right, b);
disp(disparity);
figure, plot(disparity);
Coherent Stereo on 2D grid
4A-L2 Finding Corners
6B-L3 Hierarchical LK