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

Thursday, August 6, 2009

A10 Preprocessing Text

Int this activity we tried to extract handwritten text from an imaged document with lines. This is often used in in handwriting recognition where individual examples of letters must be extracted.










Fig. 5. Used the enhanced image in Figure 4. So, from the left the image, its shifted Fourier tranform, its mask to remove the horizontal lines and the final image with only the text and the horizontal lines gone.


Figure 6. The histogram of the masked or cleaned image with handwriting. After thresholding , it was converted to a binary image to extract the text, but the text is still dirty.

In Figure 5 the desired text is already achieved and the image cleaned , but trying to extract the text requires more cleaning. But I think results inFigure 5 is already ok. I am still working on the threshoding and cleaning Figure 5 image... to get better results than what is shown in Figure 6.

Wednesday, August 5, 2009

A11 Color Image Processing

Following the set procedure:
1.We take the picture of colorful objects and an ensemble of objects with different shades , in this case shades in green from leaves.
2. Under a fixed light source (sunlight, sunny) we took the their picture under proper and incorrect white balancing through the Auto, Cloudy, Daylight , Fluorescent and Incandescent settings.
3. We proscessed these images through the white and gray world algorithm.
4. For the WWA we took the the RGB values of pixels belonging to the white object of objects. Let these be Rw, Gw, Bw and implement the White Patch algorithm by dividing all the pixels in the red layer of the image by Rw, the green layer by Gw and the blue layer by Bw. After making sure that no pixel exceed 1.0, the resulting image is shown in the second column in Figure 1.
5. For the GWA we took the average red, green and bluevalue of the captured image and used them as the balancing constants and the resulting image is shown in the third column.

Figure 1.

6. Afterwards we did the same procedure for the ensemble of green leaves and put them agains a white background. And did both for the proper and improper white balancing for the outdoor illumination where AUTO nad DAYLIGHT provide the proper white balancing while CLOUDY, FLURESCENT and INCANDESCENT are the improper.


Haha I couldn't make the imwrite fcn work in this activity so I just resorted to PrtSc and the images are saturated since imshow has limited resolution and still saturated after multiplying the imshow by 0.5.


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

A12 Color Image Segmentation

Color segmentation can be performed by determining the probability that a pixel belongs to a color distribution of interest.This is how we can do just that.

1.The histogram of the region of interest (ROI) must first be extracted.

2.Tag a pixel as belonging to the ROI or not is to find its probability of belonging to the color of the ROI

3. We used the sample code in the manual to show just how histogram backprojection is implemented to tag the pixel and get its histogram value in chromaticity space.




Figure 1.Normalized chromaticity
space. X-axis is r and y-axis is g.[manual]


Figure 2. Image 1-Christmas balls





Figure 3. The top 3 RGB patches are taken from the RGB regions in figure 2. The 2D histograms at the bottom illustrate that these ROI coincide with the small region in their individual R G B space in the NCC shown on the left with the variation corresponding to the pixel chromaticity.



As expected, images of the 3D objects such as bright colored balls have shading variations . Given that its shading variation due to brightness is small, for the red green and blue ROI pathces its pixel chromaticities for RGB will occupy a small portion in its region in NCC as in Figure 3.


The next step was to see which method was more effective in the tagging of the pixels RGB value with the first using use of the red and green joint PDF by parametric segmentation with a Gaussian Distribution function and the other using non parametric segmenatation which takes the 2D histogram of the ROI that is then used segment the image using histogram backprojection. For both technique the result should yield white or high peaks (red in hotcolormap) for matches in the range of red and green of the ROI to that image pixel's RGB.


The results and code are shown below:


Figure 4. The ROI was the red patch in the left middle of the red ball.


Figure 5. The ROI was a blue patch in the left middle of the blue ball.

Figure 6. The ROI was a green patch in the left middle of the green ball.


Figure 7. Plots of Gaussian Probability Distribution of the red, blue and green ROI

It can be noted that both the parametric method and non parametric method were indeed effective in highlighting the image pixels with the same RGB probability distribution as seen in the rendered image.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code:

1st part:
//////1. Capture a digital image of a 3D object with a single color (e.g. colored mugs,faces, hands or any exposed skin, brightly colored balls, etc.). You may also
//////use colored pictures you've captured in previous activities.
//
//


stacksize(4e7);
I=imread('red.jpg');



//
//R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
//Int= R + G + B;
//r=R./I;
//g=G./I;
//b=B./I;


I = double(I); //I is the image of the ROI
R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
Int= R + G + B;
Int(find(Int==0))=100000;
r = R./ Int; g = G./Int;
BINS = 32;
rint = round( r*(BINS-1) + 1);
gint = round (g*(BINS-1) + 1);
colors = gint(:) + (rint(:)-1)*BINS;
hist = zeros(BINS,BINS);
for row = 1:BINS
for col = 1:(BINS-row+1)
hist(row,col) = length( find(colors==( ((col + (row-1)*BINS)))));
end
end
scf(1);
subplot(221);
imshow(I)
subplot(222);
imshow(hist,[]);xset('colormap',jetcolormap(64));
//imwrite(hist,'redh.jpg');

/////////////////////////////////////////

I=imread('green.jpg');

//
//R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
//Int= R + G + B;
//r=R./I;
//g=G./I;
//b=B./I;


I = double(I); //I is the image of the ROI
R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
Int= R + G + B;
Int(find(Int==0))=100000;
r = R./ Int; g = G./Int;
BINS = 32;
rint = round( r*(BINS-1) + 1);
gint = round (g*(BINS-1) + 1);
colors = gint(:) + (rint(:)-1)*BINS;
hist = zeros(BINS,BINS);
for row = 1:BINS
for col = 1:(BINS-row+1)
hist(row,col) = length( find(colors==( ((col + (row-1)*BINS)))));
end
end
scf(2);
subplot(221);
imshow(I);
subplot(222);
imshow(hist,[]);xset('colormap',jetcolormap(64));

//imwrite(hist,'greenh.jpg');

/////////////////////////////////////////

I=imread('blue.jpg');

//
//R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
//Int= R + G + B;
//r=R./I;
//g=G./I;
//b=B./I;


I = double(I); //I is the image of the ROI
R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
Int= R + G + B;
Int(find(Int==0))=100000;
r = R./ Int; g = G./Int;
BINS = 32;
rint = round( r*(BINS-1) + 1);
gint = round (g*(BINS-1) + 1);
colors = gint(:) + (rint(:)-1)*BINS;
hist = zeros(BINS,BINS);
for row = 1:BINS
for col = 1:(BINS-row+1)
hist(row,col) = length( find(colors==( ((col + (row-1)*BINS)))));
end
end
//scf(6);
//subplot(221);
//imshow(hist,[]);
scf(3);
subplot(221);
imshow(I)
subplot(222);
imshow(hist,[]);xset('colormap',jetcolormap(64));
//imwrite(hist,'blueh.jpg');

2nd part:

stacksize(50000000);

//parametric segmentation



//ROI,image patch
ip=imread('red.jpg');//Imagepatches: red,green blue.jpg
Rp = ip(:,:,1); Gp= ip(:,:,2); Bp = ip(:,:,3);
Ip=Rp+Gp+Bp; Ip(find(Ip==0))=100000;

//normalize
Rp=Rp./Ip;Gp=Gp./Ip;Bp=Bp./Ip;

//for the probability destribution
//mean-nu
rm=mean(Rp);gm=mean(Gp);bm=mean(Bp);

//stdev-sigma
rs=stdev(Rp);gs=stdev(Gp);bs=stdev(Bp);

//whole image
wi=imread('balls.jpg');
R=wi(:,:,1);G=wi(:,:,2);B=wi(:,:,3);

I=R+G+B;I(find(I==0))=100000;

//normalize
R=R./I;G=G./I;B=B./I;//

//probability in r and g

pr=(1.0/(rs*sqrt(2*%pi))) *exp(-1.0*(R-rm).^2/(2*rs^2));

pg=(1.0/(gs*sqrt(2*%pi))) *exp(-1.0*(G-gm).^2/(2*gs^2));

p=pr.*pg;//joint prob.
//normalize
p=p/max(p);

scf(1);
imshow(p,[]);xset('colormap',hotcolormap(256));//parametric in Gaussian
imwrite(p,'prredpatch.jpg');xset('colormap',hotcolormap(256));

//hm=histogram of the patch


[row,col]=size(ip);

BINS=32;

r=0:1.0/32:1; g=0:1.0/32:1;


ht = zeros(BINS,BINS);//EMPTY

for i=1:row //of imagepatch
for j=1:col
//get
fr=find(r<=ip(i,j,1)); fg=find(g<=ip(i,j,2)); //find frr=size(fr,2); fgg=size(fg,2); ht( fr(frr), fg(fgg) )=ht( fr(frr) ,fg(fgg))+1 ; end end //the backprojection to the whole image [xi,yi]=size(wi);//whole image tag=zeros(xi,yi);// EMPTY for i=1:xi for j=1:yi //get fr=find(r<=wi(i,j,1)); fg=find(g<=wi(i,j,2)); //find frr=length(fr); fgg=length(fg); tag(i,j)=ht(fr(frr), fg(fgg)); end end scf(2); imshow(tag, []);xset('colormap',hotcolormap(256));// non paramertric imwrite(tag,'tagred.jpg');xset('colormap',hotcolormap(256));

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acknowledgements: Mandee salamat sa pagturo ng parametric segmanetation :)


Monday, August 3, 2009

A9 – Binary Operations

The activity goes like this:
From an image we must be able to obtain the a value of the cell area. Getting that means that we must be able to properly threshold the image before we can convert it to black and white and be able to separate as many cells as we can from the cluster.

First I crop a 256x256 patch from Figure 1 and called it co1. Converted it to graycale, then got the histogram to obtain a threshold so I can separate the cell from the background and turn co1 to a bw image. This is in Figure 2.


Afterwards, with no erosion nor dilation, I used paint to crop 5 distinct well separated cells and got its area using sum(im1).I used the mean area ai=540 as reference area before getting the areas from different regions of the image.


In order to removed the holes and other irregularity in cells and reduce some distinct cells from the clusters, I used a double erosion to remove holes and separate some cells and double dilation to reconstruct some separate cells.

However not all cells were completely separated and they remained in clusters. No matter, the areas of these cells or clusters are then computed as follows,

a.Identification and labelling by [L,n]=bwlabel(im)
b.Getting the area of the n labeled cells by getting the area via tabul(L,'i'). This way I get the frequency of each i in n which is equivalent to that cell or cluster area. I just set the limit the computed areas in the range of 500<600>




Finally, the average area obtained with this method is af= 531.38889 or 531 and the standard deviation of the values from this mean is 14.034155. Furthermore, the reference area mean is ai= 540, and the percent difference of the computed area from the reference is only 1.6%. This suggest that the values are very close and the calculation was indeed correct.


References:
[1]Activity 9 AP 186 Manual
[2]http://groups.google.com/group/face-rec/browse_thread/thread/0d29fb3a357a530a?pli=1
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

stacksize(50000000);

apat=4;

nito=1;//areas nito

for i=1:apat

cut=imread('C:\Users\USA\Desktop\co'+string(i)+'.png');

cut=im2gray(cut);

cut=im2bw(cut, 0.80);

//imwrite(cut,'C:\Users\USA\Desktop\cobw'+string(i)+'.bmp');

x= [-2.0:.5:2];

[X,Y] =ndgrid(x,x);

[sx,sy]= size(X);

r= sqrt(X.^2 + Y.^2);

c=zeros(sx,sy);

f=find(r<=2);

c(f)=1;

im=cut;

se=c;

//h=scf(i+1);

//1.erodeerode. to close holes

im=erode(im, se); im=erode(im, se);

im=dilate(im, se);im=dilate(im, se);

//h=scf(i+3);

//imshow(im);

//imwrite(im,'C:\Users\USA\Desktop\cobwdilate erode use tabul'+string(i+sho)+'.bmp');

[L,n]=bwlabel(im);

//find the area

mm=tabul(L,'i');// frequecny ng values na eun(0,1,2..n n cell)=area ng cell

xx=mm(:,2)//area

make it easy remove area na higher than 600 na

fi=find(xx<=600 & xx>=500);

xx=xx(fi)

end

-->Size: 258 rows X 258 columns

Truecolor Image

xx =

529.

554.

514.

523.

524.

533.

526.

Size: 258 rows X 258 columns

Truecolor Image

xx =

530.

521.

542.

522.

560.

559.

Size: 258 rows X 258 columns

Truecolor Image

xx =

533.

536.

523.

531.

526.

524.

521.

518.

529.

Size: 258 rows X 258 columns

Truecolor Image

xx =

528.

518.

515.

530.

areas=read('areas.txt',-1,1);

f=tabul(areas,'i');

x=f(:,1);

y=f(:,2);

bar(x,y);

areas=read('tabul areas.txt',-1,1);

f=tabul(areas,'i');

x=f(:,1);

fi=find(xx<=600)

fi=find(xx>=500);

x=x(fi);

y=f(:,2);

y=y(fi);

bar(x,y);



A8 – Morphological Operations

Part I. Paper and pencil
First weee predicted the effects of erosion and dilation of different shapes ,namely, square (50×50) , a triangle (base = 50 , height= 30), a circle (radius 25), a hollow square (60×60, edges are 4 pixels thick), and a plus sign (8 pixels thick and 50 pixels long for each line). Each shape is individually dilated and eroded using the following structuring elements or SE :
1. 4×4 ones
2. 2×4 ones
3. 4×2 ones
4. cross, 5 pixels long, one pixel thick.




Figure 1. Triangle ( a.) top : original shape, (b.) middle: dilation and (c.) bottom: erosion.

Figure 2. Circle ( a.) top : original shape, (b.) middle: dilation and (c.) bottom: erosion.

Figure 3. Hollow square ( a.) top : original shape, (b.) middle: dilation and (c.) bottom: erosion.

Figure 4. Square ( a.) top : original shape, (b.) middle: dilation and (c.) bottom: erosion.


Figure 5. Plus ( a.) top : original shape, (b.) middle: dilation and (c.) bottom: erosion.

'under construction'