Martes, Agosto 22, 2017

Masked Figures: Scilab Basics

I was struggling to find how to turn off the axis and change the aspect ratio of the figure. given that scilab has poor documentation, the task was twice harder. After more than a week of pressure, I finally learned how to do it. Hahaha. The code is shown in figure 1.

Figure 1. Code for setting tick marks off

For the activity,we were tasked to try basic matrix operations in scilab such as matrix multiplications, element wise operations, or creating masks to simulate images from matrices. One of the key knowledge here is the use of masks to multiply to matrices to be able to have the desired look of the image. The example code produced an image with a centered circle.

Figure 2. The code for a centered circle with black background
From figure 2, the first parts of the code created a grid for the matrix of the image. Scilab works mostly with matrices so in this case from line 2, x, which is an 1 by 50 matrix was made into X, a 50 by 50 matrix using the function ndgrid. ndgrid is like the meshgrid function in python where it outputs X whose columns have similar elements and rows of varying elements such that 1st column has all 1s and 1st row has 1, 2, 3, .... The Y has rows of similar elements and columns of varying elements. From line 5, r produced concentric circled of different radius depending on the x and y values. So, throughout the activity, the matrix A whose initial values are all zeros, will be used as the base matrix to produce the synthetic images. For the centered circle, a mask for A was used pointing to the region of interest, which is the area r < 0.5 to create a circle of radius 0.5. This was done using a condition for A and changing the element values of A to 1. The next lines of the code are for creating the window where the figure will be shown. f = scf() creates the figure window and f.color_map changes the rendering of the images from numbers to pixel colors. xs2png saves the image.
Figure 3 shows the desired image(left) and the 3D projection of the image(right) where the values of the matrix elements are represented by the z-axis values. In this case, the white pixels in the image has a value of 1 in the z-axis of the 3D projection. The 3D view will be used to confirm if the xy-view of the image corresponds to the required output.


Figure 3. A centered circle (left) and the mesh of the image showing the 3D view where the z axis are the colors of the image(right) showing a cylindrical object

Figure 4. The code for creating a centered square aperture where a mask was used to create an aperture with a value of 1. 

To create the square aperture, the X and Y matrices can be used by just creating an AND condition using &. Without the &, a cross will be produced since for example in the X condition, the Y coordinates of the elements will not be considered. It can still work however by adding additional lines. So far, this is the shortest was I can think of consisting of 2 lines for the mask. The reformulation of the X and Y matrices using absolute value was used so that fewer lines will be need to produce a centered square with the center at 0,0.
Figure 5. Centered square aperture(left) and the mesh of the matrix(right) where the white pixels has values of 1 in the z coordinate similar to a box.

Figure 6. The code for creating a matrix with a sinusoid along the x-direction similar to a corrugated roof

For the corrugated roof along the x-direction, the sinusoidal mask was multiplied to the matrix A, which is a sin of the X matrix. This creates a matrix with sinusoidal row elements. Figure 7 shows a corrugated roof and the sinusoidal shape was confirmed by the 3D perspective. A cute variation can be done by multiplying a mask that is changing along Y.

Figure 7. Sinusoid along the x-direction similar to a corrugated roof (left) and the 3D perspective of the matrix showing the undulation along the x-direction. In the figure, the reference x-axis was labeled Y by scilab. X-axis refers to the horizontal. 
Figure 8. The code for creating a grating along the x-direction. 
For the grating, the previous sin function was used with an added condition to binarize the image by making the values 1 when greater than a threshold and 0 when less. The threshold can be varied to change the grating width. Figure 9 shows the output image with a square wave when projected in 3D.
Figure 9. Grating along the x-direction (left) and the 3D perspective of the image(right)

Figure 10. The code for an annulus created by a circle masked with a smaller circle
For the annulus, the previous method of making a centered circle could be done but now, using 2 circles with different radii. The difference will determine the thickness of the annulus. The image produced in figure 11 shows a hollow cylindrical object when projected in 3D.
Figure 11. White annulus (left) and the 3D perspective showing a hollow cylindrical object (right)

Figure 12. The code for creating a circular aperture with graded Gaussian transparency. 
For the circular aperture with graded transparency, the code for the centered circle can be used. However, a 2D gaussian mask should be multiplied with the base matrix A first before obtaining the circular aperture. In my case, I started with a matrix of 1s and multiplied a gaussian along X and along Y. This created a 2D gaussian surface. Then, the elements greater than the desired radius could be rewritten as 0 to give it color black. Figure 13 shows the output image with the center having the whitest color and the 3D projection similar to a dome, or a fluffy chiffon cake. The output image will be dependent on the mu, sigma, and the radius of the desired aperture. Variations can be done by changing mu to a non zero value or using a different mu and sigma along x and along y, crating a lopsided transparency, or a lopsided dome for the 3D projection. The sigma could also be given a value that is a function of the radius to adjust the transparency while varying the radius similar to that of a lens.
Figure 13. Circular aperture with graded or Gaussian transparency (left) and the 3D perspective showing an object similar to a dome (right)

Figure 14. The code for creating an ellipse using  the formula for an ellipse

For the ellipse, the equation for the ellipse was used and the condition for extracting an ellipse was done similar to that in the case of the centered circle. The image is dependent on the center and the denominator in the equation used. In my case, I used 0,0 as the center and 3 for the x and 4 for the y, creating an ellipse elongated along y. 

Figure 15. Ellipse (left) and the 3D perspective (right) showing an image similar to a leche flan

Figure 16. The code for creating a cross at the middle of the image
For the cross, a conditional similar to the centered square in figure 4 can be used, but this time, the ampersand was not used anymore. The two conditions, along x and along y, was separated as shown in lines 10 and 11 in figure 16. This creates a long cross with ends not detected by the frame of the image. To make is neater, with the edges seen, additional conditions was added which turned the values beyond the edge back to their original values, in this case is 0. The thickness and the length of the cross can be adjusted by changing the values inside the conditionals in lines 10 to 13 in figure 16. The cross can also be tilted by multiplying the X and Y matrices by some sinusoidal value. The center can also be shifted by changing the range of the x and y arrays.
Figure 17. White cross (left) and the 3D perspective (right)

For the whole activity, the pixel size of the images affects the quality of the rendered figures. This is due to the colormap used since scilab tend to connect neighboring pixels by getting the average value of the two neighbors. For example instead of showing the edge between black and white, gray pixels was shown to connect the discontinuous black and white neighboring pixels. For better visualization, a larger pixel size should be used. 

Exploration

For the exploration, I recreated the current logo of the Instrumentation Physics Laboratory, National Institute of Physics, UP Diliman. Looking at figure 18, the logo is made up of circles and half circles making it a bit easier to be reconstructed using basic shapes and masking. With a closer inspection, the shapes in the logo can be divided into similar circles of 4 different radii. The circles with the smallest radius are the grayscale circles at the left most. The x- coordinates of the center of the circles are the same. The next group are the RGB circles with the same x-coordinates for the center. The semi-circles representing concave and convex lens can be done using two circles of the same radius which are spliced for the half of the x-values. The last circle, which is the black sphere, has same radius with the lenses and same y-coordinate for the center with the lenses and the green circle. To make things easier and to avoid trial and error, I used a reference image, which is shown in figure 18, and obtained the center and radius by getting the pixel coordinates of certain points in the figure(such as the centers of each circle, an edge of a circle for the radius, etc) using paint same as what was done in the previous activity

Figure 18. The IPL logo taken from the cxteam.liknayan.com to be used as reference for the exploration part of the activity

So, for the reconstruction, I created 10 circles of different centers. For example, for a circle c = sqrt((X - centerx).^2 + (Y - centery).^2)will create a matrix of concentric circles centered at the center of the circle c, where centerx and centery are the x and y coordinates of the center of the circle c
 The base matrix will be matrix A of zeros similar to that in figure 2. The masks for each circles was done by making the elements of A when the value of the c is less than radius of the circle c be equal to a certain value unique for that circle. Example: A(find(c < radiusc)) = uniquevalue. the radiusc is the radius of the circle c and uniquevalue can be any number not equal to the original elements of the matrix A. For the lenses, which are half circles, an additional condition was added to locate only the other half of the circle. The initial output is shown in figure 19. 
Looking at the original logo however, the background is white. Since currently, the graycolormap(32) is being used, the higher numbers pixels will be colored white. So from the initial matrix A, instead of zeros, the value can be any number that is not the value of the circles. In my case, since there are only 9 unique colors (since the lenses have the same colors) 10 can be a safe value of the original matrix. It can be done by making the original matrix 1s instead of zeros using ones() and mutiplying element wise to 10. A = ones(nx, ny);A = A.*10. The resulting figure is shown in figure 20. The values of the circles were also adjusted for it to match with the original logo. 
Figure 19. The initial output for the recreation of the IPL logo with a black background and grayscaled circles or varying values.
Figure 20. Reconstruction of the IPL logo with a white background and grayscaled circles of varying values to mimic the actual look of the logo

To increase the details of the logo, the color_map used can be adjusted to mimic the colors of each circles in the logo. For this, I used the RGB values (closest to the original logo I could get) of each circle and used it as the color_map of the figure. Since the RGB has values from 0 to 255, the RGB values was divided by 255 for the value of the color_map be from 0 to 1. The cmap are 9 RGB values and the value of the circle are based on the index of its RGB color in the cmap. For example, the RGB value of the circle c is the 5th RGB value in the array cmap, so the circle c was given the value 5. The resulting image is as shown in figure 21. The complete code for the whole recreation of the logo is shown in figure 22. 
Figure 21. Colored IPL logo done using color_map by specifying the RGB values of the circles


Figure 22. The final code of reconstructing the IPL logo using different circle masks and specified RGB values of each circle for the colormap 

I find the whole activity quite fun especially since relatively complex shapes were produced from basic shapes. I would give myself 12/10 for this activity since I was able to produce all the required output and I successfully understood the basic scilab operations, the images are of good quality and properly labeled, and I was able to do the exploration part plus I used the previous lesson of obtaining pixel locations and I was also able to use manipulate the desired color of each mask. 

For the whole activity, I would like to thank Elijah Justin Medina for sharing his knowledge about color_map, on editing the axes_size, and on placing codes on the blog. Right now, I just placed figures of the screen shot of the codes, which made my blog post kinda messy. I will do the editing of html on my next blog posts for the codes. I would also like to acknowledge Adonis Villagomez and Clauzette Lofamia for telling me about the &, the equation for the ellipse, and other matters about scilab and the whole activity. 

Mortals are equal; their mask differs. -Voltaire

Linggo, Agosto 13, 2017

Tracing the Curves: Digital Scanning

Graphs and plots are helpful ways of presenting data. Many sites offering free data, like government websites, share their information to the public through graphs. They sometimes however do not give away the raw coordinates of the graphs. For data scientists, this would be a huge pain since a lot more investigations can be done with the raw data.

In the second activity, we were tasked to obtain the actual data points from a digitally scanned plot. Using ratio and proportion and by tabulating the pixel locations of the points in the graph, the plot can be perfectly replicated with its actual coordinates, with the limitations dependent on the quality of the scanned image. After this, the data points are now fresh and ready to be processed!

I used the image in figure 1 obtained from an old dissertation found in IPL[1]. The slight distortion of the plot can be observed from the curve in the horizontal axis since it was captured by a phone camera.

Figure 1.  A figure obtained from an old dissertation[1], captured using a phone camera.

The pixel locations from the plot can be tracked using Photoshop, GIMP, ImageJ, or Paint. I used paint due to availability. With paint, the origin (0,0) pixel is located at the upper left edge of the image. Using Excel, the tabulated pixel location of points in the plot is shown in figure 2.

Figure 2. Obtaining pixel locations with Paint. Hovering the mouse over the point of interest(indicated by the intersection of the red lines) shows the x and y pixel locations at the lower left corner of the window (marked by the blue box).

To convert the pixel locations to actual data point, a conversion factor can be done using pixels of known data points in the plot. In my case, I used the x and y tick marks as reference points since it was properly labeled. With this method, it was assumed that the x and y axis of the graph is horizontal and vertical. For the x-axis pixel locations, only the x-pixels were noted and for the y-axis pixel locations, the y-pixels. It can be seen that distortion in the image was not accounted since the y-locations of the x-axis and the x-locations of the y-axis was implied to be constant. Through ratio and proportion the difference between the points along the x and y coordinate will be converted to the difference in the actual coordinate, however, the difference of the points with respect to the origin will be neglected. It will be assumed that the origin of the image is same as that of the graph unfortunately the origin of the graph is not at the upper left edge but is at the lower left point after a thick margin.

Linear regression would be a good way to automatically convert the pixel locations to the actual data points since includes a shift due to the margins from the y-intercept and automatically accounts for the inverted origin location of the image from upper left to lower left from the negative sign in the slope of the resulting linear relation.

Figure 3 and 4 shows the plot for the calibration of the x and y axis of the plot. Looking back again at reference image in figure 1, the plot is in semilogx. It was noted that the x-axis is logarithmically scaled, so the trendline fitted should be in log scale. However, in excel the nearest fitting is exponential, which would be might cause a errors since the approximation may not match. So, I used the logx for the fitting and used the linear equation to convert the pixel values to the logx coordinates as shown in figure 3(b). The linear equation for the conversion of the x-pixels to the logx of the actual coordinates is logx =  0.0041xpixel - 2.0358 and the calibration equation for the conversion of the y pixel locations to the actual y coordinates is yactual = -0.1488ypixel + 423.63. Note the negative slope in the yactaul calibration equation, which accounts for the inverted origin point.

Figure 3. The actual x-axis of the plot vs the pixel location of the x-axis in the image. (a) With the real x value of the x-axis, an exponential trend line can be plotted. (b) With the logarithm of the x value of the actual x axis, a linear trend line can be used for the calibration equation.


Figure 4. The actual y-axis of the plot vs the y-pixel location of the y-axis in the image. A linear trendline can be used for the calibration equation of the y-axis

With the recalibrated points, the plot can already be reconstructed in lin-lin scale. However, the actual coordinates is in logx. As an additional step, the x-coordinates of the points was obtained getting the 10 raised to the logx values. The complete reconstruction of the plot with the actual point coordinates is shown in figure 6, which is in semilogx.



Figure 5. The reconstructed plot in lin-lin scale. The actual y coordinates of the plot vs the logarithm of the actual x coordinates of the plot.

Figure 6. The reconstructed plot in semilogx using the actual x and y coordinates of the plot.

By overlaying the image, it can be observed that the produced plot overlaps the captured image. Notice that the x-axis of the image does not completely overlap with the produced graph. This is due to the distortion dependent on the angle of the camera and the flatness of the page at which the image was taken. From figure 3, the R^2 value of the graph also shows that the fitted equation does not perfectly fit since it is slightly less than 1. This affects the conversion since the fitting equation was used as a calibrating equation.

Figure 7. The reconstructed plot overlayed with the captured image.


For this activity, I would give myself 10/10 since I successfully overlayed the image with the graph but the picture I took was a bit distorted, which affected the results of the overlap. In reality, most of the images do have distortions. With this, to obtain the actual plot coordinates, the image should first be corrected.

It was kinda fun doing the activity especially in the part where I was trying to overlap the image and the plot. It was quite fulfilling to see that it matches. The tracing part however tested my patience since at first, I chose a plot that is super wigly. I would like to acknowledge Micherene Clauzette Lofamia and Adonis Villagomez for the help they offered throughout the activity especially in the convertion method done of using linear regression up to which dataset should be in the x and y axis of the plot. I would also like to thank Sir Mario for his opinion on the method of converting to logx and the aproval of using the images. Towards the end of the lesson, when kuya Mario was checking if ever we did something during class, I explained the method and the linear regression I did to convert the pixel locations to actual location. Sir made a face looking a bit unconvinced about the method. He was actually expecting to use the ratio and proportion method to for conversion. But then, thinking more about it, the linear regression method is more direct to the point since the ratio and proportion was already given by the slope of the actual vs pixel graph, plus it accounted several points which is equivalent to averaging several measured ratios. Also, the group of Anton Cruz (w/ Jarms, Jethro and Ardie) kind of made the room super happy and lively with their nice remarks, especially since everyone else left the building. Their presence helped me finish the activity faster and enthusiastically.

In life, as in art, the beautiful moves in curves. - Edward G. Bulwer Lytton

[1] Lumawag, Efren (1992) Real-time Spectral Characteristics of Laser Diodes Under Large-Signal Current Modulation at Low Frequencies, Dissertation, UP Diliman