Sunday, September 29, 2013

Neural Networks

This activity introduces another Scilab toolbox which is the ANN toolbox. The ANN toolbox is a data analysis and statistics module for designing neural networks. This activity is the same with that of the pattern recognition activity. The difference is the approach made.

Neural networks simulates the human brain process done by the neurons. In simplified terms, this is the decision making simulation. Neural networks are self-training process that limits the coder interference in decision making.

The process according to the activity manual, the an input will be introduced to the neural network. These can be multiple inputs. all of the inputs goes through a hidden layer where the inputs will be compared to the trained knowledge in the memory of the network. There are two possibilities in this hidden layer. If all attributes are found to be true to a specific class, the output will be the corresponding label of that class (the error will be at minimum). If the attributes is not the same (large error), it will move on to another class and repeat the process until the corresponding class is found.

For this activity, I used the objects and information from the previous activity. I used 4 attributes as inputs and 400 iterations for the training. I attribute class 0 for the peanuts and class 1 for the calamansi. The output I obtained is shown in Figure 1.
Figure 1. Output for 4 inputs and 400 iterations.

From this it can be said that the neural network method of classifying objects worked! Exploring further, I tried to determine the class output if only 2 inputs are used. The result I obtained are shown in Figure 2.
Figure 2. Outputs for 2 inputs 400 iterations.

From my results, it can be seen that 2 inputs were not enough to classify the objects to their corresponding groups. Lastly, I tried to increase the iteration done by the network. The output I got is shown in Figure 3.
Figure 3. Outputs for 4 inputs and 1000 iterations.

The result I got classified the objects to their correct groups. The difference is that the output of objects in call 0 has a value nearer to 0 and class 1 objects are nearer to 1 when compared to that of the 400 iterations.

For this activity I give myself a grade of 12/10 because I was able to classify the objects in their corresponding groups and I was able to determine the effect of less inputs and more iterations into the output.

[1] Soriano, Maricor. Activity 15 manual.

Pattern Recognition

In this activity, the goal is to recognize different objects to their corresponding classes using specific characteristics of the objects. The objects that I have used in this activity are peanuts and calamansi which are shown in Figure 1. I used 8 samples for each object. Four of the 8 are used as training while the remaining 4 are used to test the method.

 
Figure 1. Object used for Pattern Recognition.

The peanuts are set into class 1 while the calamnsi were set into class 2. The object characteristics I used are the area of the object, and their values for the R-, G- and B- channels. The training objects and the test objects I used are shown in Figure 2. The corresponding characteristics of the training objects are shown in Figure 3 and Figure 4 shows the characteristics of the test objects.

Figure 2. The train and test objects used.

Figure 3. Characteristics information of the train objects.

Figure 4. Characteristics information of the test object and their corresponding classes.

I determined the membership of each test object by obtaining the difference each object characteristics from the mean of the train objects. If the test object is a member of the class, the sum of their deviation must be smaller than that of the other class. Therefore using this method, I was able to determine the class of the 7 test objects. I was able to determine correctly the class of my 7 test objects and so I rate the method to be 100% reliable.

I grade myself 10/10 for this activity because I was able to put the test objects in their corresponding classes. 

[1] Soriano, Maricor. Activity 14 manual.

Image Compression

This activity, I used Scilab to reconstruct an image using the PCA function in Scilab. PCA stands for Principal Component Analysis. This method utilizes the eigenvalues and eigenvectors idea. Since I was dealing with images, the components are called eigenimages. The idea is that all images can be reconstructed by superimposing the eigenimages multiplied to their corresponding weights or the eigenvalues.

The image that I used for this activity is my favorite fruit of all time.... bananas! This is shown in Figure 1. I obtained this image from Wikepedia. [1] The main function that I used is pca(). This function produces 3 outputs: lambda, comprinc and facpr. Each represents different information. The 2 outputs that I used in this activity is facpr and comprinc. Facpr represents the eigenimages and comprinc respresents the eigenvalues corresponding to the eigenimages.

Figure 1. Original image to be compressed.

Using the pca() function, the eigenimages that I obtained is shown Figure 2.
Figure 2. Eigenimages of the original image.

From each of the eigenimages, a weight is multiplied to it to determine which of this images contributes to the desired image. There are 100 eigenimages to choose from, some contributed more than the others and the comprinc values determine that. The reconstructed image is shown below in Figure 3.

Figure 3. Reconstructed image using the PCA method.

This is a cool process to compress and decompress images that is transferred through limited media. Sending the eigenimages saves up space but the quality is not compromised. For this activity, I give myself a grade of 10/10 because I was able to reconstruct my image.

[1] http://en.wikipedia.org/wiki/Banana
[2] Soriano, Maricor. Activity 13 manual.

Playing Notes in Scilab

The focus of this activity is to obtain a music sheet of a song and translate it to actual sound using Scilab. The music that I chose is an American folk song entitled Oh My Darling, Clementine. I learned this song when I was in grade school. Back then, I learned to read music sheets and this song has been our staple example. I liked this song because it is quite easy to play in the piano but the emotion of the song is so strong.

For this activity I chose to play only the first staff of the music sheet. I got the music sheet from the Free Notes website [1]. I removed the unnecessary texts in the sheet and thus obtained Figure 1.
Figure 1. First musical staff of "Oh My Darling Clementine".

I used Scilab to extract the position of the centroid of the notes. I obtained that information by using imcomplement() to reverse the image. The dark pixels become white pixels and vice versa. I removed the horizontal and vertical lines using the vertical and horizontal structure elements respectively. The final processed image is shown in Figure 2.

Figure 2. Processed image of Figure 1.

Obtaining the pixel position of the centroid of each blob, I can translate it to sound. The x pixel values represent time and the y pixel values represent the frequency of the notes. Using the sound() function of Scilab I was able to play the music which is attached below.



For this activity I give myself a grade of 10/10 because I was able to do the required tasks. Sorry for the late upload.

[1] http://www.free-notes.net/cgi-bin/noten_Song.pl?song=Oh+My+Darling+Clementine&profile=null&lang=en&db=Main 
[2] Soriano, Maricor. Activity 12 manual.

Monday, August 19, 2013

Binary Operations



Cancer has been and always will be the scariest disease without a direct cure in modern times. Cancer or more specifically, cancer cells are cells in our body that has malfunctioned. They have malfunctioned in the sense that they don’t want to die. They continue to divide and grow in abnormal proportions. Typically cells in the body self destruct when they arrived at their limit or when it is not functioning properly. What’s scary about this disease is that symptoms are usually undetected until the disease has spread in a wide area of the body. Therefore it is necessary to be able to detect the existence of these cells and kill them immediately.


There are many ways to detect cancer cells and today, I will show you one way to do it. This method is through image processing. Two images will be processed. The first image consists of normal cells. From these, the mean size of normal cells and its standard deviation will be determined. With these information at hand, abnormally large cells can be isolated and be tagged as cancer cells. So let’s try doing this method using Scilab’s SIVP and IPD modules.

To start of, figure 1 shows the photo of the "control" cells. To determine the mean and the standard deviation of the cells, its area must be known first. To do that, I segmented this image into 12 subregions with dimension of 256x256. The segmented image is shown in figure 2.

Figure 1. Original image of normal cells.

Figure 2. Segmented images of Figure 1. Each segment has a dimension of 256x256 pixels.

The next process is to convert this image to binary. There are two ways to do this. Fist is by using the functions im2bw(). This function converts an image to binary and the threshold can be specified. The threshold value ranges from 0 to 1. For my code, I used a threshold of 0.84. The second method is by using the function SegmentByThreshold(). This function is similar to im2bw() since the threshold can also be specified but the threshold values range from 0-255. The threshold can be determined using the function CalculateOtsuThreshold(). The Otsu method is a clustering based image thresholding method. Using these two functions for each of the subimages produced results as shown in figure 3. From here, we can see that images after using im2bw() is free of noise but the structures of interest are not fully formed. Using the function SegmentByThreshold() produced sfully formed structures but the background noise was not removed. The Otsu method showed that the 12 images have different threshold value. These values range from 177 to 199. Therefore what I did next was to place a threshold value higher than everyone else, the value I used is 206. As we can see, the image noise was reduced but still it is still present in some images. 

I now use morphological operations, which was the activity last time, to fully clean up the image. There are three functions that I chose from: OpenImage(), CloseImage() and TopHat(). The function OpenImage() applies an opening filter to the image. This means that the image is first eroded and is subsequently dilated. Therefore this process retains and remove light objects where the structuring element does not fit. The function CloseImage() is just the inverse. The image is first dilated and then eroded. Lastly, the TopHat() function that emphasizes light objects where the structure element does not fit. Since the image is just cells, the structure element I used was a circle. What I did to clean the image is to use OpenImage() and then CloseImage(). I thought that TopHat() was unnecessary. The cleaned images are also shown in figure 3.

Figure 3. Segmented image converted to binary and cleaned.

With clean images, the next thing is to determine the area of each clusters. To do this, I used the function SearchBlobs() to tag each cluster to a separate number. What this function do is to change the value of the matrix to the designation number. Therefore the image matrix is converted from binary (1 and 0) to values of 0 to the max number of blobs. Figures 4 to 15 shows each of the blobs per image. As it can be seen, some blob number doesn't contain blobs. Technically, this function "searches the number of connected regions in a binary image. All pixels belonging to the same connected region are assigned the same number. All pixels that do not belong to a region are background pixels and have the number 0." So maybe the blob numbers that doesn't contain elements are regions that are surrounded by blobs. It is not really connected to the background but it is not also connected to the clusters. 

To determine the area, I took the length where the matrix has a certain value. Taking the length means determining how many matrix element has this value which is basically the area. For example, looking at the fifth blob, I took the length in which the image matrix has a value of 5. I did this to all the blobs in all the images and they are shown from figures 4 to 15. 
 
Figure 4. Blobs and their corresponding areas for subimage 1.

Figure 5. Blobs and their corresponding areas for subimage 2.

Figure 6. Blobs and their corresponding areas for subimage 3.

Figure 7. Blobs and their corresponding areas for subimage 4.

Figure 8. Blobs and their corresponding areas for subimage 5.

Figure 9. Blobs and their corresponding areas for subimage 6.

Figure 10. Blobs and their corresponding areas for subimage 7.

Figure 11. Blobs and their corresponding areas for subimage 8.

Figure 12. Blobs and their corresponding areas for subimage 9.

Figure 13. Blobs and their corresponding areas for subimage 10.

Figure 14. Blobs and their corresponding areas for subimage 11.

Figure 15. Blobs and their corresponding areas for subimage 12.

To get the mean, I took all the blob areas of the 12 subimages taking note that some blobs are connected to other blobs in another images. Also some of this blobs are repeated since the image cannot be divided exactly by the specified dimensions. Dividing this the sum of all the areas by the total number of blobs, I was able to determine its mean to be 439.42 pixels. The standard deviation was determined to be 69.28. Now that these values are known, I was able to isolate cancer cells to an image of mixed normal and cancer cells. The image I used for this second part is shown below. Again I repeated all the process I did until the area determination. Figure 17 shows the cleaned image. Using the SearchBlobs() function, 76 blobs were found.
Figure 16. Image of normal cells mixed with cancer cells.

Figure 17. binary converted and cleaned image of figure 16.


Again, all blob numbers don't contain blobs of interest. Figure 18 shows each blob with their corresponding areas. To isolate abnormally large cells, I made a circular structure element with radius of that corresponding to the mean+standard deviation. I used 3 structure elements, the first is for the mean + 1StDev value, the second is the mean + 2 StDev value and the third is for mean + 3 StDev value. These values are 12.72, 13.56 and 14.35 respectively. Again I used the OpenImage() followed by CloseImage() as filters. Both of them used the same structure element. The filtered images for 1, 2 and 3 standard deviations are shown in figures 19 to 21.



Figure 18. Blobs and their corresponding areas of figure 16.

Figure 19. Filtered cells whose areas are above the mean + 1 standard deviation. 

Figure 20. Filtered cells whose areas are above the mean + 2 standard deviation. 

Figure 21. Filtered cells whose areas are above the mean + 3 standard deviation. 

For this activity, I give myself a grade of 12/10. This activity, after a long time, was on time. (Yey!) I was able to do everything that was asked. I think I did all of it properly. I thank Ma'am Jing's activity manual, Wikepedia and Scilab Help for giving me the information needed to do this activity. 

Saturday, August 10, 2013

Morphological Operations


Morphological operations are done to binary images. This operations affect the resulting shape of an object. There are two common types of morphological operations. The first is dilation. The dilation in tha mathematical sense is given by the equation
.
By looking at the word, dilation can be defines as something that grew wider. By looking at the results obtained, it can be seen that this colloquial definition still holds true. The second is erosion. Mathematically, erosion is defined as

Erosion is the opposite of dilation. Which means that erosion shrinks the object it applied to which in our case is an image. The code for this activity is easy to do because all of them are already defined in the IPD toolbox of Scilab. Simple matrix construction was done to create the objects in which the morphological operations  act upon. The structure element was done using the function CreateStructureElement(). Usually the structure element produced by this function are defined by default geometrical shapes but by including the term 'custom' in the function, the user can define and construct a matrix of structure element of their choice. The dilation can be done using the function DilateImage() and subsequently the erosion can be done using the function ErodeImage().
This activity was first done manually using a graphing paper. The result are shown below.







Figure 1. Resulting images for manual morphological operations.
The next is to repeat everything using an algorithm and check the results. The images obtained using the algorithm are shown below. Most of the images obtained for the dilation and erosion are the same with my predictions.


Figure 2. Image results of the dilation of different images and structure elements.


Figure 3. Image results of the erosion of different images and structure elements.
For this experiment, the algorithm was quite easy to make. The predictions was a little bit more tricky because it was quite confusing. I got confused in counting the boxes. For this activity, I give myself a grade of 10/10 because I was able to produce all the necessary outputs.