Saturday, October 3, 2009

A18 – Noise models and basic image restoration

In this activity this is what I did:

Procedure:

1. Add noise to the original image:

Original Image

Noises:
I applied the following noises using genradn and imnoise in Scilab and obtained their corresponding PDF p(z):



2. Filter the noisy images

I made and then applied the following filters on each type of noise in (1).

Assuming a rectangular sub-image with of size
n*m, n=m=3( for nearest neighbor perimeter)


1.ARITHEMETIC FILTER

2.GEOMETRIC FILTER

3.HARMONIC FILTER

4.CONTRAHARMNIC FILTER

Q=+1,- 1




3. Determined which type of filter suited for each type noise.


In the following images the arrangement is as follows:

from the top- left-right :
(1) NOISY image (2)
Filtered Image with Arithmetic Filter, (3) Arithmetic Filter
bottom-left-right is :
(4) Harmonic Filter, (5) Contraharmonic Filter : Q=-1, (6) Contraharmonic Filter : Q=+1.


1. GAUSSIAN OR NORMAL NOISE, PDF AND FILTERING


All but the geometric filter was able to somehow remove the gaussian noise in the image.


2. RAYLEIGH NOISE , PDF AND FILTERING



From these results, all the filter worked but I think the contraharmonic filter with positive Q =+1 was most effective in removing the uniform noise since the filter at Q=+1 is beginning to restore the white circle at the center.

3.EXPONENTIAL NOISE, PDF AND FILTERING

All the filter worked except the arithmetic filter.

4.GAMMA NOISE AND FILTERING


The arithmetic and geometric filter cannot remove gamma noise.

5.UNIFORM NOISE , PDF AND FILTERING

The contra-harmonic filter was most effective since its beginning to restore the white circle at the center.

6.SALT AND PEPPER NOISE AND FILTERING

Again , the contra-harmonic filter wins.

7.SPECKLE NOISE AND FILTERING
Again , the contra-harmonic filter wins (again).

SOURCE: A18 – Noise models and basic image restoration manual

ASSESSMENT: 10/10 because I was able to code and implement all the image noise and image filters :) and use imnoise. The main problem was that for the results I only used the imwrite outputs. The imshow rendered different effects that seemed better than end results saved in imwrite :) ..odd

GRATITUDE: I would like to thank Mandee and Alva for discussing with me :) (salamat) and Gibert for the rayleigh.

A19 – Restoration of blurred and noisy image


Objective: Be able to restore the original image from a degraded image using Wiener filter


Steps:

1.Motion blur the original image by taking

or the product of FT of f(image) and h (spatial degradation function) that is G(u,v):



where F(u,v) is the Fourier transform of the original image and H(u,v) is the trasfer function given by
where a and b is the total distance for which the image has been displaced in the x and y direction.
The blurred image g(x,y) is obtained by taking the ifft of G(u,v).This is shown below with a=b=0.01 and T=1.

2.Add noise to this blurred image


For the noise I used: n=grand(ix,iy,'nor',0.05,0.06);

The blurred and noisy image:



3.Use Wiener filter to restore the original image.


The Wiener filter aka minimum mean square error filter or least square error filter.


As stated earlier,the objective is to find an estimate of the original image f(x,y) from the of the corrupted image such that the mean square error between them is minimized. Based on these conditions, the minimum of the error function is given in the frequency domain by the expression :

where


From here the restored image f(x,y) can be retrieved as the ifft of F(u,v).





4. Effects of varying the values of a,b and at constant T=1(sec) and applied noise (normal / gaussian).



as expected , there will be no capture image as a=b=T=1.

Here we notice that as the distance a,b is reduced the image captured becomes less blurred and we can see that the text become more clear. Moreover, as the blurring is reduced the degradation due to the noise become more apparent and becomes a major factor in precluding the restoration of the image. So.


5.Effects of varying the noise added while at constant a=b=0.01, and T=1.


APPLYING RAYLEIGH NOISE:

APPLYING EXPONENTIAL NOISE:
APPLYING UNIFORM NOISE:
APPLYING NORMAL OR GAUSSIAN NOISE:
APPLYING SPECKLE NOISE:
The speckle noise was easily removed by the W.Filter as seen in the image above but the restored image still retained the degradation effects of the blur.


APPLYING SALT AND PEPPER NOISE:


Some errors and remarks:


I used the imwrite (Scilab) versions of the images for the blurred, noisy and restored and not the imshow.
I don't know but the resulting imshow and imwrite of the images are somewhat different:


(LEFT) imshow (RIGHT) imwrite

SOURCE: A19 Restoration of blurred and noisy image

ASSESSMENT: 10/10 since I was able to implement the motion blurr and Wiener filter in Scilab and get the results for various parameter :) I also enjoyed doing this activity.


Thursday, September 17, 2009

A16 Neural Networks

We are done with pattern recognition and classification by (1) mean distance method and (2) probabilistic classification by LDA. Let us now try another classification technique by way of (3) probabilistic neural networks. A neural network is a mathematical model of how neurons in the brain work. Among the many function and varied capabilities of the brain is classification.

Just like the brain, neural network can perform tasks based on experience. It can carry out mapping or classification only after learning the patterns of the classification scheme, something which can be taught by giving examples or training the program. Each example serves to improve the neural network program’s recognition processing.

The architecture of this neural network is illustrated as:

Input layer- is where the features of the set are fed. This first layer computes distances from the input vector to the training input vectors and produces a vector whose elements indicate how close this input is to a training input.


Hidden layer- acts contributions from the input layer by computing the summed probabilities for each class of inputs and passes the results to the output layer.


Output layer-outputs which class these set of feature vectors pertain/ belong to.

In Scilab, we use ANN or Artificial Neural Network to do the classification of the set of features extracted in activity 14. The program used was adapted from Cole’ Blog.



In Activity 14, the samples consisted of 2 class of chips, the Cheese- it labeled as (0) and Pillows as (1). Each class has 12 samples and each sample is described by 4 features namely: hor*ver or area 1, area 2(#pixels), perimeter, and the ratio of area2/perimeter.


Procedure:

1.Specify the no. of neurons in (4)input layer,(4) hidden layer and (1)output layer.


The ANN architecture

N = [4,4,1];

There are 4 features (normalized) extracted from each sample:
These features were specified as x:

x=read('data6train.txt',-1,4);


2. Training

Used 12 objects, 6 from each class in training process as the example classification

t=[0 0 0 0 0 0 1 1 1 1 1 1];


3. Learning

The learning rate and threshold are specified by:

lp = [0.1,0];

Here 0.1 and 0 is the threshold for the error tolerated by the network:

Training cycles or iterations:

T = 1000;


4. Run the classification using ann_FF_Std_online and ann_FF_run(x,N,W);


W = ann_FF_Std_online(x,t,N,W,lp,T);

c=ann_FF_run(x,N,W);

c=c'



5. Results and Discussion

The expected output values were supposed to be in 0’s and 1’s. However the actual results as seen in column 1 Table1 were in decimal. Still, values for each sample were very close to 0 and 1’s and from here we can say that the program has learned the proper grouping of each sample.

Next, I tested whether the program is now ready for the real classification.


This test worked well and the results show that the program worked with 100% correct classification.
Then to see whether or not the program is dependent on the arrangement of the samples, I shuffled the arrangement of the test samples.

Again the test worked well and the samples were classified correctly and showed that this time the program can work smoothly as it can adapt to the arrangement of the inputs.
Then I tested the effects of varying the parameter T which is the no. of cycles or iterations.

As expected lowering the T value reduced the accuracy of the classification while increasing it improves the accuracy, although doing that took a rather a longer runtime. Still the classifications for both cases were all correct.


Afterwards I shuffled the 24 samples to test whether this program can still sort them into their respected classes. Then I just rearranged the output in order.


These tests proved that the program has learned the classification pattern and is 100% accurate.

SOURCE: A16 Neural Networks Manual and Cole' Blog

ASSESSMENT: 10/10 because I was able to use NN in Scilab and make it classify my samples into their respective classes correctly.

Wednesday, September 16, 2009

A17 Photometric Stereo

Fig1. Images I1,I2,I3 and I4


Based on the manual for this activity , from the point sources locations (1) and intensities obtained from the 4 images of the object we are able to compute for g via equation (2). Next we determine the normal vector(3), and then compute component surface normals(nx,ny,nz) from each row.

And relate these surface normals to the partial derivatives of f (4) and compute the elevation z=f(u,v) by taking the integral of in (5) via cumsum of (4) over x and y. This can be easily plotted in 3d using mesh.

Fig 2. 3D plot of the object shape using mesh


SOURCE: A17 Photometric Stereo Manual


ASSESSMENT: 10 because I was able to reconstruct the surface using the directions in the manual :) and I was able to use mesh :)

Wednesday, September 2, 2009

A15 Probabilistic Classification

Linear discriminant analysis (LDA) is one of the techniques used pattern recognition to classify two or more classes of objects and in this case the separation of the 2 kinds of chips using the 4 features aextracte from previous activities.

The classification works by taking the combinations of features based on differences. Say the objects’ features are linearly separable, we can use linear discriminant model (LDA) formula given as:


SOURCE: http://people.revoledu.com/kardi/tutorial/LDA/LDA.html#LDA

This is a summary of the data used in activity 14:

Table 1.
Procedure:
1. LDA
After processing the data in Table 1 as input x in the implemented LDA formula in Scilab, each sample was easily classified as either belonging in class 1 or 2 depending on its value f.
2. Determine if f1>f2:
By comparison, a large value of f1 would mean that this object belonged to class 1 and and class 2 if f1 is small.
The LDA worked successfully in assigning the 24 samples to its respective class.

ASSESSMENT: 10/10 because I successfully implemented LDA in scilab and used EXCEL to summarize the data into tables :)

Wednesday, August 26, 2009

A14 Pattern Recognition

PROCEDURE:
1. Trials
Pattern recognition by way of shape classification was implemented in this activity where I took an image full of various shapes(Fig.1a),convert it to black and white after thresholding, and took the edges of each shape, then labeled each shape and computed its perimeter by bwlabel and tabul.

a.original imageb. bw of the original im


c. thind. skeleton

Figure1.

Figure 2. Took the edge this time
Figure 3. Barplot of each shape tagged from 1-30 and its corresponding perimeter.

From Figure 3, the tally of the shapes are :DEW: 8,HEART: 5, petal :6, circle :10, leaf:1 and these results agree with the number of occurrence of those patterns in Figure 1,2. :)


2. Minimum Distance Classification
Applying this,we captured an image of 2 kinds(2 class) of chips and extracted the best features of the objects. After obtaining these features I separated them in half as training feature and test features.For each class, I computed mean feature vector mj and which I then used to classify the remaining test features. The one with the minimum distance was used determining which class an object belonged to. Afterward, the percent error of the classifier was then computed.

The original image (Fig4)has these vertical that I cannot remove using fft so I just used Gimp to remove it.Then I converted the image to grayscale then to bw after thresholding. I used dilation and erosion to finally clean the image. Here they are(Fig5).
I then extracted the following features:

1. AREA1 from the vertical and horizontal length of each chip,

hor=[];ver=[];this=1;

for i=2:n

[x,y]=find(L==i); hor(this)=max(y)-min(y); ver(this)=max(x)-min(x);

area1=ver.*hor; this=this+1;

end

area1



2. AREA from the length of the bwlabel(L,n) then the are itself by area=tabul(L,"i")


3. the perimeter using edge of the bwlabel(L,n) then the are itself by perimeter=tabul(L,"i")


4. ratio of area /perimeter by (area./perimeter)


5. rgb component of each chip by

r=im1(:,:,1);g=im1(:,:,2);b=im1(:,:,3);red=[];blue=[];green=[];

this=1;for i=1:n

[x,y]=find(L==i);

//at that chip lang

red(this)=mean(r(x));

green(this)=mean(g(x));

blue(this)=mean(b(x));

this=this+1;

end

//lhat ng chip

red

green

blue


After obtaining these features I separated them in half as training feature and test features.For each class, I computed mean feature vector mj and which I then used to classify the remaining test features. The one with the minimum distance was used determinining which class an object belonged to. Afterwards, the percent error of the classifier was then computed.


Fig 4 Image of the chips :), cheeseit and pillows

Fig 5 After removing the lines, graycaling, converting to bw, dilation and erosion .
From here got the area from the vertical*horizontal line running across each chip and area from the sum(pixels)

Fig 6 Got the perimeter from the edges.
Table1: Summary of the extracted features.

Table 2. Summary of the data after Minimum Distance Classification. In the classification I just used area1, area,perimeter, and area/perimeter in the classification(para madali).

SOURCE: A14 Pattern Recognition Manual

ASSESSMENT: 10/10 because I implemented my idea of using of using only bwlabel and tabul in getting the features from areas to RGB of each sample.I was also able to apply other techniques from the previous activity. Then the classifier was 100% error free in classifying the two chips. (yes:)

Wednesday, August 19, 2009

A13 – Correcting Geometric Distortions

Two forms of distortion commonly encountered in image processing :

1.Barrel effect

This effect gives the image an inflated or blown up appearance .


2. Pincushion Effect

This effect gives the opposite of barrel distortion wherein the image suffers from deflated appearance.



PROCEDURE:
To correct for distortion we need to capture a reference image with a regularly occuring pattern so that we can visualize the distortion across the image frame.


The correction will be done on two image properties:
1. pixel coordinates and
2. graylevel values

1. From the most undistorted part of the image the ideal grid vertex points were located and the reference image for the corrected constructed.
2.For each rectangle compute c1 to c8 using pairs of equations (x's,y's) from
the four corner points. Per pixel in the ideal rectangle, the location of that point in the
distorted image was determined. If the computed distorted location is integer-valued, the grayscale value from the distorted image onto the blank pixel was used. If the location was not integer-valued, the interpolated grayscale value was computed instead using the equations in the manula provided in the source.

SOURCE: A13 Correcting Geometric Distortions Manual

Figure 1 . Distorted image



Figure 2. Reference image for the distorted image in figure1. pixel distances from a square in fig1.


Since it was really difficult to correct the distortion in Figure 1 at once, I just tried to correct the distortion in 1/2 the image.
So far


Figure 3. a. Half portion of image 1, (b) Corrected image distortion using linear approximation and (c) using bilinear interpolation. The crooked lines in b and c are partly due to the error in the location of the corner points in locate(and the sobrang grabeng distortion).
The quality of the corrected image using linear approximation was not as good as that of the bilinear interpolation since the image for the linear approximation using only the nearest neighbors resulted to more jagged corners for each square in the grid.


ASSESSMENT: 9/10, because I didn't get the complete distortion correction of the image though I was able to implement the distortion correction in Scilab. The main problem and the hardest part was the location of the distorted points xi and yi. Eventhough I used locate I still couldn't get the exact endpoint for each square. I should have limited myself in either barrel or pincushion distortion. :)

GRATITUDE:I would like to thank Mandee and Alva for teaching me how to do this activity :)