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.

No comments:

Post a Comment