Act 3. Image Types and Formats

Images are data with varied importance depending on where it will be used. Each type of image and file formats have its pros and cons, for one, some formats are lossy in terms of details but are more handy in terms of compression or the other way around. Knowing these information about images helps us determine which type and format is appropriate for which application.

There are four basic types of images:

Binary

square

Binary image

Size:        100 rows X 100 columns
Binary Image
FileName: D:\files\kaye\186\square.jpg
FileSize: 592
Format: JPEG
Width: 100
Height: 100
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: inch
XResolution: 72.000000
YResolution: 72.000000

Binary images are those that are composed of only two colors. Each pixel are either balck or white only, no in betweens.  The information were provided by Scilab 4.1.2 which could be access through the command imfinfo(‘file’, ‘verbose’) after the image has been loaded to Scilab via the imread(‘filename’) syntax.

Indexed

Size:   248 rows X 248 columns
Indexed Image

indexed

Indexed image

FileName: D:\files\kaye\186\indexed.gif
FileSize: 27065
Format: GIF
Width: 248
Height: 248
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

Indexed images work by having a representation for each major color in an image thus making it good for compression since the image is divided into two data set that take up less memory space – a matrix filled with the color representation, usually from values 0 to 255, and the color palette to be associated. However, if the palette got mixed up the image may turn out to be very different from the original. The binary image is a special case of indexed image only here the values are restricted to 1 or 0.

Grayscale

grayscale

Grayscale Image

Size:   493 rows X 740 columns
Indexed Image
FileName: D:\files\kaye\186\grayscale.jpg
FileSize: 86990
Format: JPEG
Width: 740
Height: 493
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: inch
XResolution: 300.000000
YResolution: 300.000000

Grayscale images unlike binary ones are composed of blacks and whites and their gradient. If indexed, it is composed of values between 0 and 1.

true color

True Color Image

True Color Images

Truecolor Image

FileName: D:\files\kaye\186\true color.jpg
FileSize: 12350
Format: JPEG
Width: 300
Height: 469
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

Aside from the basic types, we also have more advance types of images such as the following:

HDR

High Dynamic Range images

hyperspec

Hyperspectral Image

3d

3D Image

wave

Temporal Image

File formats of images are also important in having the appropriate image for a certain application. Knowing the differences between each format can help in determining which is best at which instance. Some of the common formats are as follows:

JPEG (Joint Photographic Extension Group)

File extension : .jpg

RGB – 24 bits

Grayscale – 8 bits

Jpeg is considered as a lossy compression for an image file however, it has an option on how large the file will be making it flexible for the most part. It may be used for files that are not too dependent on every inch of its details. It is good for photographic images but not for graphics and the likes.

GIF (Graphics interchange Format)

Indexed color: 1- 8 bits

File extension: .gif

Gif is a lossless file compression format sharp-edged arts like logos and small animations that require only a few colors. Gif is limited to 256 colors making it not suitable for high quality photographs.

TIFF (Tagged Image File Format)

RGB – 24 or 48 bits,
Grayscale – 8 or 16 bits,
Indexed color – 1 to 8 bits

File extension: . tif

TIFF uses a lossless compression technique known as LZW which makes it appropriate for instances which makes use of every bit of detail in an image such as xray scans. TIFF can be considered as the best file format among all however it has also its own downside. When used in photo images, Tiff files tend to eat up so much space since it does not compress much. Aside from this, TIFF is also not compatible with internet browsers.

Conversion of file formats

The final part of this activity deals with the conversion of the image type or format.

Using Scilab 4.1.2 and SIP toolbox, a true color image is loaded into it and is manipulated. Using im2gray and im2bw syntax the image is converted into a grayscale image or binary image respectively. Using the histplot command, the component color of the image was determined. The data provided by the histogram also helped determine the threshold level that must be set such that the the binary converted image can have a good contrast.

graph

Graph from activity 1that is converted into grayscale

histplot

Histogram plot of the graph from activity 1

From the histogram plot we could see that the value of the most  number of pixel lie between 0.85 and 1. Since we know that majority of the pixels lie in the background we can say that those with values between 0.85 and 1are part of the background. With this we set the threshold level to 0.80 which effectively set all pixel pixel values below or equal this level to 0 while those higher than this to 1 thus turning the image to a binary image.

threshold

Converted to binary and adjusted to have a good contrast between lines and background

We can see that this edited image of the same graph is much cleaner and the lines are much clearer.

I will give myself a 10 for this activity since I was able to give all the necessary outputs and I understood the concepts presented.

References:

[1] http://www.scantips.com/basics09.html

[2] http://www.scantips.com/basics9t.html

[3] http://en.wikipedia.org/wiki/JPEG

[4] http://en.wikipedia.org/wiki/Graphics_Interchange_Format

for the pictures:

grayscale: http://www.pixelperfectdigital.com/free_stock_photos/data/509/medium/grayscale_flower.jpg
indexed: http://star.pst.qub.ac.uk/idl/images/imgcolor16.gif
true color: http://www.spacew.com/gallery/image004202-thumb.jpg
HDR : http://farm2.static.flickr.com/1047/1156912539_ecd588a316.jpg
hyperspectral : http://rst.gsfc.nasa.gov/Sect13/HyMap_Zack_3D_cube.jpg
3d: http://drawn.ca/wordpress/wp-content/images/nightmare3d.jpg

temporal image: http://www.freefever.com/animatedgifs/animated/wave2.gif

Scilab Code:

stacksize(100000000);

// basic types
binary= imread(‘D:\files\kaye\186\square.jpg’);
imfinfo(‘D:\files\kaye\186\square.jpg’, ‘verbose’);

indexed = imread(‘D:\files\kaye\186\indexed.gif’);
imfinfo(‘D:\files\kaye\186\indexed.gif’, ‘verbose’);

grayscale = imread(‘D:\files\kaye\186\grayscale.jpg’);
imfinfo(‘D:\files\kaye\186\grayscale.jpg’, ‘verbose’);

truecolor = imread(‘D:\files\kaye\186\true color.jpg’);
imfinfo(‘D:\files\kaye\186\true color.jpg’, ‘verbose’);

// advance types
hdr = imread(‘D:\files\kaye\186\HDR.jpg’);
imfinfo(‘D:\files\kaye\186\HDR.jpg’, ‘verbose’);

hyperspectral = imread(‘D:\files\kaye\186\hyperspectral.jpg’);
imfinfo(‘D:\files\kaye\186\hyperspectral.jpg’, ‘verbose’);

3d = imread(‘D:\files\kaye\186\3d.jpg’);
imfinfo(‘D:\files\kaye\186\3d.jpg’, ‘verbose’);

//converting

//black and white
bw_highmax =im2bw(truecolor, 0.7);
bw_lowmax = im2bw(truecolor, 0.3);
scf(1), imshow(bw_highmax);
imwrite(bw_highmax, ‘D:\files\kaye\186\bw_highmax.jpg’);
scf(2), imshow(bw_lowmax);
imwrite(bw_lowmax, ‘D:\files\kaye\186\bw_lowmax.jpg’);

//grayscale
gray = im2gray(truecolor);
imshow(gray);
histplot(256, gray);

//from act 1
act1 = gray_imread(‘D:\files\kaye\186\act1 graph.jpg’);
scf(3), imshow(act1);
scf(4), histplot(256, act1);
bw= im2bw(act1,0.8);
imwrite(bw, ‘D:\files\kaye\186\bwtreshold.jpg’);
scf(5), imshow(bw);

Size:        100 rows X 100 columns
Indexed Image
FileName: D:\files\kaye\186\square.jpg
FileSize: 592
Format: JPEG
Width: 100
Height: 100
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: inch
XResolution: 72.000000
YResolution: 72.000000

Act 2. Scilab Basics

Scilab is an open source programming language that is highly useful in performing tasks such as image and signal processing, statistical analysis, and the likes.[1]  It is significantly similar to MATLAB not just on its features but as well in syntaxes making it a good substitute for the said programming language. Among its advantages over other languages, particularly those that are open source as well, is in the way it treats variables as array and matrices which is more convenient than the usual ‘for loop’ way of other languages.[2] Activity 2 focuses on basic Scilab matrix manipulations. It aims to familiarizes the students with basic operations in Scilab and as well as the use of the Scilab Image Processing (SIP) toolbox.

For this activity we were asked to produce the following images using Scilab matrices and SIP toolbox:

a. centered square aperture

b.grating along the x-direction
c. annulus
d. sinusoid along the x-direction (corrugated roof)
e. circular aperture with graded transparency (gaussian transparency).

We were first given a sample code which produces a centered circle from where I based my codes for the other images. The sample code goes:

centered circle

Fig 1. Sample code producing a centered circle

nx = 100; ny = 100; //defines the number of elements along x and y
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates
r= sqrt(X.^2 + Y.^2); //note element-per-element squaring of X and Y
A = zeros (nx,ny);
A (find(r<0.7) ) = 1;
imshow (A, []);

* For the next sets of codes the following must be inserted before the the first line of each of the sets:

nx = 100; ny = 100; //defines the number of elements along x and y
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

A. Centered Square

centered square

Fig 2. Centered Square

// square
A = zeros (nx,ny);
A (find ((abs(X) <= 0.5) & (abs(Y) <= 0.5)))=1;
imshow (A, []);
imwrite (A, ‘square.jpg’);

For this figure  a 100 x 100 matrix with each element set to zero was first created then the elements with index of (-0.5 to 0.5 ,  -0.5 to 0.5) were set to 1. Using SIP toolbox, as indicated by the syntax imshow (), the resulting matrix was converted into an image where the zeros were represented by black pixels while the ones with white pixels. Finally the last line, imwrite(), takes the matrix converts it into image and save it under the file name indicated by the second argument.

B. Grating along the x direction

grating

Fig 3. Grating along x direction

// grating
A = zeros (nx,ny);
A (find(X<=.7))=1.0;
A (find(X<=.5))=0;
A (find(X<=.3))=1.0;
A (find(X<=.1))=0;
A (find(X<=-.1))=1.0;
A (find(X<=-.3))=0;
A (find(X<=-.5))=1;
A (find(X<=-.7))=0.0;
imshow (A, []);
imwrite (A, ‘grating.jpg’);

This figure follows the same principles as the second figure only that the index of the elements to be set as 1 were quite different. Due to lack of familiarity to the Scilab syntax, I used brute force in specifying the elements to be set to 1. There is no doubt that there is a shorter form of this code.

C. Annulus

annulus

Fig 4. Annulus

// annulus
r= sqrt(X.^2 + Y.^2);
A = zeros (nx,ny);
A (find(r<0.7) ) = 1;
A (find(r<0.4) ) = 0;
imshow (A, []);
imwrite (A, ‘annulus.jpg’);

Producing figure 4 follows closely to the first figure. The only difference is that since we only want a ‘ring’ instead of a whole circle we set elements of the matrix falling inside a circle a desired but smaller radius back to 0.

D. Sinusoid Along the X-Direction (Corrugated Roof)

// sinusoid

sinusoid

Fig 5. Sinusoid along x direction


A= sin(X.*4*%pi)
imshow (A, []);
imwrite (A, ‘sinusoid.jpg’);


For this figure we don’t define a zero matrix anymore since we are not interested anymore in 1’s and 0’s only. We are now in need of fractions between 0 and 1 wherein the value of the fraction determines the darkness of its pixel representation still having 0 as the darkest while one the lightest. Here we set the values  of the elements of the matrix according to the sine function with frequency of 4 pi.

E.  Circular Aperture with Graded Transparency (Gaussian Transparency)

gaussian

Fig 6. Circle with gradient transparency

// circle with gradient transparency (gaussian)
r= sqrt(X.^2 + Y.^2);
A= (1/2*%pi)*exp(-(r^2/2^2));
imshow (A, []);
imwrite (A, ‘gaussian.jpg’);


In this figure we follow the same procedure as in the previous figure however I used the equation for circular Gaussian given by:

For simplicity, all constants and parameters were set to 1.

*Note : Please click the image of the equation for a clearer version

Overall, I think this activity is successful in familiarizing the students with Scilab and SIP toolbox. Since I was able to perform all task at hand and comprehend he concepts behind this activity I would give myself a score of 10.

I would like to acknowledge the help of Rob Entac for sharing insights about the activity as well as Jonathan Abat who unknowingly enlightened me about some parts that I’m having problem with.

References:

[1] http://en.wikipedia.org/wiki/Scilab

[2] A2- Scilab Basics 2010 Manual by M. Soriano

[3] http://mathworld.wolfram.com/GaussianFunction.html

Act 1. Digital Scanning

First, I’d like to welcome you to my very first blog ever. “Hello World!”

Our first activity for Applied Physics 186 involves the reconstruction of a digitally scanned hand-drawn graph. The graph I used for this activity was taken from the Physical Review 2nd series vol. 19 No.4 which dates back to the year 1922.

And this is how it looks:

Whole Page

The Plot

According to the journal this is a plot of  the efficiency of electrostatic transmitter.

After obtaining a digital copy of the graph that is to be reconstructed the next steps are fairly easy though a bit tricky at some points, needing only basic math and computer skills.

So you open the image with any image editor (paint, gimp, etc.) so long as that editor displays pixel coordinates, I used MS paint for this since it is readily available on the computer that I was using at that time. Note the pixel coordinates of points of known value in your graph both in the x and y axes. At least 3 points per axis will do the job but I suggest to take at least 10 points to lessen the error. Also, note the pixel coordinates of pertinent points in the graph.

Caution: For the x-axis you only have to take care of the excess space before the origin (or the left most part of the graph) which could be easily done by subtracting the x coordinate of the origin from the x coordinates of all your data in the x-axis. However, this is the tricky part, for the y-axis, notice that the (0,0) coordinate of the pixels is at the upper left corner, the y-coordinate increases downwards while for most graphs it increases upwards. If you carelessly repeat what you did for the x-axis without adjusting for this you’ll end up with a flipped image of your graph. Once you’ve noted this the solution is as simple as subtracting the y coordinate of the highest point from the rest of the points.

After gathering all necessary information a simple ratio and proportion must be done to know how many pixels are there for every unit of measurement in the graph. My original plan in obtaining this ratio is by using linear regression to get the exact relation however after doing linear regression in OpenOffice spreadsheet I found out that it does not display equations unlike in Excel. Since I’m limited to OpenOffice I settled with the traditional way of doing things. Finally, multiply the ratios obtained with the adjusted coordinates of the pertinent points (data points) to get the actual value of these points then plot them.

And so this is my final product,

My digitized plot

Superimposition

From the superimposed image of the two plots (the black is the original while the purple line is the reconstructed) we can see that the two lines match well indicating that the ratios obtained where fairly accurate. The grids, however, does not match at some points because the original image is tilted (or distorted).

The problem with hand-drawn plots lies in defining the exact value of the data points especially if these points does not fall exactly in the grid lines. Throughout the years technology has helped science to become more accurate, precise, and easily accessible. Through this activity I learned how to use simple math and computer skills to be able to analyze old-age scientific findings better using new-age technologies.

I give myself a 9 for this activity since I think I understood its concept and I was able to produce good results although I have missed some points.

While writing this, I analyzed my data with Microsoft Excel and found that the exact relation of the pixels and the physical values are as follow:

for the y coordinates: y = 17.21x + 66.34

for the x coordinates: y = 3.375x + 37.86

where is x is the physical value and y is the number of pixels.