Monday, June 29, 2009

Activity 4 Enhancement by Histogram Manipulation

The histogram of a grayscale image is equal to the graylevel probability
distribution function (PDF) if normalized by the total number of pixels. As such,
the grayscale PDF of an image can be modified in the same way one can modify
the PDF of random numbers.
Histogram manipulation is one way of improving the quality of an image,
enhancing certain image features, or mimicking the response of different imaging
systems, such as the human eye.
Given the cumulative distribution function (CDF) of a desired PDF, the grayscale
PDF of an image can be modified by backprojecting the grayscale values using
the CDF. Then
newly encountered functions in scilab:
tabul - frequency of values of a matrix or vector
tabul allows
you to compute the frequency of of occurence of a vector or matrix
[m]=tabul(X [,order])
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Activity 4:
Two categories of image enhancement techniques include spatial and frequency domain techniques. Spatial techniques involve direct manipulation of image pixels while frequency domain techniques works by manipulation of fourier of the and wavelet transform of a signal.
Here, the spatial technique will used by way of histogram enhancement. The histogram of an image illustrate the distribution of gray levels in the image in the range 0 to 255 where 0 is black and 255 is white and normalized when in the range [0.0, 1.0].
The method of histogram enhancement works by equalization.This is one of the ways in which it can be done in Scilab:
You get the histogram of the image by using the tabul(filename, ‘i’) command that gives the frequency of occurrence of the matrix or vectors, or in this case the no. of pixels having that graylevel and then plot it.
The histogram:
x axis for the graylevel
y axis for the frequency value

Next, normalize the greylevel and you distribute the frequency over the total no. of pixels and thus spread out the most frequent intensity values.


Results and discussions:



Using linear method of histrogram equalization, we see in Fig.1 that the resulting new image has a well distributed histogram as compared to the original image which has uneven probability distribution. The new image has a higher contrast than the original.


In Figure 2, using nonlinear histogram equalization using interp1(,,'linear') was used. However the resulting image has lower contrast than the original.

For this image the linear method was able to enhance the contrast in the image while the non linear method was not and only resulted to reduction in grayvalue of each graylevel.


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code:
linear

im=imread('C:\Users\USA\Desktop\scilab and 186\grayscale parrot.png');

m=tabul(im,'i');hx=m(:,1);hy=m(:,2);

scf(0);

[px,py]=size(im);

np=px*py;

nx=hx/max(hx);

ny=hy;

ccy=cumsum(ny)/max(cumsum(ny));

subplot(321);

imshow(im,[]);

subplot(322);

plot(nx,ny);

title('PDF1- Histogram 1');xlabel('grayscale level');ylabel('frequency');

subplot(323);

plot(nx,ccy);

title('CDF1');

//2. backprojection of the image through the CDF original

newim= [];

for i = 1:px

for j = 1:py

newim(i,j) = ccy(im(i,j));

end

end

subplot(324);

imshow(newim,[]);

//scf(1);

//imshow(newim,[]);

//imwrite(good1, 'newim.jpg');

/////for this newimage get the PDF and CDF again

im=imread('C:\Users\USA\Desktop\scilab and 186\newim.jpg');

im=tabul(newim,'i');hx=m(:,1);hy=m(:,2);

scf(0);

[px,py]=size(im);

np=px*py;

nx=hx/max(hx);

ny=hy;

ccy=cumsum(ny)/max(cumsum(ny));

subplot(325);

plot(nx,ny);

title('PDF2- Histogram of Image 2');xlabel('grayscale level');ylabel('frequency');

subplot(326);

plot(nx,ccy)

title('CDF of Image 2');

xs2sgif(0,’figure1.gif’);


code: non linear:


im=imread('C:\Users\USA\Desktop\scilab and 186\grayscale parrot.png');

m=tabul(im,'i');hx=m(:,1);hy=m(:,2);

scf(0);

[px,py]=size(im);

np=px*py;

nx=hx/max(hx);

ny=hy;

ccy=cumsum(ny)/max(cumsum(ny));

subplot(321);

imshow(im,[]);

subplot(322);

plot(nx,ny);

title('PDF1- Histogram 1');xlabel('grayscale level');ylabel('frequency');

subplot(323);

plot(nx,ccy);

title('CDF1');

// for the equalization by backprojection of the image through a desired cdf

xx=[0:1:255];

yy=xx^2;

yy=yy/max(yy);

xxp=[];

yyp=[]

for i=1:px;

for j=1:py;

xxp(i, j)=ccy(im(i,j));

end

end

yyp=interp1(yy,xx, xxp,'linear');

//yyp=interp1(yy,xx, xxp,' spline');

//yyp=interp1(yy,xx, xxp,'nearest);

newim=yyp;

subplot(324);

imshow(newim, []);

newim=round(newim);

subplot(324);

imshow(newim,[]);


im=imread('C:\Users\USA\Desktop\scilab and 186\newim.jpg');

im=tabul(newim,'i');hx=m(:,1);hy=m(:,2);

scf(0);

[px,py]=size(im);

np=px*py;

nx=hx/max(hx);

ny=hy;

ccy=cumsum(ny)/max(cumsum(ny));

subplot(325);

plot(nx,ny);

title('PDF2- Histogram of Image 2');xlabel('grayscale level');ylabel('frequency');

subplot(326);

plot(nx,ccy)

title('CDF of Image 2');

xs2sgif(0,’figure1.gif’);


Activity 2:Area estimations of images with defined images

Activity 2 Area estimations of images with defined images

For this activity the area of certain region in an image with a defined geometry is estimated using Green’s theorem. In summation (discrete) form, if there are Nb pixels in the boundary or contour of R then the area of R is

The parametrized x and y coordinates for this computation were obtained using th SCIlab’s SIP toolbox function follow() that has the command
[x,y] = follow(Img).The xy vectors stores the parametized contour and Img is a binary array of 0 and 1 where 0 is equivalent to background and 1 is the object. But before we can do this, the image data is first loaded in for processing.

Imread is used to read the image and obtain an array containing the image data and im2bw is used to convert these data values to binary by thresholding, say 0.5. Then the follow command is used to trace the x y pixel coordinates nearest the edge which we plug in to Green’s function to get the area.




Another way of obtaining the area is by taking the sum of the white region via sum(Img). Basically we sum the 1’s in the binary array. This value is equivalent to what we call the analytical sum and is used to check the experimental area value obtained using Green’s function. The closer these values coincide the lesser the error.

The code made is shown below:

I=imread('C:\Users\USA\Desktop\circle140d.bmp');
b=im2bw(I,0.5);
[x,y]=follow(b);
xsize=size(x);
ysize=size(y);
xlen=length(x);
ylen=length(y);
xi(1)=x(xlen);
yi(1)=y(ylen);
A_g=abs(0.5* sum( xi.*y - x.*yi));
A_sum=sum(b) ;
D= abs( (A_sum-A_g)/A_sum),"Error"

**************************************

I would like to acknowledge Rommel for helping me with scilab and code for loop code.. when it didn’t worked… thanks otherwise I wouldn’t have finished the work.
Grade: 9/10 because of the small error in the results.

Monday, June 22, 2009

A3 – Image types and basic image enhancement

Images
"An image is represented by a two-dimensional table in which each cell is a pixel. To represent an image by means of computer, it is thus enough to create a pixel table in which each cell contains a value. The value stored in a cell is coded on a certain number of bits which determine the colour or the intensity of the pixel, This is called the coding depth (or is sometimes also called the colour depth. "[http://en.kioskea.net/contents/video/affich.php3]


Using imfinfo(filename, 'verbose') in Scilab , one can determine the type of an image and also determine other file information.

1.Grayscale:
Pixel color depth: 8-bit color

Grayscale images are monochromatic and carries only intensity information composed exclusively of shades of gray that vary from black (lowest intensity) to white (highest intensity) equivalent to 2^4 intensities( 256) or 8 bits per sampled pixel.

Image source:http://en.wikipedia.org/wiki/Grayscale


-->imfinfo('C:\Users\USA\Desktop\scilab and 186\grayscale parrot.png','verbose')
FileName: C:\Users\USA\Desktop\scilab and 186\grayscale parrot.png
FileSize: 29342
Format: PNG
Width: 150
Height: 200
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000
******
On part 6. Bonus – If you can make a program in Scilab to compute and plot the
histogram of a grayscale image, you get 2 points bonus.


im=imread('C:\Users\USA\Desktop\scilab and 186\grayscale parrot.png'); // read image
im=im*255;
scf(0);
m=tabul(im,'i');


hx=m(:,1);
hy=m(:,2);
h=scf(0);

[px,py]=size(im);
npixel=px*py;

nx= hx/max(hx);
ny= hy/npixel;

ccy=cumsum(ny).*(1/max(cumsum(ny)));

subplot(321);
plot(nx,ny);
title('norm hist');
title('nxy');

subplot(322);
plot(nx,ccy);
title('nxccy');

xs2gif(0,'C:\Users\USA\Desktop\histograms2.gif');
****************
2. True color
Pixel color depth: 24-bit color (True Color)

These consist of at least 256 shades of red, green, and blue resulting to around 16,777,216 color variations.

-->imfinfo('C:\Users\USA\Desktop\scilab and 186\color pizza.png','verbose')
FileName: C:\Users\USA\Desktop\scilab and 186\color pizza.png
FileSize: 246909
Format: PNG
Width: 302
Height: 302
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
XResolution: 23.230000
YResolution: 23.230000

3. Binary

These are two level images, wherein each pixel store only store two possible values, that is, either 0 or 1, black or white but any two color combination can be used.

Image source: http://en.wikipedia.org/wiki/File:Neighborhood_watch_bw.png



-->imfinfo('C:\Users\USA\Desktop\scilab and 186\binary.png','verbose')
FileName: C:\Users\USA\Desktop\scilab and 186\binary.png
FileSize: 2660
Format: PNG
Width: 200
Height: 140
Depth: 8
StorageType: indexed
NumberOfColors: 2
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

4. Indexed
Indexed images are made up of colors indexed by its position in the pallete or an array of color elements. The color information is not directly carried by the image pixel data.

Image source: http://en.wikipedia.org/wiki/File:IndexedColorSample_(Mosaic).png


-->imfinfo('C:\Users\USA\Desktop\indexed.png','verbose')
FileName: C:\Users\USA\Desktop\indexed.png
FileSize: 9789
Format: PNG
Width: 160
Height: 142
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000


********************************


Getting the area of scanned of images or objects
In this part, a rebisco cracker was photograph and the resulting image was scanned and the area of this biscuit was computed using the resulting plot in Scilab from the traced edges through the command follow.( Whahah I later realized that I should have scanned the rebisco directly in order to lessen the error :p .. oh well )

The procedure and results are as follows:
1. Take picture and scan it.
2. Approximate the physical area via an area of a circle with radius r=diameter/2.
3. Find the computed area from the ratio of the no. of pixels to the actual measurements in the photo or take the plot of the object via the follow() command.
4. The first one was difficult cause the x y coordinates in the photo matching the pixels in paint required alllot of data points and I soon give up this approach.
5. I then used the plot in the follow() command to trace the edges. and approximate the object as a circle and get the radius from the diameter.
6. Also I compared the results of the computed area from analytical sum and the Green's function but wasn't able to relate these values to the physical area. (whahaa)
7. The difference between the analytical area and are computed from green’s function is the error in this estimation. Taking that its area for a circle, the physical area is around 14.13 cm (d=4.5 cm) while the computed are is 10.6814 (w/ d=3.4 cm from the plot).
8. The area from the two physical and numerical methods do not coincide since area of the object was only approximated to that of a circle even thought the object is curved in the edges. The computed area of the image is more accurate since it has taken into account the curved edges but is affected by the distortion of the scanned image. The error is 24.42 image if we take the physical area to be more accurate while 32.30% for the computed area.
9. The sources of errors includes the distortion effect of the camera, lighting or background and the shades or shadow in the edges . These errors may have been avoided if the object was scanned directly (o_o) or photographed in a uniform background lighting to avoid unwanted shading in the edges.
Here are the pix:

The scanned picture:


The image after im2bw and thresholding, and converting the black to white and vv. to get the sum of the white area:

The plot:

.*.*.*.*.*.*.*.

Grade:

Wednesday, June 17, 2009

Activity 1 Re-plotting with Paint and Excel

In this activity we scanned a handwritten plot and used Paint and Excel to replot this.
To do this:
1.A number of x and y coordinates in pixel positions of the scanned plot are obtained one by one starting from the left side and listed down in Excel. It must be noted that the origin in Paint is at the top left part.

2. Next, the origin of the scanned plot is noted down and the number of pixel per unit measure in the x and y axis of the original plot is computed to be able to convert the x-y pixel positions to x-y units--use this ratio as the conversion factor.

3. Third, the x-y coordinates for the replotting are computed using the relation of the listed x-y pixels coordinates to the origin of the scanned plot and multiplied by a conversion factor found in 2. This is how:
For the x axis, positive direction (to the right):

x plot=1* (x pixel-x origin) pixel *( measure/#pixel in x)

For the y axis, negative or downward direction:

y plot= -1* (y pixel-y origin) pixel *( measure/#pixel in y)

4. These xplot and y plot coordinates are then plotted in Excel and the picture of the scanned plot is used as the background in Plot area in Excel to superimpose the two to see if the resulting plot coincides with the original using the pixel and units relation.

The figure below illustrates the resulting plot and the table includes the datapoints for the pixels and the corresponding converted values using the formulas in 3.


This plot coincides with the original plot along with the grid and origin.

*************
Acknowledgement: I would like to give thanks to Kaye for teaching us and Gary in the CS lib on how to go about the pixel and ratio of the units while waiting for our library clearance. :) Also, thanks to Janno for rotation of the image in Nero.

Grade: 10/10. Because I was able to understand and enjoy the activity and superimpose the plots right away. :)

Book: Algebra and Calculus 1912, Palmer



Monday, June 15, 2009

A1 DIGITAL SCANNING

Introduction (ahem...)

In this activity , we use ratio and proportion to fing the numerical values of a digitally scanned hand drawn plot. Using paint and XCel we reconstruct the graph ...kuno.

first day ng class at 186

testing.....da- blog :)