Friday, December 18, 2015

Special functions related to image processing

The following just summaries some common functions in Matlab related to image processing. it will be updated if new ones are chanced upon and worthy-while.

conv: convolution of two 1-dim vectors

example 1:
suppose u = [1, 2, 3]; v = [1, -1], then r = conv(u, v) = [1, 1, 1, -3]
the procedure is as follows:
1. flip v, so we have v = [-1, 1]
2. overlap the last element of v, that's 1, with the first element of u, that's 1, do the multiplication and addition. note if we assign u[0] = 1, u[1] = 2 and u[2] = 3, we implicitly padding u[-1] = 0 ahead of u. so we have u[-1] * 1 + u[0]*-1 = 0*(-1) + 1*1 = 1
3. advance v or slip v along the positive direction for one step, and do the multiplication and addition. so we have 1*(-1) + 2*1 = 1
4. repeat the above procedures, we will have the final result
note: conv(u, v) is equivalent to conv(u, v, 'full')

example 2:
suppose u = [1, 2, 3]; v = [1, -1], then r = conv(u, v, 'same') = [1, 2, -3];
the procedures is as follows:
1. flip v, so we have v = [-1, 1]
2. align u, v to the same head elements. alternatively, if we denote u[0] = 1, u[1] = 2, u[2] = 3, then we have v[0] = -1, v[1] = 1
3. do the multiplication and addition until the last chance of having u and v overlapping. that's means the final value must be u[end]*v[head]

example 3:
suppose u = [1, 2, 3]; v = [1, -1], then r = conv(u, v, 'valid') = [1, 1]
the procedures is as follows
1. flip v, so we have v = [-1, 1]
2. align u, v. however in each advancement of v, it must ensure u and v are totally overlapped. the precedures will automatically stop if partial overlap happen.

conv2: convolution of 2-D matrices U and V
suppose 
U = [1, 2, 3;
        4, 5, 6;
        7, 8, 9;];
V = [-1, 1;
        -1, 1];

R = conv2(U, V) = [ -1,  -1,  -1,  3;
                                -5,  -2,  -2,   9;
                               -11, -2,  -2, 15;
                                -7,  -1,  -1,   9;];

R = conv2(U, V, 'same') = [ -2,  -2,   9;
                                            -2,  -2, 15;
                                            -1,  -1,   9;];
 
R = conv2(U, V, 'valid') = [ -2,  -2;
                                           -2,  -2;];
the detailed procedures are identically the case of one dimension. note the way of reversing V

suppose V = [1, 2;
                      3, 4;];
then the reversing of V equals to
[4, 3;
 2, 1;]


filter2: filtering one two-dimensional vector using a FIR digital filter with coefficients stored in another two-dimensional vector

U = [1, 2, 3;
        4, 5, 6;
        7, 8, 9;];
V = [-1, 1;
        -1, 1];

R = filter2(V, U) = [ 2,  2,   -9;
                               -2,  2, -15;
                                1,  1,   -9;];
 
note: the filter2 function is essential resemble the conv2 function. and the filtering matrix needn't be reversed. 
 
however, pay attention to the order of input parameters. since the default behaviour is with shape parameter set as 'valid', so if U and V are of different sizes (usually they are of different sizes), different outputs will be generated if reverse the two.
 
gradient: find the gradient for 1-D vector or 2-D matrix
example 1: suppose a = [1, 2, 4, 1], g = gradient(a) = [1, 1.5, -0.5, -3];
the procedures are as follows:
suppose a[0] = 1, a[1] = 2, a[2] = 4, a[1] = 1;
so g[0] = (a[1] - a[0]) / 1 = 1; g[1] = (a[2] - a[0]) / 2 = 1.5;
g[2] = (a[3] - a[1]) / 2 = -0.5; g[3] = (a[3] - a[2]) / 1 = -3.
that's means for interior point, the central difference will be utilized. for edge point, the single-sided difference will be utilized.

example 2:
suppose a = [1, 2, 3;
                      4, 5, 6;
                      7, 8, 9;];
then [gx, gy] = gradient(a) leads to
gx = [1, 1, 1;
          1, 1, 1;
          1, 1, 1;];
gy = [3, 3, 3;
          3, 3, 3;
          3, 3, 3;];

pay attention to the ways of difference approaches applied to interior points and edge points along row and column directions respectively.

fspecial: create special 2-D filters, like gaussian, laplacian, etc.
example1: 
G = fspecial('gaussian', 5) = [
    0.0000    0.0000    0.0002    0.0000    0.0000
    0.0000    0.0113    0.0837    0.0113    0.0000
    0.0002    0.0837    0.6187    0.0837    0.0002
    0.0000    0.0113    0.0837    0.0113    0.0000
    0.0000    0.0000    0.0002    0.0000    0.0000
];
example2:
L = fspecial('laplacian') = [
    0.1667    0.6667    0.1667
    0.6667   -3.3333    0.6667
    0.1667    0.6667    0.1667
];
for other types, please refer to the help system of matlab
 
 
 
Reference:
UCF Computer Vision lectures
Signals and Systems for Bioengineers, John Semmlow
 
 
 
 
 

 








No comments:

Post a Comment