Wednesday, September 25, 2013

Neural Network

Using the features from the pattern recognition activity, classification can be done using neural network. For this activity, the result is still not what is expected and still need to improve the training

For now... here are my results for the training set


Even when binary numbers were used, still the result was not what is desired for the training set.

__________
References
[1] Soriano, M. "A15 – Neural Networks." AP 186 Laboratory Manual. National Institute of Physics, University of the Philippines, Diliman. 2013.

Monday, September 16, 2013

Pattern Recognition

Human can easily identify objects and we've been able to do this by looking for object features or patterns like color, shape, size, etc. The process of identifying objects through the use of computer is of major interest in Pattern Recognition. To be able to imitate this complex ability of humans would be amazing. In the next generations, we will have our computers do everything for us.

In pattern recognition, two steps are involved - feature extraction and classification. Choosing the feature of the object is crucial for distinguishing it from the set of classes. I choose the set of cereals with the three classes: honey gold flakes, koko krunch and honey stars. The feature that obviously differentiate the three classes is the color. Actually, it's been hard for me to choose my classes. It's either not good for classification or been taken already by my classmates.

Figure below presents the training set.


Figure below shows the plot of features of the cereals based on color (red and green chromaticity value). The X marks represent the mean points of each class. 


Now for the test set, the comparison of the distance measurement will be able to classify the cereal. The one having shortest distance from the test object will be the predicted class. Table below shows the predictions. 


Out of 9 test objects, 7 were correctly predicted, that is 77.8 % accuracy. Color feature were fairly enough to differentiate the cereals however, I think the prediction would be more accurate if more features were extracted like the eccentricity since the stars would have big difference on the eccentricity of the circle cereals. However, I lack the time to do another feature extraction so I sincerely apologize for disappointing my readers. :P 

I give myself a grade of 9/10 for at least doing the objective of the activity even I only extract one feature which is the color. 

__________
References
[1] Marcos, S. and Soriano, M. "A14 – Pattern Recognition 1." AP 186 Laboratory Manual. National Institute of Physics, University of the Philippines, Diliman. 2013.

the following sites are where the cereals pictures are taken
http://breakfastlullaby.blogspot.com/2012/11/nestle-honey-gold-flakes.html
http://www.seriouseats.com/2012/07/3-cereals-from-singapore.html
http://fun-fanie.blogspot.com/2011/01/di-saat-kehamilan-begini-makanan-yang.html
http://wayfaringchocolate.com/2012/03/29/march-bits-and-bobs-pancakes-asian-sweets-and-advice-we-
http://nicolekissdiet.blogspot.com/2008/02/koko-krunch-duo.html

Friday, September 13, 2013

Not Just Your Usual Image Compression

In this activity, an image is compressed in not just the ordinary way that you know. The main reason why we want to compress images is to reduce the image size. Our cameras are continuously advancing and the information that it can gather from the object are almost limitless, in other words, high-def. But some information are not necessary. On way of compressing image is through principal component analysis (PCA). [1]

To clearly demonstrate the compression, I use a very large image of a clownfish taken from National Geographic website [2] as shown below. It has a dimension of 1600 x 1200 pixels and size of 2.12 MB - truly a BIG image. However, the very big original image encountered a problem on the working stack size in the Scilab. Even I maxed out the stack size, it really wasn't able to operate on very large scale. So, I need to resize the image to 800 x 600 pixels resulting to size of 388 KB - still relatively large.


The image was first converted to grayscale. The image was then cut into 10 x 10 pixels subblocks then concatenated into single column matrix with 100 elements. The concatenated subblocks was arranged into n x p matrix x where n is the number of subblocks and p is the number of elements in each block. We then get the pca of x. In Scilab, the following line was used.
[lambda,facpr,comprinc] = pca(double(x))

The facpr matrix contains the eigenvectors or eigenimages that composed the image, with the first column to have the highest contribution or weight to the original image and so on. The eigenvalue or the constant value

From the grayscaled image below...


the reconstructed images using different numbers of eigenimages are presented. From the lamda, the corresponding weight of each eigenimages was known. The cumsum() function will determine how many eigenvectors will achieve a specific percentage in reconstruction. One eigenimage already correspond to 95.1% of the original image. On the other hand, using 5, 20, 85 and 100 eigenimages result to 97.9%, 99.0%, 99.9% and 100% of reconstruction, respectively. 


For each reconstruction, compression of the image were made. From the grayscaled image original size of 183 KB, the reconstructed images resulted to decrease in size to 89.0 KB, 80.8 KB, 94.5 KB and 174 KB using 1, 5, 20 and 85 eigenimages, respectively. The decrease were 51.4%, 55.8%, 48.4% and 4.9%.

Using all 100 eigenimages (100% reconstruction), doesn't have much difference from when 85 eigenimages were used. Also, no compression had took place. The image size resulted to 183 KB, the same size as the original image. 



Now, let's put some colors to our reconstruction


The image size resulted to 112 KB, 97.6 KB, 114 KB and 195 KB. From the original 388 KB image size, the percent decrease were 71.1%, 74.8%, 70.6% and 49.7%. The compression was observed to be greater for the colored image. 

I thank Phoebe for helping me debug my code. I am giving myself a grade of 11/10 for accomplishing the goal of the activity and doing the reconstruction in colors. 

__________
References
[1] Soriano, M. "A13 – Image Compression." AP 186 Laboratory Manual. National Institute of Physics, University of the Philippines, Diliman. 2013.
[2] Clownfish and Bubble-Tipped Anemone. Photograph by David Doubilet. Retrieved from 
http://photography.nationalgeographic.com/photography/photo-of-the-day/clownfish-bubble-tipped-anemone/

Sunday, September 1, 2013

The code of music

Going over the title, you may misunderstand the point of this blog post. So I will have to tell it as early as now. This is to producing music through Scilab. Yes! Some functions will be able Scilab to sing. I will just be simple but what we want to accomplish in this activity is to make the Scilab read the notes in a music sheet.

I am a frustrated pianist. I was taught to play piano when I was in elementary but things (like my laziness and impatience in learning.. well I'm just a kid then so I do have more significant matters to be taken care of :P) lead to another path and I wasn't able to learn much. Well, I regret missing that chance because I really have a passion for music. It just that I am not the best suited for it so let's just stay admiring these work of art. :)

At first, I really thought that it would be tedious in making this activity work. I listed below some of the things that I think what are needed to be considered.

(a) make the Scilab know the pitch of the note (if a C, D, E, etc);
(b) make the Scilab know the tempo of the note (or the duration, depends if a whole note, quarter note, etc.); which will both require to
(c) have the right filtered image

Better start losing sweat! The song that I chose is Old MacDonald, one simple nursery song because we don't want to make our life more difficult. Below is the music sheet for Old MacDonald in C Major [2]. Again this is simple because there were no flats and sharps :)


The music sheet was first cropped so we can handle on the first line for the mean time.
 

The image must be binarized first, if it is not a binary image. Then the color of the image was inverted. As I worked through my code, the task seems more problematic than I thought. The first problem that I encountered: problem (c) or filtering the image. I instantly thought that using a circle structuring element then applying open operator would isolate the notes. It pretty much did however, the half note would not be included. So I think of another way. It was to remove the vertical and horizontal line. Shown in below is the step by step cleaning of the image. First, removing the vertical, then horizontal lines, both using the TopHat() function. Unfortunately, there really parts of the note that would be removed in performing these steps so the CloseImage() function was used.


I will now be having my blobs and using the FilterBySize() would remove the unnecessary small blobs. We now have the following blobs.


We now arrive to problem (a) mentioned above. How will the Scilab know the ptich of the note? In the music sheet, the pitch was differentiated through the location of the note in the staff (the one with the five horizontal lines). Thus, those position should also be recognized by the Scilab. AnalyzeBlobs function in Scilab measures the location of the centroid of each blob. In Gimp, the range of pixel location of each line and space along the y-axis were determined. These were incorporated as conditions for the Scilab to know the right pitch. We should now face problem (b). Since, half notes are not solid circles, we can deduce that their areas are different from the quarter notes. So again, conditions were made to determine the tempo.

The whole process of making the Scilab read notes seems successful. Below is the output.


This task really has been tedious. I spend a lot of time in determining the location range because the you have to consider that there are different range for spaces and lines. Though once established correctly, you're okay. For the next line, however the next staff will be another problem because the pixel position or ranges of spaces and line would be different when another cropping of image was made. The practical thing to do is to work to match the pixel positions of spaces and lines of each staff. In addition, working on the tempo is really a killer. I am lucky that the first staff only contains only half and quarter notes. I able to distinguish between the two notes. There still whole, eighth and sixteenth notes and the dot beside the note. The eighth and sixteenth notes is notable for the one bar and two bars above their musical symbol, respectively. I plan on working on those by determining their position along the x-axis and if they are reasonably close to the blob representing the note, the tempo of the note will be known. Still, these need a lot of work to be accomplished. Maybe I'll try it when I had the time, including the rests in the music scores.

I am thankful for the initial code that Ma'am has provided us that make the Scilab produce different pitch. I also thank Aeriel Constantino and Phoebe Gallanosa for the useful discussion. I believe I deserve a 10/10 in this Activity because technically, I accomplished the task at hand.

__________
References
[1] Soriano, M. "Playing Notes by Image Processing." AP 186 Laboratory Manual. National Institute of Physics, University of the Philippines, Diliman. 2013.
[2] http://www.pianolessons4children.com/songs/old-mcdonald.php
[3] http://www.ukuleles.com/spreadsheets/Guitnote.XLS (for the frequency of other pitch)