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

Quantize an image

2.5, 0.7, 3, 6
3.7, 4.5, 1.9, 3.2
-1.3, 5.2, 7.5, 2.9
Levels:0,1,2,3,4,5

Round down:1.8 -> 1
Limits: <0 -> 0, >5 -> 5

each number always rounding down.

% Load and display an image
img = imread('dolphin.png');
imshow(img);

% Image size:
disp(size(img));

% Image class or data type:
disp(class(img));

hight 320
width 500
class uint8

u -> unsigned
int -> integer
8 -> 8bits

A little bit of pedagody

Computational Models(Math!)
Algorithm
Real Images

matlab
https://jp.mathworks.com/

GNU Octave
https://www.gnu.org/software/octave/

An image can be thought of as:
– a 2-dimensional array of numbers ranging from some minimum to some maximu
– a function I of x and y: I(x, y)
– something generated by a camera

Images as functions
we think of an image as a function, f or i, from R^2 to R
f(x, y) gives the intensity or value at position (x, y)
Piratically define the image over a rectangle, with a finite range:
f:[a, b]x[c, d] -> [min, max]
f(x,y) = [r(x,y) g(x,y) b(x,y)]

f:[10, 210]*[15.155] -> [0, 10]
(r,g,b) channels or planes

In computer vision we typically operate on digital images:
sample the 2d space on a regular grid
quantize each sample

Image thus represented as a matrix of integer values.
width 320, height 258, area 82560
3 color -> 82560*3 total color values

>> im = imread(‘peppers.png’); % semicolon or many numbers
>> imgreen = im(:,:,2);
>> imshow(imgreen)
>> line([1 512],[256 256],’color’,’r’)