Digital Image Processing Transformation Examples

Post on 16-Oct-2014

1.341 views 3 download

Transcript of Digital Image Processing Transformation Examples

Exercise 1: Write a function named ‘transform’ to implement intensity

transformations:

INPUTS: 1) Image, 2) Transformation name i.e. ‘inverse’, ‘power’, or ‘log’.

OUTPUTS: Show your results for each of the transformations and give a comparison between log and power transformations with reference to your findings/observations.

Solution:

M File:function transform(name,img)if(strcmp(name,'inverse')==1) [m n]=size(img); for i=1:m for j=1:n img1(i,j)=255-img(i,j); end end imshow(img),figure,imshow(img1) endif(strcmp(name,'power')==1) img1=mat2gray(img); disp('Power Law Transformation Formula is c*(img(x,y))^r'); c=input('Enter Constant c :'); r=input('Enter Gamma Correction value r (gamma):'); [m n]=size(img1); for i=1:m for j=1:n img2(i,j)=c*img1(i,j)^(r); end end imshow(img),figure,imshow(img2)endif(strcmp(name,'log')==1) img2=mat2gray(img); [m n]=size(img2); c=input('Log Transformation formula is c*log(1+img(x,y)), Enter constant c :'); for i=1:m for j=1:n img1(i,j)=c*log(1+img2(i,j)); end end imshow(img),figure,imshow(img1)end

Functional Call from Command Window for ‘inverse’:

>> img=imread('rice.png');>> transform('inverse',img)

Fig 1 Result of inverse transformation on “rice.png”

Functional Call from Command Window for ‘power’:

>> transform('power',img)Power Law Transformation Formula is c*(img(x,y))^rEnter Constant c :1Enter Gamma Correction value r (gamma):0.3

Fig 2 Result of power transformation on “rice.png”Functional Call from Command Window for ‘log’:

>> transform('log',img)Log Transformation formula is c*log(1+img(x,y)), Enter constant c :2

Fig 3 Result of log transformation on “rice.png”

Comparison between power and log transformation results:

As we can see from the log and power transformation results that in both the cases low range of input intensities are mapped to higher range of output intensities but in the power transformation there is a slight wash out effect on the output image and in the log output image a little bit sharpness is there. When using power transformation value of gamma greater then 1 would result in a darker image and value less then 0 would result in a brighter image.

Exercise 2: Write a function named ‘myhist’ to MANUALLY compute

image histogram as described in the LAB:

Solution:

M File:function imhist1(img)[m n]=size(img);k=0:1:255;k(1:256)=0;for i=1:m for j=1:n if(j==img(i,j)) k(j)=k(j)+1; end endendfor h=1:255 title('Bar Graph'); xlabel('gray-levels', 'fontsize', 10); ylabel('frequescy', 'fontsize', 10); bar(h,k(h),'b') % plot(h,k(h),'-') hold on;endl=imhist(img);,figure,bar(l);,figure,for h=1:255 title('Stem Graph'); xlabel('gray-levels', 'fontsize', 10); ylabel('frequescy', 'fontsize', 10); stem(h,k(h),'r') % plot(h,k(h),'-') hold on;end,figure,stem(l);

Functional Call from Command Window:

>> img=imread('rice.png');>> imhist1(img)

Output:

Bar graph:

Fig 1 Histogram of “rice.png” on bar graph

Stem Graph:

Fig 2 Histogram of “rice.png” on stem graph

Comparison between imhist() graphs and our algorithm graphs:

Figure 3 imhist () bar graph algorithm bar graph

Figure 4 imhist () stem graph algorithm stem graph

Exercise 3: Consider a function f(x) defined over an image ‘cameraman.tif’

with the intensities in the range [0 1], as defined below:

1 – x 0 < x ≤ 0.25

f(x) = 1 0.25 < x ≤ 0.5

x x > 0.5

a). Draw a graph for f(x) to show its influence on image intensities.

b). Write a MATLAB program to implement f(x) on an input image.

Also, show your results after transformation.

Solution:

a). Graph:

Figure 1: Graph showing the influence of the function on intensities

b).

M File:function slicing1(img1)[m n]=size(img1);img=im2double(img1);for i=1:m for j=1:n if(img(i,j)>0 && img(i,j)<=0.25) img2(i,j)=1-img(i,j); end if(img(i,j)>0.25 && img(i,j)<=0.5) img2(i,j)=1; end if(img(i,j)>0.5) img2(i,j)=img(i,j); end endendimshow(img),impixelinfo,figure,imshow(img2)

Functional Call from Command Window:

>> img=imread('cameraman.tif');>> slicing1(img)

Output:

Figure 2 Original Image Transformed Image

Exercise 4: Write a MATLAB program for the function shown in figure 2, on an input image ‘cameraman.tif’. Also show your results.

Solution:

M File:function slicing1(img1)[m n]=size(img1);img=im2double(img1);for i=1:m for j=1:n if(img(i,j)>0 && img(i,j)<=0.25) img2(i,j)=1-img(i,j); end if(img(i,j)>0.25 && img(i,j)<=0.5) img2(i,j)=1; end

if(img(i,j)>0.5) img2(i,j)=2*img(i,j)-1; end endendimshow(img),impixelinfo,figure,imshow(img2)

Functional Call from Command Window:

>> img=imread('cameraman.tif');>> slicing2(img)

Output:

Figure 1 Original Image Transformed Image