Sie sind auf Seite 1von 4

Date Conducted ____________________

CS707-2003(F), Lab 3[Page 1 of 4]

Histogram equalization
1. To perform histogram equalization on an image, we have to make the probability density function and the cumulative density function of the image. This involves counting the number of pixels of each color in the image, and producing a running sum of the count. Then by simply scaling the output, we can perform histogram equalization:
int pixelCount[256]; //initialize all indexes to zeros for(int x= 1; x < imageWidth ; x++) { for(int y = 1 ; y < imageHeight ; y++) { pixelCount[getpixel(x,y)]++; } } int sum = 0; for(int i= 0; i < 256 ; i++) { pixelCount[i]+=sum; Sum = pixelCount[i]; }

2. The above snippet would produce the cumulative density function (not scaled) of the image pixels. You can perform histogram equalization by simply reading a pixels intensity, looking it up in the array pixelCount and scaling it accordingly to set the pixels new value. Note that we can perform image equalization on the image without the need for a temporary space ( we can overwrite values of pixels as we move along) but we cannot do this for filtering as we always place the result in a separate image/space (why?). 3. As for histogram specification, a brief description here would suffice. Write a short routine to output 256 values to the console. The values can be according to some criteria (such as a log function, a Gaussian probability density function etc). Use these values to perform histogram specification on your image. 4. Write a procedure that would display the histogram of an image graphically. This would help you to visualize the changes introduced by the histogram equalization and specification operation in an image.

MatLab Commands
To perform the above operations, as well as some other basic operations, in MatLab, we use the following commands.

Date Conducted ____________________

CS707-2003(F), Lab 3[Page 2 of 4]

img = imread (imageName); imGray = rgb2gray(img); myHist = imhist(imGray); eqImage = imhisteq(imGray); figure, imshow(imGray); figure, imshow(eqImage);

1. The first line reads the image file into a 3 dimensional array (x, y, color). MatLab can read many image file formats, so you do not have to worry about the details (like the width being a multiple of four). 2. The second line converts an RGB image into a gray image. This is not necessary if the image is already a gray level image. (Please note that there is a difference between an image that "can only contain" gray color and an image that "has only" gray colors). 3. The third line returns the image histogram in the array myHist. This can be used to analyze the image histogram and possibly perform histogram specification. 4. The fourth line performs histogram equalization. Note that this doesnt require you to pre-compute the histogram as we did here (in step 3), instead we could have performed histogram equalization without line 3. 5. The final two lines display the un-equalized image and the equalized image. The function that performs this is imshow, however if you use it alone, it would overwrite the image previously displayed in the window, so to open a new window, we use the figure command. 6. In order to view the histogram of the image, you will supply two output arguments to the function for histogram equalization. The output arguments are the transformation function and the equalized image respectively. This is given as follows:
[eqImage, transfFunc] = imhisteq(imGray); figure, plot(transfFunc); figure, imshow(eqImage);

The second and third lines display the transformation function and the equalized image respectively. 7. In order to perform histogram specification, we use the same function as before (histeq), but include a specified histogram as the second argument as in the first line below
[imSpec, transSpec] = histeq(img, specPDF); figure, imshow(imSpec), title('Matched Image'); [imSpecEq, transfSpecEq] = histeq(imSpec); figure, plot(transfSpecEq), title('Specified Image Histogram...');

The second line simply displays the matched image, the third line gets the histogram of the matched image, and the last line plots the histogram, which should match the specified PDF. 8. Remember the PDF is supplied to the histogram equalization function, not a CDF, it automatically creates a CDF from the PDF supplied to it. The figures below illustrate this difference:

Date Conducted ____________________

CS707-2003(F), Lab 3[Page 3 of 4]

9. The complete script for the equalization and specification demonstration is something like this:
function = histogramDemo(imageFileName) % im= imread('c:\3.bmp'); im= imread(imageFileName); img = rgb2gray(im); for i=1:1:256 specPDF(i) = log(i/256); %Log Transform % specPDF(i) = power(i/256,3); %Power Function end specPDF = specPDF-min(specPDF); figure, plot(specPDF), title('Specified CDF...'); sum = specPDF(1); for i=256:-1:2 specPDF(i)= specPDF(i)-specPDF(i-1); end figure, plot(specPDF), title('Specified PDF...'); figure, imshow(img), title('Original Image');

Date Conducted ____________________

CS707-2003(F), Lab 3[Page 4 of 4]

[imEq, transEq] = histeq(img); figure, imshow(imEq), title('Equalized Image'); % figure, plot(transEq), title('Equalization Function'); [imSpec, transSpec] = histeq(img, specPDF); figure, imshow(imSpec), title('Matched Image'); % figure, plot(transSpec), title('Specification transform'); [imSpecEq, transfSpecEq] = histeq(imSpec); figure, plot(transfSpecEq), title('Specified Image Histogram...');

Das könnte Ihnen auch gefallen