A4: Enhancement by Histogram Manipulation

In this activity, we were to produce the histogram equalized images of raw grayscale pictures using backprojection from raw image cumulative distribution functions (CDF) on an ideal CDF. We are also to create and apply a CDF that can represent the nonlinear response of the human eye to images.

All image processing was done on Scilab with the SIP toolbox. Images were first imported using imread, which imports the matrix of pixel values that make up the images. To get the CDF of the image, we first determine its PDF, which is just the histogram normalized with respect to the number of pixels in the image. The pixel value frequency count was determined using the tabul command. tabul only lists frequency counts for pixel values that do exist in the image matrix, it is possible that pixel values will be skipped. To fill up the skipped pixel values with frequency counts of zero and to set the pixel value range to 0 to 255:
allpxvals = [0:1:255];
allvalfreq = allpxvals*0;
for j = allpxvals,
allvalfreq = allvalfreq + (bool2s(allpxvals==j)*(valfreq(find(pxvals==j))));
end;


We then calculate the CDF per pixel value r as in the equation below (p1 is the PDF):

which is equivalent to the cumsum command in Scilab applied on the CDF of the image.

We then need an "ideal" CDF for the transformation. For the linear enhancement, we simply create an array from 0 to 255 to match the pixel values and normalize it by the total number of pixels (Figure 1).


Figure 1. Linear CDF for a uniform distribution used as ideal CDF for backprojection.

We then finally do backprojection by matching image CDF and ideal CDF values and replacing the respective image pixel value with the pixel value from the ideal CDF (Figure 2).


Figure 2. Backprojection example.

CDF value matching may be attained using the vectorfind function, which searches a certain value on a given array. But since the values calculated for the ideal CDF array are finite, not all image CDF values may be matched with the ideal CDF values. Hence, it is easier to simply invert the equation for the ideal CDF and input the image CDF values as in the code below. Note that this is for each pixel value r.
//backprojection
modimg = img*0; //create empty matrix, same size as img
for r = [1:1:length(pxvals)],
modimg = modimg + (bool2s(img==pxvals(r))*round(CDF(r)/m));;
end;

where img is the imported image pixel value matrix, pxvals contains the list of pixel values obtained from the frequency count done by tabul, CDF is the CDF of the image, and m is just the slope of our ideal CDF. Note that img==pxvals(r) outputs a boolean matrix that has True values for those img elements that are equal to pixel value r and then bool2s turns the boolean matrix into a matrix of 1s and 0s for True and False values, respectively, so that only matrix elements with pixel value r will be changed into the new pixel value. New pixel values are calculated from CDF(r)/m. We then view the resulting image using imshow.

Figures 3, 4, and 5 show the raw images and enhanced images and PDF and CDF plots.


Figure 3. Top: raw grayscale eye image (left) and enhanced eye image (right). Middle: PDF for raw (left) and enhanced (right) image. Bottom: CDF for raw (left) and enhanced (right) image. (image source: http://farm2.static.flickr.com/1342/577585546_c53f536278.jpg?v=0)



Figure 4. Top: raw grayscale hand image (left) and enhanced hand image (right). Middle: PDF for raw (left) and enhanced (right) image. Bottom: CDF for raw (left) and enhanced (right) image. (image source: http://farm4.static.flickr.com/3606/3346568944_37df6fd2b5.jpg?v=0)



Figure 5. Top: raw SEM image (left) and enhanced SEM image (right). Middle: PDF for raw (left) and enhanced (right) image. Bottom: CDF for raw (left) and enhanced (right) image. (image source: http://www.mos.org/sln/SEM/diatomb.gif)

All enhanced images show higher contrast than the raw files; dark pixels in the raw image appear darker in the enhanced image and light pixels in the raw image appear whiter in the enhanced version. We also note that the CDF for the new images (bottom right of Figures 3, 4, and 5) have been converted to the ideal CDF (Figure 1) but with some flat regions since pixel values are whole numbers and finite.

For the nonlinear ideal CDFs we try using y = x2 and y = log(x). Note that the human eye responds logarithmically. Figure 6 summarizes the resulting images for the linear, parabolic and logarithmic CDFs.




Figure 6. For each 4-image set clockwise: raw image, linear CDF, logarithmic CDF, and parabolic CDF.

The parabolic CDF saturated the image, as expected, since it just squares the intensity of each pixel while the logarithmic CDF produced contrast even higher than those produced by the linear CDF.



I would like to thank Mr. Mark Jayson Villangca for teaching me to use the tabul command and Mr. Miguel Sison for pointing out errors in the image histograms.

I give myself a grade of 9 because I was able to increase the contrast of the images using backprojection and CDFs of the modified images show that the algorithm used was correct.

A3: Image types and basic image enhancement TAKE 2

Since image conversion isn't allowed, here's another set of images.

Figure 1. Truecolor image of a flower taken in 2007 at the new NIP.
Figure 2. Indexed image of a root locus plot done with SciLab for Applied Physics 183.

Figure 3. Grayscale image. (http://www.mos.org/sln/SEM/diatomb.gif)

Figure 4. Binary image. (http://www.flickr.com/photos/adrianblack/2285934929/sizes/o)

For area estimation, I have scanned the following hand-drawn images with a ruler:




Figure 5. Scanned images for area estimation.

From imfinfo :


and for both images:


To determine effective threshold value for binarization, we plot histogram or the PDF as in Activity 4:
//histogram imgsize = size(Img); //get image dimensions
imgfreq = tabul(Img); //get histogram

pxvals = imgfreq(:,1); //pixel values

valfreq = imgfreq(:,2); //pixel value frequencies

allpxvals = [0:1:255];

allvalfreq = allpxvals*0;

for j = allpxvals,

allvalfreq = allvalfreq + (bool2s(allpxvals==j)*(valfreq(find(pxvals==j))));
end;

//PDF = histogram normalized with respect to total no. of pixels

numpx = imgsize(1)*imgsize(2);
PDF = allvalfreq/numpx;

fig = scf(1);

plot(allpxvals,PDF);


Figure 6 shows the PDFs for both images and we find an obvious distinction between the background and the region of interest or ROI:

Figure 6. Heart (left) and "JICA" text (right) image PDFs.

//after visual inspection and deciding on threshold
threshold = 0.5;

ROI = Img((1:imgsize(1)),(100:imgsize(2))); //select ROI

ROIBW = im2bw(ROI,threshold); //binarize based on threshold
imshow(ROIBW);


Figure 7 shows the binarized ROIs:


Figure 7. Binarized ROIs.

And as in Activity 2, we compute for the area using pixel counting and Green's area approximation:
[x,y] = follow(ROIBW); //get contour from image
len = length(x); //no. of boundary pixels

//close contour

x(len+1)=x(1);
y(len+1)=y(1);

len = length(x);
Parea = sum(ROIBW); //white pixel count (theoretical area)

Garea = sum(((x(1:1:len-1).*y(2:1:len))-(x(2:1:len).*y(1:1:len-1)))/2)+len/2; //Green's Theorem area


Area estimation results with pixel count and Green's area in pixels and the physical area in cm2:

Estimation of actual area was done using the ratios technique done in Activity 1.

I give myself a grade of 9 in this activity.

I would like to thank Mr. Paul Leonard Atchong C. Hilario and Ms. Cherry May Palomero for scanning the images for us and lending their laptops for the task and Mr. Miguel Sison for allowing us to use their group's scanner. I would also like to thank Ms. Ma. Herminia Balgos for sharing that SEM images are grayscale.

A3: Image types and basic image enhancement (NOT FINAL)

Figure 1. Truecolor image.

Figure 2. Truecolor image histogram.

Figure 3. Indexed image.

Figure 4. Indexed image histogram.

Figure 5. Grayscale image.

Figure 6. Grayscale image histogram.

Figure 7. Binary image.

Figure 8. Binary image histogram.