Sunday, June 29, 2008

Image Enhancement

June 30, 2008
11:51 am


The goal for this activity is to enhance the contrast of an image by manipulating its cumulative distribution function (CDF). The standard way of doing this is by applying a transformation to the image such that its (CDF) is a straight line. We do this by replacing the gray level values of each pixel by the CDF value of its gray level. This is called histogram equalization. This is to distribute the intensity over the whole range of gray level values, i.e., 0-255. We may also create our own image enhancement protocols. I did it by replacing the gray level values of each pixelin the original image by the squared CDF value of its gray level.



Images and Their Corresponding Histograms



a) original image




















b) histogram-equalized image






















c) custom enhanced by replacing the original pixel values by the CDF value squared of the original























The CDF for (c) takes the shape of a quadratic function that opens to the right. It is fitted to the function y = sqrt(x/255).


Codes:


This code. It aims to perform histogram equalization and get the probability distribution function and cumulative distribution function of both the original and the enhanced. Its histogram-getting part is taken from Mr. Tugaff's code.


A=imread('sunflower3.jpg');


A = A-1; //because the image that would be read would have pixel values from 1 to 256.


val=[];


num=[];


counter=1;
for i=0:1:255


[x,y]=find(A==i);


val(counter)=i;


num_(counter)=length(x);


counter=counter+1;


end


num = num_./((size(A,1))*size(A,2));


plot(val, num_);


cdf = [];c


cdf(1) = num(1);


for i = 2:256


cdf(i) = num(i) + cdf(i-1);


end


plot(val,cdf)


dimensions = (size(A,1)*size(A,2));


B = zeros(size(A,1),size(A,2));


for i = 0:1:255


[x,y] = find(A==i);


for k=1:length(x)


B(x(k),y(k)) = (cdf(i+1) - min(cdf))/(dimensions -min(cdf));


end


end


B = B/max(max(B));


B = round(B*255);


counter = 1;


val2=[];


num = [];


num2_ = [];


for i=0:1:255


[x,y]=find(B==i);


val2(counter)=i;


num2_(counter)=length(x);


counter=counter+1;


end


num2 = num2_./((size(B,1))*size(B,2));


plot(val2, num2_);


cdf2 = [];


cdf2(1) = num2(1);


for i = 2:256


cdf2(i) = num2(i) + cdf2(i-1);


end


plot(val2,cdf2)


To get the images for section (c), custom enhancement, we simply this part of the above code


B = zeros(size(A,1),size(A,2));
for i = 0:1:255
[x,y] = find(A==i);
for k=1:length(x)
B(x(k),y(k)) = (cdf(i+1) - min(cdf))/(dimensions -min(cdf));
end


by


B = zeros(size(A,1),size(A,2));
for i = 0:1:255
[x,y] = find(A==i);
for k=1:length(x)
B(x(k),y(k)) = (cdf(i+1) - min(cdf))*(cdf(i+1) - min(cdf))/(dimensions -min(cdf));
end


SELF-EVALUATION RATING: 10/10 because I got a straight line for the CDF of the histogram-equalized image.

Credits:


Orignal sunflower photo was taken from: Christopher Maxwell, PhotographerWedding Photography: Black and White Info, http://www.christophermaxwell.com/black-white.htm


The part of the above code that get the histogram of an image is lifted from a code by Mr. Jeric Tugaff.

Thursday, June 26, 2008

Digital Scanning part 2



June 26, 2008


Thursday




The goal for today is to get the area of a real object via Green's Theorem. My chosen object is a leaf.


The leaf image I used for processing was scaled to half of the above image. As can be seen a length of 1 centimeter in the vertical direction ate the same number of pixels as that in the horizontal direction. So no further rescaling was required. The image was saved with a bit depth of 8 using Paint software. Its histogram was then used to find out which gray level should be the threshold. It was found to be at gray level = 100 or 0.4. When the image had been converted to binary, its colors were afterwards inverted so that the leaf is bright and the background is dark. Then Green's Theorem was applied. (See preceding post for my code for executing the theorem.)

Scilab code for converting the leaf image to binary:

A = imread('leaves2.jpg');

histplot([0:255],A); //to determine the appropriate threshold level

bw = im2bw(A, 0.4); //threshold level= 0.4

imwrite(bw,'bw2.jpg');

Via the pixel counting method, getting sum(sum(bw)), the resulting area was 8491 pixels.

Meanwhile, Green's Theorem yielded an area of 8245 pixels (%difference from the pixel counting result = 2.9%). This is approximately 21 cm squared.

............

I rate myself: 10/10! because I think 21 cm squared is reasonable. The leaf's length was about 10 cm and its width, measured across the middle, was nearly 2 cm.

Credits: Benj and Ed for helping with syntax.

Monday, June 23, 2008

Different Image Types according to Bit Depth

June 24, 2008
An image of each type is opened in Scilab to get their pixel size and bit count - one for binary, one for grayscale, one for true color, and one for indexed image. Since most of the internet files are saved as JPEG. They were converted to other image types using Paint.

A. Binary
Original photo from www.unm.edu
File size : 2922 bytes
Format: BMP
Size: 148 x 143
Depth: 8
Number of Colors: 2
ResolutionUnit: centimeter
XResolution: 37.800000
YResolution: 37.800000

B. Grayscale
Original photo from purplezoe.blogspot.com
FileName: C:\Documents and Settings\AP186user20\Desktop\ap186_june24\grayscale.bmp
FileSize: 18254
Format: BMP
Width: 150
Height: 113
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 37.800000
YResolution: 37.800000

C. True Color
photo from twisted physics.typepad.com
FileName: C:\Documents and Settings\AP186user20\Desktop\ap186_june24\truecolor.jpg
FileSize: 2202
Format: JPEG
Width: 135
Height: 90
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

D. Indexed
photo from chamorrobible.org
FileName: C:\Documents and Settings\AP186user20\Desktop\ap186_june24\indexed.png
FileSize: 9610
Format: PNG
Width: 150
Height: 107
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

Credits:
Beth Prieto for helping me with scilab

Wednesday, June 18, 2008

Area Measurement I






June 19, 2008 11:21 am
Grade: 10/10

The goal for today is to determine the area of a regular shape saved as a binary image, which is created in Paint.

Area is computed for using Green's Theorem, an approximation for contour integration.
My code, which has been written for scilab is as follows.
A = imread('C:\Documents and Settings\AP186User20\Desktop\circ4.bmp');
[x,y] = follow(A);
length_x = size(x,1);
length_x_minus1 = length_x-1;
x_(2:length_x) = x(1:length_x_minus1);
length_y = size(y,1);
length_y_minus1 = length_y-1;
y_(2:length_y) = y(1:length_y_minus1);
Area =(sum( x.*y_ - y.*x_))/2
theo = sum(sum(A))
error = 100*((theo - abs(Area))/theo)

The pixel count of the binary image is taken to be the theoretical area.

There were three shapes investigated: triangle, rectangle and circle. For the circle, the effect of radius on the error is investigated. Error has been found to be decreasing with increasing radius.

Triangle
Area calculated: 3162
Theoretical area = 3326
%error = 4.93%

Rectangle
Area calculated: 2517.5
Theoretical area: 2652
%error: 5.07%

Circle (radius = 15)
Area calculated: 600.5
Theoretical area: 649
%error: 7.47%

Circle (radius = 25)
Area calculated: 1707
Theoretical area: 1788
%error : 4.53%

Circle (radius = 35)
Area calculated :3509.5
Theoretical area: 3625
%error: 3.19%

Credits:
Thanks to Beth Prieto for helping me with scilab; Rica for loading ImageMagick; Aiyin for a pdf copy of the emailed attachment; Paul & JC for letting me use their internet; and to Ma'am Jing for pinpointing that scilab can have memory problems.

Wednesday, June 11, 2008

Digital Scanning

TEODORO, DONNA APRIL P.

June 12, 2008

AP 186 Rating: 9.5/10

Remarks: I got the shape of the graph, but the values are not that close.

Along the y-axis, the graph interval is 600. From paint, we tabulate the following pixel values (FIg1).

The average y2 – y1 is 56.44 (rounded off to 57).

The maximum y value(the y pixel value in the origin) is 538.

The equation relating the pixel value to its proper Excel value is

y' = (538 – y)/57.

Along the x-axis, the graph interval is 1 (month). From paint, we tabulate the following pixel values (FIg2).

The average x2– x1 is 38.42.

The equation relating the pixel value x to its proper Excel value x' is

x' = x/38.42.





Tuesday, June 10, 2008

Introduction

This is Donna April Teodoro's individual report site for her Instrumentation Physics II course, which is handled by Dr. Maricor Soriano. The aforementioned blog owner is a bonafide fifth year student from the University of the Philippines Diliman, which is now celebrating the centenary of its foundation (June 19, 1908).