Activity 3 – Scilab Basics or: It’s Not That Easy

so we were tasked to generate synthetic images using Scilab, a high-level programming language that can do image-processing (among other things). i however, used MATLAB (and will be using MATLAB for the rest of the semester and my life), since i’m more used to it, and i have been using it for more than half a year now. still, just because you’ve been using it for a relatively long time, doesn’t mean you’re pretty proficient with it.

anyway, this is a checklist of what images we should be able to generate:

a. centered square aperture

b. sinusoid along the x-direction

c. grating along the x-direction

d. annulus

e. circular aperture with gaussian transparency

f. ellipse

g. cross

and here’s the code:

%%%Hilado Activity 3%%%
saveloc = 'D:\Random Stuff\20150820\AP 186 Act 3\';
saveext = '.png';

%%%set size of image%%%
nx = 150; ny = 150;

%%%create square aperture%%%
x = linspace(-1,1,nx); y = linspace(-1,1,ny);
A = ones(nx, ny);
A(find(abs(X) > 0.4)) = 0;
A(find(abs(Y) > 0.4)) = 0;
figure(1); imshow(A);
imwrite(A, strcat(saveloc, 'square', saveext));

%%%alternative method to creating square aperture%%%
% I = ones(50,50);
% I = padarray(I,[50,50]);
% figure(1); imshow(I);

%%%create sinusoid grid%%%
x = linspace(-10,10,nx); y = sin(x);
[X,Y] = ndgrid(x,y);
figure(2); imshow(Y);
imwrite(Y, strcat(saveloc, 'sinusoid', saveext));

%%%create square grating grid%%%
y = square(x);
[X,Y] = ndgrid(x,y);
figure(3); imshow(Y);
imwrite(Y, strcat(saveloc, 'grating', saveext));

%%%create annulus%%%
x = linspace(-1,1,nx); y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
r = sqrt(X.^2 + Y.^2);
A = zeros(nx, ny);
A(find(r<0.7)) = 1; A(find(r<0.4)) = 0;
figure(4); imshow(A);
imwrite(A, strcat(saveloc, 'annulus', saveext));

%%%create circular aperture with gaussian transparency%%%
sigma = 2.0;
gauss = 1/(2*pi*sigma^2)*exp((-r^2)/(2*sigma^2));
gauss = gauss./sum(gauss(:));
figure(5); imshow(mat2gray(gauss));
imwrite(gauss, strcat(saveloc, 'gaussian', saveext));

%%%create ellipse%%%
a = 1; b = 1.5;
ellipse = (X/a).^2 + (Y/b).^2;
ellipse./sum(ellipse(:));
A = zeros(nx,ny);
A(find(ellipse<0.4)) = 1;
figure(6); imshow(A);
imwrite(A, strcat(saveloc, 'ellipsis', saveext));

%%%create cross%%%
A = zeros(nx,ny);
A(find(abs(X)<0.3)) = 1; 
A(find(abs(Y)<0.3)) = 1;
figure(7); imshow(A);
imwrite(A, strcat(saveloc, 'cross', saveext));

and here are the corresponding synthetic images:

Figure 1: Square aperture
Figure 1: Square aperture
Figure 2: Corrugated roof
Figure 2: Corrugated roof
Figure 3: Grating
Figure 3: Grating
Figure 4: Annulus
Figure 4: Annulus
Figure 5. Circular aperture with gaussian transparency
Figure 5. Circular aperture with gaussian transparency
Figure 6. Ellipse
Figure 6. Ellipse
Figure 7. Cross
Figure 7. Cross

I would give myself a 10/10, for doing all the required output.

Activity 2 – Digital Scanning or: The Search For The Elusive Hand-Drawn Plot

so basically, the objective is to digitally recreate a plot from the physical world. like, say, you have a hand-drawn plot, as shown below

Figure 1. Image taken from http://bruceravel.github.io/demeter/aug/plot/etc.html
Figure 1. Image taken from http://bruceravel.github.io/demeter/aug/plot/etc.html

you can digitally recreate the plot. all you need is to tabulate the image coordinates of the tick marks of the x- and y-axes, and derive a formula relating physical value to image coordinate value. once you’ve done that, get enough points from the plot itself, and convert their pixel coordinates to their physical coordinates. and voila, you’re done.

of course, this whole paragraph is pretty unintelligible to some, since i just condensed everything in one quick 30-second read. so i’ll be more detailed from now on.


this was deceptively easy.

actually, the activity itself was very doable. the hardest part is finding a hand-drawn plot. and also the realization that you cannot – never, ever – photocopy some journal without the explicit permission of the author. which is tricky, since they are either too busy to be bothered by some puny undergraduate, too old that they are probably drinking tea by the seashore, or that they’re dead.

so i looked at the next best thing: books. i went to the IPL (Instrumentation Physics Laboratory) at the 4th floor, and scrounged the old books lying about. fortunately, i came across this beauty of a plot, which is most likely hand drawn. i mean, what machine can reliably make this in the 80’s?

002
Figure 2. A plot taken from the paper “Evidence for a Structural Phase Change in the Fast-Ion Conductor Na3Sc2P3O12”

this was scanned with a resolution of 100 dpi.

so the software i used to align and rotate the image was GIMP. lemme show you what it looks like:

Figure 3. A GIMP 2 interface
Figure 3. A GIMP 2 interface

it’s pretty messy, but lemme show you a quick rundown. the left toolbox is where you’ll find the basic tools you’ll normally find in some other basic image processing program such as Paint, or Photoshop. there’s the crop tool, scale, zoom, pencil, blur, and other nifty stuff. on the right is the layers and brushes toolbox, where you organize your layers and manipulate your brushes. the plot at the center is the plot i scanned. and the small toolbox beside the layers-brushes toolbox is the pointer toolbox. it’s where you’ll know exactly what are the coordinates of some part of the image you’re pointing at. the important part here is that you know the pixel coordinates, encircled below:

Figure 4. Where the pixel coordinates are
Figure 4. Where the pixel coordinates are

note that the origin, or (0, 0) of the image in this case is located at the upper-leftmost corner.

next thing to do is to crop and rotate the image, such that the x- and y-axis of the plot is really horizontal and vertical.

Figure 5. Rotated and cropped
Figure 5. Rotated and cropped

using gimp, locate the tick marks on the axes, and note their pixel coordinates. just point your mouse onto your marks, and the pointer toolbox will show you the corresponding x and y pixel coordinates. and then you tabulate them on Excel.

Figure 6. My messy Excel workbook
Figure 6. My messy Excel workbook

i’m pretty sure most of us know how to make a graph and construct a trend line on excel, since this is basic stuff. so, yes, basically, you find an equation relating the pixel location to the plot coordinates. you can see the equations in the two graphs on the image, as examples.

then what’s left to do is collect points from the plot itself, and convert their pixel locations to physical coordinates, using the equations you have derived. and this should appear:

Figure 7. Reconstructed plot, overlaying the original plot
Figure 7. Reconstructed plot, overlaying the original plot

as one can clearly see, the reconstructed plot fits neatly into the original plot. and that’s a job well done.


oh, by the way, how did i make an overlay image on the background of my Excel graph? simply right-click on the plot area, click “Format Plot Area”, and then you’ll see this:

Figure 8. Format Plot Area
Figure 8. Format Plot Area

you should select “Picture or texture fill”, and browse for your desired image (or in this case, the original plot). and then adjust the image through fiddling with the “offsets” in “Stretch Options” until you fit the image perfectly.


i am inclined to give myself 11/10 because i did everything in the activity, and also i managed to overlay an image into the spreadsheet.

Source of plot: Delbecq, C. J., Marshall, S. A., & Susman, S. (1980). Evidence for a structural phase change in the fast-ion conductor Na 3 Sc 2 P 3 O 12. Solid State Ionics,1(1), 145-149.

the first post is always the first post

unfortunately, for those who were looking for thomas mueller and was hoping for a fan-made blog about his unorthodox yet strangely beautiful darts across and through a bamboozled back line and his array of seemingly ordinary goals, you will be disappointed. (though if you do know where to find them, please please please do tell me.) what this website is is a series of posts detailing the activities of an undergraduate physics student – specifically, those related to image and video processing. there are others just like this made by my classmates, and i have tremendous respect for them, especially those who get really high grades without even trying. the purpose of this blog is to show that we (me and my classmates) are indeed capable of doing such work, and to help other people find their way in case they get lost in all the technicals. the blog also aims to show that doing all these is also fun. hence, the entries posted here will be as vibrant and engaging as possible, and all technical stuff (codes, graphs, you name it) will be infused with human emotions and feelings, if at all possible. here’s a sample of what might be written here:

%%%initializing zero matrix with nervous breathing%%%

size = 100; %just because

A = zeros(size, size);

%%%end initialization and initialize raucous applause%%%

you get the idea.

anyway, if you are even remotely interested in programming and/or seeing a student fumble, then you’ve come to the right place. i hope you can take something from my posts. criticism is highly encouraged. i’m happy to be told when i’m doing something wrong, or if there is a better way of doing things. toodles!

Jeremy Hilado

p. s. you will see a self-rated score at the bottom of each post. this is not a way for me to display my narcissistic side, but is a requirement to show that we are capable of objective self-evaluation.