Thursday, September 23, 2010

Image Compression

Using pca, this activity enabled to compress image memory in small memory packets just by storing the Eigenvectors and eigenvalues.



**CODE**
stacksize(100000000);
for vectors = [1,3,10,20,46];

img = gray_imread('Dock.jpg');
//img = (img(:,:,1) + img(:,:,2) + img(:,:,3))/3;
//imshow(img);

//cut to 10 by 10

k = 0;
for r = 1:20;
for c = 1:40;
itemp = img(((10*r-9):(10*r)) , ((10*c-9):(10*c))); //divides to 10 by 10
xtemp = itemp(:); //columnizes
k = c + (r-1)*40; //makes proper index
x(k,:) = xtemp'; //reconstructs
end
end
//imwrite(itemp, 'itemp.jpg');
//imshow(itemp);

//applying pc
[lambda, facpr, comprinc] = pca(x);



//reconstructing the image
B = [];
for i = 1:800,
A = [];
for j = 1:vectors,
recim = comprinc(i,j) * facpr(:,j);
A = [A recim]
end
A = sum(A, 'c');
B = [B A];
end

C = B';
//test reconstruction
y = zeros(200,400);
k = 0;
for r = 1:20,
for c = 1:40,
k = c +(r-1)*40;
//xtemp = facpr(:,k);
xtemp = C(k,:);
y(((10*r-9):(10*r)), ((10*c-9):(10*c))) = matrix(xtemp,10,10);
end
end
//scf(vectors);
imwrite((y-min(y))/(max(y)-min(y)), 'vectors'+string(vectors)+'.jpg');
//imshow(y);
end

**END of CODE**

What this code does is that it cuts the image into 10x10blocks. Then pca is applied onto it. Thanks to Maam Jing for the sample code that simplifies things. After PCA, the image is reconstructed. In reconstructing the image, each 10x10 block is reconstructed with the comprinc and the facpr from the pca function. Comprinc and facpr is used in image representation as superposition of weighted basis images.


Using this 200x400 pixel image:


200x400 = 80000 pixels
Reconstructed with 3, 20 and 46 eigenvectors:


800 pixels X 3 eigenimages


800pixels * 20 eigenimages


800* 46 eigenimages

In the reconstructed images, only the basis coefficients are saved. In this case, the comprinc. So, there are 800pixels plus the number of eigenvector to be saved. It drastically compressed the image compared to the 400*200pixels to be saved.

I'd rate myself 10/10 for this activity.
Thanks to Maam Jing for the handouts and the basis codes. It may the activity a lot easier. Thanks to my roomates for helping me figure out the reconstruction part of the compressed images and some debugging too. And for teaching me to append in scilab.

Color Image Segmentation





This is another type of segmentation. This uses the RBG values and not the Grayscale values to segment colors.

This uses normalized chromaticity coordinates or NCC. In NCC, the RGB values of the image is convertible to just r and g. where r is R/I and I = R+G+B.


NCC example. Y-axis is r and X axis is g.

This image is subject for segmentation:

A bird

There are two way to segment colors. First is the parametric probability distribution.
It the probability of finding the color in the ROI in the image is given by:

where r is the pixel in the image in the red.(it may be in the green too). Sigma and mu are standard deviation and mean of the selected color in the ROI respectively.

The color segmentation in parametric probability distribution is given by:





parametric pdf

In non-parametric, 2D histogram and backprojection are used. The histogram is used to determine the pdf of the selected pixel in the ROI. Then they are backprojected to reconstruct the segmentation. Resulting image is shown below:


non-parametric pdf

This is the histogram of the ROI.
This is the histogram of the image.

Those two are different in a way that the non-parametric part has brighter values for most of the parts. There are parts in the non-parametric that have gray values while in the parametric has black values. Maybe this difference is from how they are obtained. Parametric attains the Gaussian form while non-parametric uses the histogram.



I'd give myself 8/10 for this activity. Thanks to Andy and Gino for helping me code.
Thanks to Maam Jing for the histogram backprojection code and the handout.

Color Image Processing

This activity, we were taught about colors, taking pictures and camera features such as white balancing. White balancing ensures that the captured image is has proper values for white and for all other colors as well.

To achieve white balancing, there are 2 popular algorithms - the White Patch Algorithm and the Gray World algorithms. Their difference is that, normalization factor that fixes the color rendered. The white patch algorithm obtains white values to a white patch in the image while gray world algorithm obtains the mean value of the whole image.

Using images captured at different light sources with different white balance, respective White Patch and Gray World algorithms are shown below.


In White patch algorithm, one will pick the worst ill- white balanced object. Then, the white value of that is used to normalize all the color values for the rest of the pictures of the same illumination.

I noticed that they form kind of SEPIA-ish,...

Using the hues on the otherhand, GW and WP algorithms are also used. And it yielded this result.


With the two algorithms, I think the WP algorithm is better. From the result, there are black points in the GW algorithm. Maybe they are information losses. And I also think that, the Rw Gw Bw obtained in WP is better because you are getting the value of white in the worst white balanced object. Also, Rw, Gw and Bw are values for white. White is the maxima per RGB. It is the most proper to use as normalization factor. Unlike in the GW that the mean is being used as Rw Gw Bw.


I'd grade myself 9/10 for this activity. It is fun though to mess around in white balancing.
Thanks to May Ann for the images. We don't have a proper camera to do capture the image.
Thanks Maam Jing for the input on GW and WP algorithms.

Playing Musical Notes by Image Processing

An integration activity that will use all the image processing techniques to be able to play musical notes from a scanned music sheet.

I selected a simple music sheet to show the proof of principle in doing this.


simple version of old mcdonald

This is the code I used to be able to play the note.

** Code**
stacksize(100000000);
Score = gray_imread('ScoreSheet2.png'); // must be columnar
Score1 = im2bw(Score, 0.1);
SE1 = gray_imread('SEnote12.png');
SE1 = im2bw(SE1, 0.1);

//correlate
Score = conj(fft2(Score1));
SE = fft2(SE1);
Corr = Score.*SE;
A = abs(fftshift(fft2(Corr)));
//imshow(Img, []);
imwrite((A-min(A))/(max(A)-min(A)), 'test1.png');

function n = note(f, t)
n = sin (2*%pi*f*t);
endfunction;
C = 261.63*2;
D = 293.66*2;
E = 329.63*2;

C5 = 523.25*2;
G4 = 392.00*2;
A4 = 440.00*2;
E5 = 659.26*2;
D5 = 587.33*2;

t=soundsec(0.25);
//s = [note(E,t), note(D,t),note(C,t), note(D,t), note(E,t) note(E,t) note(E,t)];
//sound(s);

//image thing
Img = gray_imread('test1.png');
Img2 = im2bw(Img, 0.9);
imwrite(Img2, 'test2.png');
[L,R] = bwlabel(Img2);
ycoor = [];
poresize = [];
s = [];
for j = 1:max(R),
[r,c] = find(L==j);
rc = [r' c'];
x = min(rc(:,2));
a = [size(rc(:,2))];
poresize = [poresize; a];
// xcoor = [xcoor; r'];
ycoor = [ycoor; x];
end

for k = 1:length(ycoor);
if ycoor(k) == 35,
s = [s, note(C5,t)];
elseif ycoor(k) == 20,
s = [s, note(G4,t)];
elseif ycoor(k) == 25,
s = [s, note(A4,t)];
elseif ycoor(k) == 46,
s = [s, note(E5,t)];
elseif ycoor(k) == 41,
s = [s, note(D5,t)];
end
end
sound(s);
wavwrite(s, 'mcdonald.wav');

**END of CODE**

Using this code, it yielded this:




This code uses correlation, Fourier transformation and bwlabel. There are several limitation to this code. First off, since there are only quarter notes, timing is constant. I have an idea on how to modify it to be able to vary the timing. It also involves correlation with other types of notes. :)
Another one is that notes are to be binarized, and rotated with the first note on top. because I used the ever powerful bwlabel. the blob indicators of the bwlabel is used for chronology purposes. Meaning, the first note is the top most with blob value 1 and so on.


I was also thinking of a possibility to use Fourier filtering to filter out the staff so that only notes will be left. Use bwlabel function to label the notes from 1 to last note. After that, use a for loop, then every loop, correlate every note. This correlation will identify the type of note and the timing. Store every information and then the information can be played. Voila! I haven't tried that method though.

I think I can have aroung10/10 for this activity. I haven't tested the prcedures on my mind regarding this activity but there are a lot of possibilities. It is exciting.

Thanks Maam Jing for the note function. And my roomates to fix my little coding mistakes.

Binary Operations

This activity is a very important application in morphological operations. The region of interest has given importance here. As well as the investigation of closing and opening operators which is just made up of erosion and dilation operations. These enhances binarized images for analysis.

This time, analysis in on segmentation of simulated cancer cells which are usually larger than normal cells.

This is done by having several regions of interests. Regions of interests are done by creating subimages of the image to be studied. Below shows the original image and the subimages. The subimages may overlap and has a dimension of 256 by 256 pixels. Each subimages are to be examined for the normal size of the cell. These values are used to have an estimate size for the normal cell.


The left is the representation of the normal cells and the right is the subimages.

The subimages are then binarized.

I assumed they are circles so, I used a circular structuring element. I tried the closing operation wherein it is just the erosion of the dilation of the image and the structuring element. And below is what happened.

Binarized images when closed.

In this method, there are a lot of clusters and only a few normal sized cells remained. The cell area that will be obtained here will have a lot of standard deviation.

Next, I tried the opening operator wherein it is the dilation of the erosion of the image and its structure factor. And I got this image:

There are few clusters and the normal sized cells are defined. This is better than closing in in finding out the area of the cells.

BWLABEL function is very cool. It names the blobs and give the coordinates of each pixel in the blob. Using this function, the area per blob/ cell is easily obtained.

After obtaining the cell area, I constructed a structure factor in relation to the area of the normal cell I obtained. Take note that this obtained area has a mean and standard deviation. Thus, the area of the normal cell is a range.

Using another opening function with the binarized image of the collection of cells with cancer and normal cells as well as the structure factor constructed in realtion to the normal cell area, I have successfully segregated the cancer cells. Shown below:


Left are segregated cancer cells form the collection of cells in the right.


I'd say i will get 10/10 for this activity. bwlabel() is very powerful. I have some ideas to apply this function in different applications.

Thanks Maam Jing for this activity and several inputs. Also to my roomates in helping me code.

Wednesday, September 22, 2010

Morphological Operations







In morphological operations, we were introduced with the erode and dilate operators.
Dilation expands the object relative to a structuring element, while erosion shrinks it.

A sample dilation is when a 5x5 ones is dilated by a structuring element of 1x2, my predicted dilation is shown below

Predicted dilation is a 6x5 pixel of ones.

Dilation from scilab is a 5x5 of ones(let black be ones)

On the other hand, the erosion of the same image and structuring element is shown below:

Predicted erosion

Simulated erosion

It is seen here that the simulations maintain the size of the main image. whatever information thereis after the size of the image is ommitted just as what happened in the sample dilation. All other erosion and dilation exhibit this.

Skel and Thin

Given an image R,

Image R

When subjected to the skel function, it looked like this:
Image R subjected to skel().

When the image R is subjected to thinning, it looked below:

Image R subjected to thin()

Their difference is that the skel leaves a skeleton like framework in the image while thin is done by border deletion. No framework is considered thus, resulting images are just several white dots indistinguishable to the root image.


This is a very tedious activity. Coding made several error that were worked upon. I'd give myself 9/10 for this activity.

Thanks to Maam Jing's very helpful handouts. Thanks to my roomates in enlightening me further in this concept. And of course scilab help. Credits for the R image goes to scilab help.




Enhancement in the Frequency Domain





This activity utilizes the Fourier space in modifying selected image in order to enhance its quality.
One modification to enhance the image quality using Fourier space is the image filtering. Using the frequency space, Fourier transforms and the convolution theorem, it is possible to filter an image to omit unwanted frequencies. This frequency filtering is affects the whole image in a way that it enhances the image.

Initial exercises in Fourier transforms involved the following:

1) Two dots were constructed and the Fourier transform of it was examined. The reconstructed dots is shown in (1) and the Fourier transform of it is in (2).

(1) Two dots
(2) Fourier transform of two dots.

2) Instead of dots, 2 small circles were constructed in a way that the radius is to be varied. Shown below are Two small circles with radius 5 and 20 pixels as well as their Fourier transforms. It is observed that as the radius increases, the central circle in the Fourier domain decreases. Also, at radius equal to 5, irregularities in the somewhat circle of the Fourier transform is more defined that that in the Fourier transform of radius equal to 20.


Two dots with r= 5
Two dots with r= 20
Fourier transform of two dots with r= 5
Fourier transform of two dots with r = 20

Also more variations were done such as squares with different width and Gaussians with varying variance. Their constructed images and Fourier transforms are shown below.

Squares

Squares with width = 5
Squares with w = 20
Fourier transform of Squares with w w = 5
Fourier transform of Squares with w = 20

Gaussians

Gaussians with variance = 10
Gaussians with variance = 20
Fourier transform of Gaussians with variance = 10
Fourier transform of Gaussians with variance = 20

3) In this exercise, we are to familiarize with the convolution and what it does. Based upon the observation, when 2 images are convolved, the resulting image is somewhat the both images combined. It has attributes of both images. In the first exercise, a random array of ones and a patter is convolved to produce an image. The patter is shown below as well as the pattern and the convolved image.

Random Ones

Array of Ones randomly distributed


Pattern used. It looks like a "T".

Convolved image with the ones obtaining the form of the pattern.

4) a 200x200 array of ones were made. The Fourier transform of this was obtained. Another array of ones were done and this time, the separation was changed. Shown below are the result.

Array of Ones (f defines the separation)

Array of ones with f = 100
Fourier transform of Array of ones with f= 100

Array of Ones with f = 10
Fourier transform of Array of ones with f = 10.

It is seen that the peaks or the white pixel in the Fourier space increases it separation as the f is decreased.

Enhancement.

Fingerprints.Ridge Enhancement


Fingerprint images were enhance using convolution. Given the Fourier transform of the fingerprint image to be enhanced, a certain mask is convolved onto it to be able to filter out the undesirable frequencies that reduces the quality of the image. Using this Fourier transform-mask-convolved-inverse Fourier transform method, the fingerprint image was enhanced. results are shown below.







Fingerprint image thanks to http://www.webschool.org.uk/revision/archfprint.jpg


Mask used.

Resulting Image. This is inverted due to the fast fourier transform.
There are more defined sections of the fingerprints that are darker in the original image.

Line Removal.
In this section, the method of enhancement is also the same. It involves a mask in the Fourier space to filter out undesired frequencies.

from this image,

Image of lunar landing. Note the vertical lines.

yielded this image:

Reconstructed image without the vertical lines.

CanvasWeave.

A subject painting was used in this part. Same method of concept that involves filtering is used. Initial painting is shown below:

Canvas Weave.

Enhanced Canvas weave. There are less dots in the painting.

This is a fun activity. Specially the enhancement part. I'd like to thank Maam Jing for most of the intellectual inputs that served as stepping stones to complete this activity.

I'd say, I got 10/10 in this activity.