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);

Noise in images

Noise is just another function that is combined with the original function to get a new – guess what – function

I(x,y) = I(x,y)+n(x,y)

Salt and pepper noise: random occurrences of black and white pixels
Impulse noise: random occurrences of white pixels
Gaussian noise: variations in intensity drawn from a Gaussian normal distribution

>> noise = randn(size(im)).*sigma;
>> output = im + noise;
dolphin = imread('dolphin.png');
bicycle = imread('bicycle.png');

diff = dolphin - bicycle;

abs_diff = abs(bicycle - dolphin);
imshow(abs_diff);

preserve image difference: uint8
・(a – b)+(b – a)
・Convert to floating point

pkg load image;

abs_diff2 = imabsdiff(dolphin, bicycle);
imshow(abs_diff2);

Add 2 images demo

dolphin = imread('dolphin.png');
bicycle = imread('bicycle.png');

imshow(dolphin);
disp("Dolphin image size:");
disp(size(dolphin));

imshow(bicycle);
disp("Bicycle image size:");
disp(size(bicycle));

summed = dolphin + bicycle;
imshow(summed);

average = dolphin / 2 + bicycle / 2;
imshow(average);

183/2 + 152/2 = 168
(183+152)/2 = 128

Inspect image value

% At a given location (row, col):
dis(img(50, 100));

dis(img(50, :));

plot(img(50, :));
% At slice of the image:
disp(img(101:103, 201:203));

disp(size(img));
% Cropped size:
disp(size(cropped));

color planes

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

disp(size(img));

img_red = img(:, :, 1);
imshow(img_red);

plot(img_red(150, 0));