HW2, Computer problem, Part (b)
Histogram equalization
EECS 203A, Digital Image Processing. UCI. Fall 2004
Nasser Abbasi


Read the image file, cat.raw, and generate the histogram table, then the probability density function PDF table, then the cumulative density function CDF table. Then scan the image and generate new pixel gray level values by a lookup to the CDF.

Clear["Global`*"] ;

Now set current directory to where the image is (same folder as this note book) and read the image file

SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[] ... pixel value in data=", Max[data]] ; Print["Min pixel value in data=", Min[data]] ;

Max pixel value in data=186

Min pixel value in data=0

Now that the image is read into data, we display it before processing it.

ListDensityPlot[Reverse[Partition[data, cols]], MeshFalse, FrameFalse, ImageSize {rows, cols}, PlotRangeAll, AspectRatioAutomatic] ;

[Graphics:HTMLFiles/index_6.gif]

makeHistogram[data_] := Module[{h, i, r, nPixels, L}, L = 256 ; Print["np ... 4;r, 2〛 = h〚r, 2〛 + 1 ; } ] ; Return[h] ; ] ;

makePDF[h_, nPixels_] := Module[{i, pdf, L}, L = Length[h] ; pdf = Table[{0, 0 ... 2〛 = h〚i, 2〛/nPixels ; ] ; Return[pdf] ; ] ; 

makeCDF[pdf_] := Module[{cdf, L, i}, L = Length[pdf] ; cdf = Table[{0, 0}, {i, ... , 2〛 + cdf〚i - 1, 2〛 ; ] ; Return[cdf] ; ] ; 

(*Print the histogram*)(*TableForm[h] *)h = makeHistogram[data] ; List ... PlotLabel {"Histogram of cat.raw image, num pixels=",   Length[data]}]

npixels=307211

h table size= {256, 2}

[Graphics:HTMLFiles/index_13.gif]

⁃Graphics⁃

pdf = makePDF[h, Length[data]] ; <br />(*Print the PDF*) ListPlot[pdf〚All, 2〛, P ... 〛, PlotLabel {"CDF of cat.raw image, num pixels=",   nPixels}]

[Graphics:HTMLFiles/index_17.gif]

⁃Graphics⁃

[Graphics:HTMLFiles/index_19.gif]

⁃Graphics⁃

(*Now generate new image*)nPixels = Length[data] ; s = Table[0, {i, nP ... ;i〛 + 1 ; s〚i〛 = Round[ cdf〚r, 2〛 (L - 1) ] ; ]

Print["Max pixel value in s=", Max[s]] ; Print["Min pixel value in s=", Min[s]] ;

Max pixel value in s=255

Min pixel value in s=0



(*Now display the new image*)ListDensityPlot[Reverse[Partition[s, cols]], Mesh ... se, ImageSize {rows, cols}, PlotRangeAll, AspectRatioAutomatic] ; 

[Graphics:HTMLFiles/index_27.gif]

(*Generate histgram and CDF for new image and plot to compare*)h = makeHistogram[s] ;  ...  {"Histogram of transformed cat.raw image, num pixels=",   Length[s]}]

npixels=307211

h table size= {256, 2}

[Graphics:HTMLFiles/index_31.gif]

⁃Graphics⁃

pdf = makePDF[h, Length[s]] ; ListPlot[pdf〚All, 2〛, PlotLabel {"PDF of transformed cat.raw image, num pixels=",   nPixels}]

[Graphics:HTMLFiles/index_34.gif]

⁃Graphics⁃

cdf = makeCDF[pdf] ; ListPlot[cdf〚All, 2〛, PlotLabel {"CDF of transformed cat.raw image, num pixels=",   nPixels}]

[Graphics:HTMLFiles/index_37.gif]

⁃Graphics⁃

(* Now write the new image to file *)fileName = "cat_histogram_equalized. ... es`WriteBinary[strm, s, Byte] ; Close[strm] ; (*That is all folks ... . *)

Conclusion

With histogram equalization applied, we see that new image contains more gray levels than the original level. Looking at generated plots of the new image histrogram and PDF and CDF we see from the CDF that the distribution of gray levels is uniform in the new image.


Created by Mathematica  (October 19, 2004)