Activity 4: Image Enhancement

June 26, 2008 at 11:27 am (Uncategorized) (, , )

This activity enhances images by stretching the histogram of pixel values and stretching it in such a way that the maximum is 255 and the minimum is 0. another requirement is that the image cumulative distribution function (cdf) of the pixel values becomes linear. i wrote here a simple imhist function that can be included in the directory of the source code, and calle using getf(“imhist.sce”).

here is the source code of the “imhist.sce” function i wrote:

function [val,num]=imhist(im)
val=[];
num=[];
counter=1;
for i=0:1:255
[x,y]=find(im==i); //finds where im==i
val(counter)=i;
num(counter)=length(x); //find how many pixels of im have value of i
counter=counter+1;
end
return val, num;

-oOo-

after obtaining the probability distribution function (pdf, which is a normalized histogram), we obtain the cumulative distribution function.

taken from: http://home.earthlink.net/~rogergoodman/XRay-2000-06-05-Modabber.jpg

The probability distribution function and the cumulative distribution function

image enhancement is obtained by obtaining the the values of each pixel and backprojecting. in a sense, we are simple mapping each pixel of the original image to a new array with the CDF as the mapping function.

here is the code, which implemented my own imhist function. the “imhist.sce” file is loaded within the same directory as the program below.

-oOo-

//Jeric Tugaff
//Image Enahancement

clear all;
getf(“imhist.sce”);
im=imread(“aram.jpg”); //opens a 24 bit image
im=im*255;
[val, num]=imhist(im);
[sx, sy]=size(im);
num=num/(sx*sy);
normval=val/max(val);
cumnum=cumsum(num)/max(cumsum(num));
h=scf(1);
plot(normval, num);
h=scf(2);
plot(normval, cumnum);

enhanced=[];
im=im/255;
for i=1:sx
for j=1:sy
enhanced(i,j)=cumnum(find(normval==im(i,j)));
end
end
imwrite(enhanced, “enhanced.jpg”);
enhanced=round(enhanced*255);

//hist ulit
[val, num]=imhist(enhanced);
num=num/(sx*sy);
normval=val/max(val);
cumnum=cumsum(num)/max(cumsum(num));
h=scf(3);
plot(normval, num);
h=scf(4);
plot(normval, cumnum);

-oOo-

Using any non-linear CDF

Using any non-liner CDF, we produced better images than backprojecting along a linear CDF. The flowchart of the pixel backprojecting is summarized by this image from Dr. Soriano’s lecture.

Enhanced Image using parabolic CDF

Actual and desired parabolic CDF

Again, there is good (not perfect) correspondence with the desired (smooth) and actual (jagged) CDF. Here is my code, which is quite slow but is more general especially when in cases where the inverse function of the CDF is difficult to solve analytically:

//Jeric Tugaff
//Image enhancement using parabolic CDF

clear all;
getf(“imhist.sce”);
im=imread(“aram.jpg”); //opens a 24 bit image
im=im*255;
[val, num]=imhist(im);
[sx, sy]=size(im);
num=num/(sx*sy);
normval=val/max(val);
cumnum=cumsum(num)/max(cumsum(num));
h=scf(0);
plot(val, num)

enhanced=[];
desired_cdfx=[0:1:255];
desired_cdfy=desired_cdfx.*desired_cdfx;
desired_cdfy=desired_cdfy/max(desired_cdfy);

for i=1:sx
for j=1:sy
step2(i, j)=cumnum(im(i,j));
end
end

enhanced=interp1(desired_cdfy, desired_cdfx, step2);
h=scf(1);
imshow(enhanced, [0 255]);
enhanced=round(enhanced);
//hist ulit
[val, num]=imhist(enhanced);
num=num/(sx*sy);
normval=val/max(val);
cumnum=cumsum(num)/max(cumsum(num));
h=scf(3);
plot(val, cumnum);

-oOo-

collaborators: julie, lei, cole

rating: 10, because i’ve been able to implement my own imhist function and the resulting cumulative disctribution is has good correspondence with the desired CDF. 🙂

Permalink Leave a Comment

Activity 3: Image Types and Basic Image Enhancement

June 24, 2008 at 10:30 am (Uncategorized) (, , , , )

Image Types

True Color Image

//www.flickr.com/photos/8700785@N08/643095209/

from: http://www.flickr.com/photos/8700785@N08/643095209/

FileName: truecolor.jpg
FileSize: 189687
Format: JPEG
Width: 500
Height: 493
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: inch
XResolution: 72.000000
YResolution: 72.000000

Indexed Images.

indexed images parrot

from: http://en.wikipedia.org/wiki/Image:Adaptative_8bits_palette_sample_image.png

FileName: indexed.png
FileSize: 25576
Format: PNG
Width: 150
Height: 200
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

Grayscale Image.

This is obtained by getting images from the web. However, this image is still 24 bits (which is not a property of grayscale images). To convert it to 8 bit (which is true grayscale), use the code:

im=imread(‘grayscale.jpg’);
im=im(:,:,1);
imwrite(im(:,:), ‘gs.jpg’);

//blog.paranoidferret.com/files/Tutorials/CSharp/Grayscale/bw_flower.jpg

from: http://blog.paranoidferret.com/files/Tutorials/CSharp/Grayscale/bw_flower.jpg

FileName: gs.jpg
FileSize: 9217
Format: JPEG
Width: 250
Height: 250
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: inch
XResolution: 72.000000
YResolution: 72.000000

Histogram and Thresholding

The histogram of the grayscale image was obtained using the following code:

//Jeric Tugaff
//Histogram

im=imread(‘grayscale.jpg’); //opens a 24 bit image
im=im(:,:,1);
imwrite(im(:,:), ‘gs.jpg’); //converts to 8 bit grayscale image
im=imread(‘gs.jpg’);
val=[];
num=[];
counter=1;
for i=0:1:255
[x,y]=find(im==i); //finds where im==i
val(counter)=i;
num(counter)=length(x); //find how many pixels of im have value of i
counter=counter+1;
end
plot(val, num); //plot. 🙂

We obtained this plot of the histogram for the image of a grayscale flower:

Grayscale image histogram

The two peaks in the histogram plot is a hint that the image is of high quality, and is good for tresholding. To do the thresholding, we use the code:

im=imread(‘gs.jpg’);
thresh=140;
im=im2bw(im, thresh/255);
imshow(im);

This results to this binary image:

Binary Image

Application To Getting Area of Images

tresholds

The images show the effect of tresholding to the leaf image. The name of the image corresponds to the treshold. We can see that the most appriate is using threshold value of 200/255. The method done in using the area follows from the previous exercise. For this activity, the area is  20108.5 while the theoretical area is  20505 with error of 1.9%.

//Jeric Tugaff
//Getting image areas through green’s theorem and grayscale image tresholding

im=imread(‘leaf_cropped.JPG’);
im=im2gray(im); //convert to grayscale
im=im2bw(im, 200/255);
im=1*(im==0); //inverts the image
[x,y]=follow(im);
x1=[];
x1(1:length(x)-1)=x(2:length(x));
x1(length(x))=x(1);
y1=[];
y1(1:length(y)-1)=y(2:length(y));
y1(length(y))=y(1);
area=0.5*abs(sum(x1.*y-y1.*x))  //green’s theorem
ta=0;
[sx, sy]=size(im);    //finds the area through recatangles
for i=1:sy
f=find(im(:, i)==1);
ta=ta+max(f)-min(f);
end
ta
err=(area-ta)/ta*100
//[r,s]=imhist(‘leaf_cropped.JPG’);

Permalink Leave a Comment

Activity 2: Measuring Areas Using Green’s theorm

June 19, 2008 at 10:53 am (Uncategorized) (, )

We use Green’s theorem in finding the area of a given regular polygon. Green’s theorems states that we can get the area of a region R bounded by a closed counter C using the formula

In Scilab, the closed counter can be traced using the ‘follow’ command from the SIP toolbox. The syntax of the command is:

[x,y]=follow(g);

which results to the lists x and y containing the x and y coordinates of the contour (or simply the edge) respectively. It should be noted, however, that follow uses a black (0) background and white (1) object. The coordinates x and y gives the coordinates of the white object nearest to the edge.

The theoretical value of the area is measured by counting the number of white pixels (valued 1). Since binary images are converted to ones and zeros by scilab, the theoretical value of the area is simply sum(g).

//Jeric Tugaff
//Activity 2
//compute for areas using green’s theorem

g=imread(‘C:\Documents and Settings\AP186user04\Desktop\AP186\Activity 2\square.bmp’);
[x,y]=follow(g);
ta=sum(g) // theoretical area
x1(1)=x(length(x));
x1(2:length(x))=x(1:length(x)-1);
y1(1)=y(length(y));
y1(2:length(y))=y(1:length(y)-1);
a=0.5*sum( x1.*y – y1.*x)

square

results yield an ares of 24964 pixel when using green’s theorem, while the theoretical value is 25281 with error of 1.2539061% for the square. For circle, theoretical is 30937 and actual is 30656. Finally, for triangle, theoretical is 20000 and actual is 19701.

Colloborators: Julie Mae Dado (for help in some scilab commands, and conversion of .jpg images to binary format), Benjamin Palmares (for the help in configuring the scilab), Mark Leo Bejemino (for the invalueble discussion), and to Lei Uy (for helping out which bmp format is suitable, which, by the way, is the 24-bit bmp format)

Rating: 10. Because the computed area is well within the accepted value of the error. 😀

Permalink 2 Comments

Activity 1: Digital Scanning

June 12, 2008 at 2:06 am (Uncategorized) ()

The goal of the activity is to reconstruct a hand-written graph by extracting data from pixel values of the data points. For this activity, Will be extracting data from this graph:

The image is opened using Microsoft Paint and the pixel values are obtained by pointing the cursor over a pixel and it’s location is shown in lower-right portion of the status bar.

We first obtained some values to be used in the calibration of our pixel values, like location of the graph origin (bias x and bias y), and the scale of x and y pixel values to relate the pixel location with physical variables.

Bias along x: 56 pixels
Bias along y: 433 pixels
Scale of x: 80 pixel / 5 units (days)
Scale of y: 34 pixels / 10 units (% percent killed)
Origin: (56, 33)
Image size: (723, 505)

To obtain the reconstructed x values from the pixel values, we use the equation:
x = (raw_x – bias_x) / scale_x = (raw_x-56)*5/80
To obtain the reconstructed y values from the pixel values, we use the equation:
y = (bias_y – raw_y) / scale_y = (433 – raw_y)*10/34

Pixel values of data points were obtained, and the physical values were calculated.

We can see that there is good reconstruction of the data with almost all points in the original graph and the reconstructed graph coinciding with each other.

Exercise 1 Superimposed Plot

Rating: 10
Because there is almost perfect correspondence in the values of the reconstructed and original graph.

Acknowledgment: Benjamin Palmares

Graph from the journal Plant Physiology 1926

Permalink 1 Comment