1D (nx)correlation

Signal
Filter
Normalized cross-correlation

Matlab cross-correlation doc
onion = rgb2gray(imread(‘onion.png’));
peppers = rgb2gray(imread(‘peppers.png’));
imshowpair(peppers, onion,’montage’)

Matlab cross-correlation doc
c = normxcorr2(onion,peppers);
figure, surf(c), shading flat;

pkg load image;
function index = find_template_1D(t, s)

endfunction

s = [-1 0 0 1 1 1 0 -1 -1 0 1 0 0 -1];
t = [1 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);

Median filter

10 15 20
23 90 27
33 31 30
sor-> 10 15 20 23 27 30 31 33 90
replace
10 15 20
23 27 27
33 31 30
Median filter is smooth nicer

pkg load image;

img = imread('moon.png');
imshow(img);

%% Add salt & pepper noise
noisy_img = imnoise(img, 'salt & pepper', 0.02);
imshow(noisy_img);

median_filtered = medfilt2(noisy_img);
imshow(median_filtered);

Linear Operation

division is a linear operation?
true becase (x + y)/z = x/z + y/z

Boundary Issues
full, same, valid

Boundary issues
methods:
clip filter(black):imfilter(f,g,0)
wrap around(f,g,’circular’)
copy edge(f,g,’replicate’)
reflect across edge(f,g,’symmetric’)

pkg load image;

img = imread('fall-leaves.png');
imshow(img);

filter_size = 21;
filter_sigma = 3;
filter = fspecial('gaussian', filter_size, filter_sigma);

smoothed = imfilter(img, filter, 0);

The reflection method of handling boundary conditions in filtering is good because
the created imagery has the same statistic as the original image

Linearity and convolution

An operator H is linear if two properties hold(f1 and f2 are some functions, a is a constant):

Additivity(things sum):
-H(f1 + f2) = H(f1) + H(f2)(looks like distributive law)

Multiplicative scaling(Homogeneity of degree 1)
(constant scales):
-H(a*f1) = a*H(f1)

Because it is sums ans multiplies, the “filtering” operation we were doing are linear:

An impulse function…
it’s just a value of 1 at a single location

An impulse response
-if I have unknown system and I put in an impulse, the response is called the impulse response
-so if the black box is linear you can describe H by h(x)

Assuming center coordinate is “reference point”.

kernel is size MxM, and our image was NxN.
filter whole image is M*M*N*N

Correlation vs Convolution(flip in both dimensions, bottom to top, right to left)

Shift invariant:
linear & shift invariant

Associative
(f*g)*h = f*(g*h)
Identity:
unit impulse e = […,0,0,1,0,0,…] f*e = f
Differentiation: α/αx(f*g) = αf/αx*g

Gaussian filter

Nearest neighboring pixels have the most influence
F(x, y)
H(u, v)
1 2 1
2 4 2
1 2 1
this kernel is an approximation of a Gaussian function
h(u, v) = 1/2πσ^2 e ^(- u^2+v^2/σ^2)

Gaussian filters
Variance(σ^2), standard deviation(σ): determines extent of smoothing

hsize = 31;
sigma = 5;
h = fspecial('gaussian', hsize, sigma);

surf(h);
imagesc(h);

outim = imfilter(im, h);
imshow(outim);
for sigma = 1:3:10
	h = fspecial('gaussian', fsize, sigma);
	out = imfilter(im, h);
	imshow(out);
	pause;
end
img = imread('saturn.png');
imshow(img);

noise = randn(size(img)) .* 25;
noisy_img = img + noise;
imshow(noisy_img);

filter_size = 11;
filter_sigma = 2;
pkg load image;
filter = fspecial('gaussian', filter_size, filter_sigma);

smoothed = imfilter(noisy_img, filter);

When filtering with a Gaussian, the sigma is most important- it defines the blur kernel’s scale with respect to the image.
Altering the normalization coefficient does not effect the blur, only the brightness.

Moving Average in 2D

F(x, y)
G(x, y)

Correlation filtering – uniform weights
2k + 1 * 2k + 1;
G[i, j] = 1/(2k + 1)^2 ΣΣF[i + u, j + v]
Loop over all pixels in neighborhood around image pixel F[i,j]

nonuniform weights
Now generalize to allow different wieghts depending on neighboring pixel’s relative position:
G[i, j] = ΣΣH[u, v]F[i+u, j+v]
This is called cross-correlation, denoted G = HF

averaging filter -> box filter
Blurry spot as a function

To blur a single pixel into a “blurry” spot, we would need to filter the spot with something that looks like a blurry spot – higher value in the middle, falling off to the edges.

Noise, weighted moving average

Noise is just a function added to an images, we could remove the noise by subtracting the noise again
-> true but we don’t know the noise function so we can’t actually do the subtraction.

-can add weights to our moving average
-weight [1, 1, 1, 1, 1] / 5
Non-uniform weights
Σ [1, 4, 6, 4, 1]/16

To do the moving average computation the number of weight should be
-odd: makes it easier to have a middle pixel

Effect of σ on Gaussian noise

noise = randn(size(im)).*sigma;
Sigma = 2, 8, 32, 64

noise = randn(size(img)).* 2;
output = im + noise;

First attempt at a solution
Replace each pixel with an average of all the values in its neighborhood – a moving average;
original -> smoothed

Average assumptions
1. the true value of pixels are similar to the true value of pixels nearby.
2. the noise added to each pixel is done independently.

Generate Gaussian Noise

some_number = randn();
disp(some_number);

some_numbers = randn([1 5]);
disp(some_numbers);

some_matrix = randn([2 3]);
disp(some_matrix);

noise = randn([2 3]);
disp(noise);

noise = randn([1 100]);
[n, x] = hist(noise, [-3 -2 -1 0 1 2 3]);
disp([x; n]);
plot(x, n);