0% found this document useful (0 votes)
57 views12 pages

Lecture 2: Transformations and Histograms: H (K) Number of Pixels Whose Shade Equals K (U (I, J) K)

The document discusses various image transformations including histograms, shifting pixel values to make images lighter or darker, and nonlinear transformations. Specifically, it introduces histograms to characterize image intensity distributions and describes how transformations like shifting, piecewise linear functions, and power functions can modify pixel values to enhance contrast or brightness. Matlab code examples are provided to implement these transformations and visualize the resulting histograms.

Uploaded by

mikimaric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views12 pages

Lecture 2: Transformations and Histograms: H (K) Number of Pixels Whose Shade Equals K (U (I, J) K)

The document discusses various image transformations including histograms, shifting pixel values to make images lighter or darker, and nonlinear transformations. Specifically, it introduces histograms to characterize image intensity distributions and describes how transformations like shifting, piecewise linear functions, and power functions can modify pixel values to enhance contrast or brightness. Matlab code examples are provided to implement these transformations and visualize the resulting histograms.

Uploaded by

mikimaric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lecture 2: Transformations and Histograms

Images often are said to be light or dark or to lack contrast. In an attempt to be more precise about this, we use the matrix representation of an image. Each pixels image intensity is recorded by a component in its matrix. If the component is small (large), then the pixel is dark (light). If there are a large number of dark (light) pixels, then one may say the image is dark (light). Let k be an integer from 0 to 255 and represent a shade for a pixel at some location (i,j) in an mxn image matrix, u. Define a histogram of the image u to be the function from integers 0:1:255 to the integers 0:1:mn given by h(k) = number of pixels whose shade equals k (u(i,j) = k). The normalized histogram is H(k) = h(k)/(mn). If the image is dark (light), the histogram will be shifted to the left (right). If the image lacks contrast, the histogram will be clustered in the center. Example Pollen Histogram.

Matlab Code pollen.m Creates Histogram


clear; pollenmat = imread('Fig3.10(b).jpg'); u = double(pollenmat); [nx ny] = size(u) nshades = 256; hist= zeros(nshades,1); for i=1:nx for j=1:ny for k=0:nshades-1 if u(i,j)==k hist(k+1)=hist(k+1)+1; end end end end plot(hist);

2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2

x 10

0 0

50

100

150

200

250

300

Matlab Code pollendark.m Creates Histogram for Darker Image


clear; pollen = imread('Fig3.10(b).jpg'); u = double(pollen); [nx ny] = size(u) nshades = 256; udark = u - ones(nx,ny)*50; % Transform by shifting to dark. hist= zeros(nshades,1); for i=1:nx for j=1:ny for k=0:nshades-1 if udark(i,j)==k hist(k+1)=hist(k+1)+1; end end end end figure plot(hist) pollendarkmat = uint8(udark); imwrite(pollendarkmat, 'pollendark.jpg');

2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0

x 10

50

100

150

200

250

300

Matlab Code pollenlight.m Creates Histogram for Lighter Image


clear; pollen = imread('Fig3.10(b).jpg'); u = double(pollen); [nx ny] = size(u) nshades = 256; ulight = u + ones(nx,ny)*100; % Transform by shifting to light. hist= zeros(nshades,1); for i=1:nx for j=1:ny for k=0:nshades-1 if ulight(i,j)==k hist(k+1)=hist(k+1)+1; end end end end plot(hist); pollenlightmat = uint8(ulight); imwrite(pollenlight, 'pollenlightmat.jpg');

2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0

x 10

50

100

150

200

250

300

The above two Matlab codes transformed the grayscale of each pixel, by shifting the component of the image matrix up or down: T(h(i,j)) = h(i,j) + or - shift = new image matrix component. There are many other possibilities. Generally, any transformation of the image should be given by a mapping from the integers 0:1:255 into the integers 0:1:255. The mapping does not need to be onto the set 0:1:255, and some times there are transformations whose ranges may not be a subset of 0:1:255! Next we present two additional examples of transformation: the piecewise linear and the power transformations Piecewise Linear Transformation with Three Linear Parts. T(r) = s where r and s are in the integers 0:1:255. The new image matrix will have components equal to T(h(i,j)) The values of (r1,s1) and (r2,s2) can be chosen to obtain desired image enhancements. In the following code, they are chosen so as the give better contrast of the pollen image. The histogram of the original pollen image indicated a clustering of the middle shades. The choice of (r1,s1) and (r2,s2) were made so as to spread these over a larger range of shades.

T(r)=s (255,255) (r2,s2)

(r1,s1) r

Matlab Code pollenspread.m Gives Better Contrast.


clear; pollen = imread('Fig3.10(b).jpg'); u = double(pollen); [nx ny] = size(u) nshades = 256; r1 = 80; s1 = 10; % Transformation by piecewise linear function. r2 = 140; s2 = 245; for i = 1:nx for j = 1:ny if (u(i,j)< r1) uspread(i,j) = ((s1-0)/(r1-0))*u(i,j) end if ((u(i,j)>=r1) & (u(i,j)<= r2)) uspread(i,j) = ((s2 - s1)/(r2 - r1))*(u(i,j) - r1)+ s1; end if (u(i,j)>r2) uspread(i,j) = ((255 - s2)/(255 - r2))*(u(i,j) - r2) + s2; end end end hist= zeros(nshades,1); for i=1:nx for j=1:ny for k=0:nshades-1 if uspread(i,j)==k hist(k+1)=hist(k+1)+1; end end end end plot(hist); pollenspreadmat = uint8(uspread); imwrite(pollenspreadmat, 'pollenspread.jpg');

6000

5000

4000

3000

2000

1000

50

100

150

200

250

300

Power Transformation with c(r/255)^p. If 0< x <1, then x^2 (x^.5) will be concave up (down) and between 0 and 1. The transformation T( r) = c255(r/255)^2 ( = c255(r/255)^.5) will tend to move light (dark) pixels to darker (lighter) pixels. The following Matlab codes illustrate these for an aerial photo and a Mars rover photo. It makes use of Matlabs array operations, which are different than matrix operations. For example, matrix squared is the matrix times itself, and array squared is another matrix whose components are the squares of the array components. This is illustrated by a 2x2 array or matrix where matrix^2 is matrix*matrix, and matrix.^2 is another 2x2 matrix whose components are [1^2 2^2; 3^2 4^2].

>> matrix = [1 2; 3 4] matrix = 1 3 2 4 % matrix product

>> matrix^2 ans = 7 10 15 22 >> matrix.^2 ans = 1 9 4 16

% array product

Matlab Code aerialpower.m with p = 2


clear; aerial = imread('Fig3.09(a).jpg'); % aerial photo u = double(aerial); [nx ny] = size(u) c = 1.; power = 2; newu = 255*c*(u/255).^power; aeria11 = uint8(newu); imwrite(aeria11, 'aerial1.jpg');

Matlab Code mars3power.m with p = .5


clear; mars3 =imread('mars3.jpg'); u = double(mars3); [nx ny] = size(u) c = 1.; gamma = .5; newu =255*c*(u/255).^gamma; mars3 = uint8(newu); imwrite(mars3, 'mars31.jpg');

You might also like