Thursday, June 27, 2013

Err, yeah... Activity 5, Area Estimation

Just to get things moving along quickly, I'll just go straight into describing what I did based on mam's instructions.

I made bitmap images from Paint shown below. 
Don't judge mah Paint skilz..

Then I started to code. A few days later, I have this:

//area finding code of Joshua Beringuela, June 2013
//discrete solution found using green's theorem
//analytic solution by counting white pixels

//read image and find edge
oi = imread('C:\Users\Shua\Desktop\untitled2.bmp');
im = bool2s(oi==255);
ie = edge(oi,'canny');

//find center
[ix,iy] = find(ie);
cx = mean(ix);
cy = mean(iy);
//disp(cx,cy);
im(cx,cy) = 0;
ie(cx,cy) = 1;
ie = bool2s(ie);
a = [im ie];
imshow(a);

//sort x,y by increasing theta
nx = ix - cx;
ny = iy - cy;
theta = atan(ny, nx);
txy = [theta; nx; ny];
stxy = gsort(txy,'lc','i');
//disp(stxy);
sx = stxy(2,:);
sy = stxy(3,:);
zsx = cat(2,0,sx);
zsy = cat(2,0,sy);
sxz = cat(2,sx,0);
syz = cat(2,sy,0);

//2 solutions and error
diar = 0.5*sum(zsx.*syz-sxz.*zsy);
anar = length(find(oi==255));
e = 100*(anar-diar)/anar
disp(e, '% error', diar, 'discrete area =', anar, 'analytic area = ');


Basically, what it does is it reads a black and white bitmap image and returns the area using the Green's method and a pixel counting method and the %error between the two. I used it for the square from the first image and got this:

analytic area = 2091.
computed area = 2077.
%error = 0.64.

I also tried it for the triangle and got
analytic area = 96.
computed area = 88.9.
%error = 7.4.


part of the code shows the original image and the outline as a result of the edge function and the center of both indicated by the dot.


A few notes. I used canny for edge detection. I noticed that error increased for a smaller resolution image.

Before any further results, let's get back to what the code does.
After reading the image, we find the edge using the edge function in Scilab.
It would be nice to explore the effects of using the different methods of edge detection when I have time.
The centroid of the image is found by getting the mean value of the points. It is a crude method but it is the average position of the points of the outline. This could be modified to find the average position of the area by simply getting the mean of the white points in the image. The center was subtracted from the outline and the angle was computed. Then, the angle was sorted from least to greatest along with the corresponding x and y values of the outline. Finally, the equation coming from Green's theorem was used and the rest of the lines are for the output.

I would like to brag about how my code did not use any loops at all. I really put an effort to make a nice code. Credits to Floyd for helping me understand what to do and helping me debug at times. I'd also like to point out that this code really took me a long time to finish. I think it would be reasonable to give it 3 class meetings. As it turns out, the code really isn't that difficult. It's just that I am not used to Scilab yet. In fact, this is probably my second real Scilab code, the first one being the Scilab basics. Nonetheless, I'm proud of my code even if it's nothing special, really. Aww. emo. haha. Anyway, I learned a lot of new stuff from Scilab. I got to use imread, imshow, imwrite, edge, gsort among other stuff. I guess, that's what matters.


Now, I start to wonder what mam is really looking for in the blog. To the results.
For the second part of the activity, the code was applied to find the area of the acad oval.
jog jog din dito pag may time.

It was first taken from Google maps and then processed to a black and white bitmap image using photoshop. Finally, the code returns this for the acad oval.

analytic area = 157403.
computed area = 157182.
%error = 0.14.

It would be nice to have the real value of the area of the acad oval and compare it to the result of this code. Actually, a pixel to actual size conversion can be done by following activity 2. However, the acad oval is an irregular shape and using the scale would still be an approximation. I'm pretty sure that somewhere out there is the land area of the academic oval. Anyway, using the code on the acad oval just proves that the code can also be used for irregular shapes with good agreement between the two methods.


 The 'acad not really an oval'

In the end, I would have to admit that I had some fun trying to make the code in an efficient way. There are a few more things that could be done here but I just feel like I've spent so much time on this na. I give myself a 9.

Tuesday, June 18, 2013

Save as ...? Activity 4, Image types and formats

I've always wondered about the file types or format of pictures. Usually it's jpeg but I know there are others. I've actually found it quite annoying to have so many file types available and it makes you ask why. So as mam said just a few minutes ago in the lecture, of course, there are reasons and even stories behind the file types.

To start off, I'd like to check just how much I currently know of file types. These are the things I know even before I start researching. I'd like to make a comparison on just how much I know.

I'm sure we are all familiar with jpeg and I've heard that it is based on Fourier transform. It is a file type with a high compression ratio. This means that jpeg files are a lot smaller than the original. However, I've already noticed that the quality is just acceptable. I mean it looks fine, especially from afar. You can find problems when you zoom in. The first jpeg image that comes to mind is the scan of the first activity, the hand drawn graph. I'll put it here to show you. As I've said, it looks fine from afar but zoom in on the details and the small text and you find it is already starting blur. You can also find artifacts sometimes such as the random gray pixels near the top of the graph.


The next file type I am familiar with is PNG. I know that it stands for portable network graphics? Close enough? Haha. I actually use this often because I've found it to have a better quality than JPEG. Why is that? I guess it's time to start researching soon. Well, to be fair, I've found that it has a bigger file size than JPEG. It is logical that it follows that bigger file size, better quality. What would be amazing is to find an image compression that reduces the file size but retains the quality. I'm starting to get excited.

It just suddenly occurred to me, what does JPEG mean anyway.. I'm guessing J something Picture experts group. Still, not ready to research just yet.

There is another file type I do know about and it is called RAW. I swear, I knew about this even before mam mentioned it a while ago. I've seen it in my camera and keep hearing it around camera enthusiasts. What I do know about it is the same as what mam said. It has a very large file size but it is raw. It is unedited. I've never tried it on my camera though.. Getting ideas to try it later..

The next thing I'd like to try and show as well is a comparison of the different file types possible. I'll use the same image and save it as different file types in Photoshop and try to upload them here so that we can see the differences between the file types.

As I looked for possible images to save as different file types, I realized that it wouldn't be fair to start from any other file type other than RAW. Is there sense to this? I think so. I'm thinking, if I start with a JPEG, some of the information has already been lost due to the compression. I think I'm going to take a RAW picture right now. I'll publish this first and get back to this as soon as I finish saving the RAW picture to the different file formats.

So I've just taken 4 pictures and I just had to blog immediately to tell you the file size. It's 64mb for the four pictures resulting to an average of 16mb per picture if I can still compute mentally. Compare that to the 1.3mb average I get with my default settings (4mp, jpeg).

Now, the next question.. Can I upload a RAW image here?
...
and the answer is yes.. but it took a while to upload it.

and here's a JPEG of the same subject for comparison.
It turns out that this is not a very good comparison since I took this a few minutes apart with my camera set to auto. If I find myself bored later on, it would be nice to have the same subject with the exact same settings taken in succession for a good comparison. It also occurred to me that the RAW image would possibly take a long time to load. As a result, I also realized why it is important to have file compression. To justify, most websites don't really need high quality pictures. It is much logical to use compressed pictures to improve upload and downloading times. It is also presented in a way that normal people won't really notice the difference in quality.

As it turns out, because of the inconsistent settings, it seems that the JPEG version looks a lot better. Is it supposed to be that way? This is another question I should answer once I start researching.

Also, I almost forgot to tell you about the beautiful lady in these pictures. Or maybe I shouldn't.. :P
Anyway, I hope that she doesn't mind I use her pictures here. It's not like anybody else would see them except her and probably a few of my batch mates :)


Chester's laptop alarm has just sounded 2 minutes ago which marks almost 2 hours of straight typing. I think it's time for a break. In the mean time, metaphorically of course, please enjoy this picture of a 20 Watt incandescent bulb I took for the Light Sources experiment but probably won't make it to our paper.

any bright ideas?

In addition to this commercial, please take note that all the pictures here so far, except for the first JPEG, the scan of the hand drawn graph are all shots taken by yours truly using a Sony Nex 5N digital camera.


...
As I press the insert image button here in blogger, I find another file type that I can upload, a GIF. This is also a popular file format for netizens as it is typically used for short animations. I would imagine file size to be proportional to the length of the animation. Here's one of my favorite GIFs. (of course, not taken by my camera. I think I found this in tumblr).

Disney is just awesome.

Next, we have here a true color image for manipulation. I found a picture of our batch from last year.

good times :)

I uploaded two versions of the same image just to get an idea of the effect of resolution. For the purpose of the blog and under normal human eye powers, the image quality is exactly the same. You may want to open the image separately to get a better look at the difference. On the other hand, the computer sees these two images very differently. Below is a snapshot of the image in the original resolution according to GIMP.
where did the 1.4 GB come from?

I'm not sure what file size and size in memory means but according to Windows, it is actually just 2.12 MB. I just used Windows photo gallery to resize the image to a lower resolution of 640x425. The resulting image is 243KB. This leads me to wonder, does this size make sense? I'm pretty confident that the resolution is approximately directly proportional to resolution. Ready for some Math?

Image
Resolution
Pixels
File size (KB)
File size compression
Resolution
compression
Original
2448x1624
3975552
2120
14.8
14.6
Resized
640x425
272000
243
This table proves just that.

I also compared the file sizes and quality of the original image with an indexed jpeg, grayscale jpeg and bitmap version of the same image. Below is the tabulated summary.


Original
Indexed
Grayscale
bitmap
File size (MB)
2.12
3.21
1.66
3.79
quality
Best
Poor color gradient
Not colored
sligtly worse than grayscale
not as expected

Probably the biggest surprise would be that the poorer quality versions would have a bigger file size. I really have to research on this as well but I don't think there's enough time. I think that the original jpeg has 8-bit color depth and probably the indexed one would have less so I expected it to have a smaller file size. The bitmap also has slightly worse quality than the grayscale image but a lot bigger in file size. What is expected though is that the grayscale image is smaller than the original since it doesn't have to retain the color information, just the brightness. In fact, I would expect it to be only 1/3 the size of the original because in Scilab, a grayscale image would have only one matrix while a colored one would have three.

We then try out some basic image processing functions in Scilab. Here are a few of the most important in my own words.
imread
Of course, to start, we need to open the file in Scilab. This function saves the image data into a matrix depending on the image. It requires an input ('of the file path'). An image is actually a collection of pixels of different colors. The number of these pixels is dependent on the resolution of the image. Then, from AP187, we learned that any color can be represented as a composition of different values of red, green and blue. So for a true color image such as the typical image from a camera, imread converts the image to an (m x n x 3) matrix where m is the height of the image and n is the width of the image in pixels while the 3 represents each of the primary colors of light, R, G and B.

imwrite
Jumping to the end, imwrite simply returns the matrix or matrices into images. It requires, of course the matrix /ces to be made into an image and the ('filename'). Recently, I started having problems looking for the images from imwrite. It was only recently that I learned that you can write the file path instead of just the filename. Not sure but I think it was Badet who taught me that. Thanks :) It is important to note that if just a single (m x n) matrix is used, the resulting image will be grayscale. The values in the matrix would represent the brightness of the pixel, 0 being black and 255 being white. The same is true for imread, just in the opposite direction. By this I mean that if you imread a grayscale image, it would result into an (m x n x 1) or just an (m x n) matrix.

mat2gray
After manipulations in Scilab, the matrices of your image may not be in the range of 0-255 which is not good for imwrite. One solution is to use mat2gray which scales the matrix to values of 0-1 which imwrite or imshow can understand properly.

imshow
Imshow is similar to imwrite. The difference is that it simply opens another window to show the matrix in image form and not save it to a file. It follows that it only requires the matrix as input and not the filename anymore.

im2bw
Im2bw converts a matrix into true or false values based on a threshold required in the input. In image form, boolean matrices result in black and white images. The true elements become white pixels while false becomes white, I think. I also think that I learn best through applying these things. The image below shows the resulting images of using the thresholds 0.2, 0.4, 0.6 and 0.8.

And that's why God said 'let there be color'.. or not..

So what im2bw does is it checks each element in the matrix and determines if it is greater than the threshold. If yes, it will will result in a true value and in image form, a white pixel. Otherwise, it will be black. In this image, most of the pixels have a value greater than 0.2 and appear white shown in image in the upper left corner of the figure above. On the other end, the image in the lower right corner is almost completely black since most of the pixels have a value less than 0.8.

histplot
The histplot is an important function for the activity six which is histogram manipulation. It's not that I know the future but because I'm just editing this post on the day of the deadline. Simply from the name itself, histplot plots the histogram of the image. It counts the number of elements or pixels (y-axis) that have a certain value which is the x-axis. Shown below is the histogram of histplot of the image of my batch mates. It requires the number of bins and the matrix or array. The default setting is normalized so the y-axis is a decimal or a fraction of 1 which is 100% or all the pixels.

the value of each pixel or element in the image is well distributed

imhist
As a comparison, below is a dark image and its histogram. Another difference is that imhist is used which is very similar to histplot. The dark image means that there are more pixels with a small value which is reflected in the histogram. Also, in this case, the y-axis isn't normalized so those are the actual numbers of pixels with that value.


Finally, imfinfo shows some information about the image such as width, height, color depth and color type which could either be true color or grayscale.


In the end, I feel like I've placed a lot of effort in this as well. However, I feel that my batch mates have done much more. In this case, I failed to research on the file types and instead just put in my own understanding of only a few. I just hope the effort I put into common image processing functions would be enough to give me an 8/10.

Thursday, June 13, 2013

Scilab Art. Activity 3, Scilab basics

When it comes to programs that make graphics, people usually think of Photoshop or Paint. Not us, we use Scilab!
Okay, fine. It's not really the easiest thing to use if you want to make simple shapes or edit photos. However, it can do image processing in a way that's different to commercial applications. It is a program that lets users code exactly what they want. More on this later but for now, I just want to prove that I was able to do most of the figures required for the activity. I'll be writing more about them later... hopefully.

The images shown are the annulus, the Gaussian window, a linear gradient window, a circular window, my understanding of the corrugated roof, sine along x, and the square window in that order.

I'm not sure if I'm required to upload or post the code. You can pm me if you need it.









I'm no Leonardo but these images are the building blocks of our journey of image processing with Scilab, I think. And these images are no Mona Lisa, but I allow myself to tell myself that these aren't so bad for my first try at Scilab. Nothing like self-encouragement before going to bed.

As for my feelings, coding is certainly more fun just listening to a lecture. I'm pretty sure I can improve and complete the required images if I find some free time. I give myself an 8 because I recognize that I didn't really put a lot of effort to complete all the requirements of this activity. I think I can make bawi naman in the future activities #medyoconyo

Tuesday, June 11, 2013

Legalized Plagiarism? Activity 2, Digital Scanning

The title somehow describes the activity we did today. Plagiarism, as I understand it is using an idea and making it appear as your own. It is best known as copying a line, sentence or paragraph and pasting it on your own work and not citing the source. The activity we did today was to reconstruct a hand drawn graph. Technically, it is not plagiarism since we did not just copy and paste a graph and make it our own. It certainly wasn't that easy either. I will also be citing the source of the graph I tried to replicate later. I should. Hopefully I remember. So now, it turns out that the title is misleading. Well, at least I hope it got your attention.

First, I looked for a hand drawn graph. I found two good ones in a book I'm supposed to be reading for my research. It is entitled Building Scientific Apparatus by Moore, Davis, Coplan published by the Addison-Wesley Publishing Company in 1983. The first one is the one used here, found in page 54 in the chapter working with glass. The first image is the raw scan.

Then I cropped it using Photoshop.

This is another graph I found but it looked too complicated so I didn't use it.


Next, I noted the pixel coordinates of the tick marks from the x-axis and plotted it in Excel. From here, I noticed that the x-axis is in the logarithmic scale. Plotting the pixel coordinate as a function of the variable on the x-axis which is wavelength, the result is an exponential. The equation of the line is is 87.658exp(0.0111x) with a pretty good R squared value

Similarly, I did the same thing for the y-axis and found that it had a linear relationship with an equation of y = -0.4808x + 104.71 and an R squared value of 1.

Next, I took the pixel coordinates of some points on the graph. I applied the transformation from pixel location to actual values namely to percent transmission as a function of the wavelength. I plotted it and placed the original scan at the back and we get the image below. Personally, I think I was able to reconstruct the graph pretty well. I give myself a 9.



Procrastinating

Greetings!

If you are currently reading this, you must be either mam Jing or a batch mate or you must really lost or bored. Please bear with me as I am not a real blogger. If you are not mam Jing nor my batch mate, for your information, this blog was created as a requirement for the course Applied Physics 186 which is basically about image processing. Although the main purpose of this blog is to satisfy the requirement, I also hope to learn a lot more in writing and presenting data and to some degree, enjoy it as well. Finally, the purpose of this post is not just to inform you what this blog is about but also to procrastinate. I hope you also bear with me and I hope it is ok with mam that I inject some humor and make this blog a bit less formal so that I can entertain you somehow as well. Technically, I am procrastinating but actually I'm just trying to practice my writing skills. Palusot pa. Haha. But really, I'm just trying to put together my thoughts on the activity we did a while ago. And wala naman pasok bukas! I'll have plenty of time to fix my grammar and stuff when I do it tomorrow. So it turns out, I am really procrastinating. Haha