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

Define the transformation and generate 2 lookup tables and plot the curves. I also list the first and last 5 rows in each table for each curve to better see the values. The first column in the table is the r values and the second column is the s values.

\(\(\(
\)\(
\)\(\(Clear["Global`*"];\)


    \(applyGLT[data_, tbl_] := 
        Module[{i, out}, 
out = 
            Table[i, {i, 
                Length[data]}]; 

\ \ \ \ \ \ \ \ For[
            i = 1, i ≤ Length[data], 
            i = i + 1, 
\ \ \ \ \ \ \ out[[i]] = 
              tbl[[data[[
                    i]] + 1, 
                2]]
]; 

out = 
            Partition[out, cols]; 
rows = 
            480; 
cols := 
            640; 
ListDensityPlot[Reverse[out], 
            Mesh -> False, Frame -> False, 
            ImageSize -> {rows, cols}, PlotRange -> All, 
            AspectRatio -> 
              Automatic]; 

Print[
            Dimensions[out]]; 
Return[
            out];
];\)


    \(s[c_, γ_, r_] := 
        c\ r\^γ;\)


    process[c_, γ_] := 
      Module[{L, tbl, r}, 
L = 
          256 - 1; 
tbl = 
          Round[Table[{r\ L, s[c, γ, r]\ L}, {r, 0, 1, 
                N[1\/L]}]]; 
Print[
          TableForm[
            Take[tbl, {1, 5}]]]; 
Print["...
", 
          TableForm[
            Take[tbl, \(-5\)]]]; 
\ 
ListPlot[tbl, 
          PlotLabel -> {\*""s=c (r^γ), c="", 
              c, "γ=", γ}, Frame -> True, \ 
          Ticks -> {{0, L/4, L/2, 3  L/4, 
                L - 1}}]; 
Return[
          tbl];
]


    \(tbl1 = process[1.0, 0.67];\)

    \(Print["lookup table has size=", 
        Dimensions[tbl1]];\)

    \(tbl2 = process[1.0, 1.5];\)

    \)\)\)

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[]], {1}, 
            FrontEnd`FileName]]];
 
fileName = "cat.raw";
rows = 480; 
cols = 640;
data = 
        FastBinaryFiles`ReadListBinary[fileName, 
          Byte];

Now that the image is read into data, we display it before appying GLT on it.

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

Now apply first GLT to this image.c=1.0,γ=0.67 and display the result

out = 
        applyGLT[Flatten[data], tbl1];

    fileName = "cat_low_gamma.raw";

    strm = FastBinaryFiles`OpenWriteBinary[fileName];

    FastBinaryFiles`WriteBinary[strm, Flatten[out], 
        Byte];

    Close[strm];

Now apply second GLT to this image.c=1.0,γ=1.5 and display the result

out = applyGLT[Flatten[data], tbl2];
fileName = "cat_high_gamma.raw";
strm = 
        FastBinaryFiles`OpenWriteBinary[
          fileName];
FastBinaryFiles`WriteBinary[strm, Flatten[out], 
        Byte];
Close[strm];

Conclusion

With γ=1.5,the new image is darker than the original image.With γ=0.67,the new image is lighter than the original image.Looking at the curves we see that with smaller γ darker areas are spread more over to the lighter gray level,and the reverse happens with higher γ