Histogram manipulation is an image processing method that is used to enhance the contrast and improve the quality of an image. [1]
In this activity, a poor contrast image found in the web is to be enhanced by manipulating a property. Initially, the image has poor contrast, and the details are not well resolved.

Left: Histogram of original image. Right: Cumulative distribution function of original image. We can see that in the histogram, the grayscale values of the pixels vary only over a small range, that is why the image is mostly gray and has poor contrast. The CDF is non-linear with a high gradient in the middle, which means the values of the histogram are mostly in the middle.
To enhance this image, we will tinker with its histogram, or its Probability Density Function (PDF). The Cumulative Distribution Function (CDF) will be projected to a desired CDF to get the desired grayscale values for a pixel. This technique is called histogram backprojection. In this technique, the desired CDF serves as a lookup table for manipulation of the histogram. [3]
Histogram Backprojection
To do this, pixel per pixel, we get the grayscale value of the original image. The value of the CDF for this grayscale value is checked in the desired CDF, and the corresponding desired grayscale value for that CDF value replaces the original grayscale value in the image.
Step by step, this is how histogram backprojection is implemented:
- For each pixel, get the grayscale value and obtain its CDF value.
- Get the desired grayscale value for this CDF value from the desired CDF. This can be done when you know the inverse function of your desired CDF. Initially, the desired CDF is linear.
- Replace the grayscale value of the current pixel by the calculated desired grayscale value.
The result:


Histogram and Cumulative Distribution Function of processed image. Now, the histogram is well-spread. The values are not concentrated in the middle, and the range of possible grayscale values include the extreme values.The backprojection was correctly implemented because the desired linear CDF was achieved.
Nonlinear Cumulative Distribution Function
Next, a non-linear CDF is now desired. The same procedure as above was done for the desired non-linear CDF. For this example, the desired CDF was y = x^2.

This equation is quickly increasing for the higher grayscale values, so we expect this to be whiter than the original image as well as the image processed with a linear desired CDF.


I give myself 10 points for this activity. The images were enhanced using backprojection, and the resulting images had more contrast than the original one. The program can be used to manipulate images using any desired Cumulative Distribution Function.
I thank Miguel Sison and Jaya Combinido for useful discussions.
The code:
Img = round(gray_imread("C:\Documents and Settings\Yayay\My Documents\AP186\Activity 4\cargray.jpg")*256);
scf(1);
imshow(Img,[]);
[x,y] = size(Img);
img = matrix(Img, 1 ,x*y);
// histogram and CDF of image
xvals = linspace(0,255,256);
f = tabul(img);
f(:,1)=f(:,1)+1; //removes zero as an index in next calculations
hist = zeros(1,256);
for i=1:length(f(:,1)), hist(f(i,1)) = f(i,2), end;
CDF = cumsum(hist);
scf(2);
plot(xvals, hist);
scf(3);
plot(xvals,CDF);
// equation for inverse of ideal function
function invCDF = invIdeal(px)
invCDF = px^(0.5);
endfunction
// histogram backprojection
for i=1:(x*y),
px = round(invIdeal(CDF(img(i))));
img(i) = px;
end;
img = round(img*255/max(img)); //resizing x-axis to 0-255
scf(4);
newimg = matrix(img, x,y);
imshow(newimg,[]);
// histogram and CDF of new image
fnew = tabul(img);
histnew = zeros(1,256);
fnew(:,1)=fnew(:,1)+1;
for j=1:length(fnew(:,1)),
histnew(fnew(j,1)) = fnew(j,2);
end;
CDFnew = cumsum(histnew);
scf(5);
plot(xvals, histnew);
scf(6);
plot(xvals, CDFnew);
[1] A4 - Image Enhancement by Histogram Manipulation 2009.pdf (by Dr. Maricor Soriano)
[2] http://www.generation5.org/content/2004/histogramEqualization.asp
[3] http://en.wikipedia.org/wiki/Histogram_equalization
0 comments:
Post a Comment