Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Gray Scale to Pseudo color transformation


   Transformation of a gray scale image into pseudo color image helps in better visualization of the image. In this tutorial, different ways to apply pseudo color transformation to a gray scale image will be discussed along with the MATLAB Code.
The main idea behind pseudo color transformation is to perform three independent transformation (RED,GREEN and BLUE) on the grayscale or intensity image and map  the corresponding intensity value in the image to the result obtained.


Steps to be performed:





Functional Block Diagram

Reference: Digitial Image Processing by Gonzalez

MATLAB CODE:

%READ AN INPUT IMAGE
A = imread('cameraman.tif');
%PRE-ALLOCATE A MATRIX
Output = zeros([size(A,1) size(A,2) 3]);

%Define a colormap
map = colormap(jet(256));

%Assign the columns to 1-D RED,GREEN and BLUE
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

%MAP THE COLORS BASED ON THE INTENSITY OF THE IMAGE

Output(:,:,1) = Red(A);
Output(:,:,2) = Green(A);
Output(:,:,3) = Blue(A);


Output = im2uint8(Output);
%DISPLAY THE IMAGE
imshow(Output);

%Save the image in PNG or JPEG format
imwrite(Output,'pseudo_color.jpg');






 Checkerboard with colormaps:



%READ AN INPUT IMAGE
A = imread('coins.png');
%RESIZE THE IMAGE
A = imresize(A,[256 256]);

%PRE-ALLOCATE THE OUTPUT MATRIX
Output = zeros([size(A,1) size(A,2) 3]);

%DEFINE SOME COLORMAPS
maps={'jet(256)';'hsv(256)';'cool(256)';'spring(256)';'summer(256)';'parula(256)';'hot(256)'};

%COLORMAP 1
map=colormap(char(maps(7)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

R1 = Red(A);
G1 = Green(A);
B1 = Blue(A);

%COLORMAP 2
map=colormap(char(maps(6)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);
R2 = Red(A);
G2 = Green(A);
B2 = Blue(A);

%SIZE OF THE MATRIX
sz=64;

ind=0;

for i=1:sz:size(A,1)-(sz-1)
for j=1:sz:size(A,2)-(sz-1)

%APPLY COLORMAPS BASED ON THE SIZE SZ
if(mod(ind,2)==0)

            Output(i:i+(sz-1),j:j+(sz-1),1) = R1(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),2) = G1(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),3) = B1(i:i+(sz-1),j:j+(sz-1));




else
            Output(i:i+(sz-1),j:j+(sz-1),1) = R2(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),2) = G2(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),3) = B2(i:i+(sz-1),j:j+(sz-1));


end

        ind=ind+1;
end

    ind=ind+1;
end
Output = im2uint8(Output);

%FINAL IMAGE
imshow(Output);

EXPLANATION:
The above code is to combine two colormaps to obtain checkboard effect on the image. To obtain checkboards in different size change the 'sz' variable with 4,8,16..etc.




UPPER OR LOWER TRIANGLE :

MATLAB CODE:

%READ INPUT IMAGE
A = imread('coins.png');
%RESIZE IMAGE
A = imresize(A,[256 256]);
%PRE-ALLOCATE THE OUTPUT MATRIX
Output = ones([size(A,1) size(A,2) 3]);

%COLORMAPS
maps={'jet(256)';'hsv(256)';'cool(256)';'spring(256)';'summer(256)';'parula(256)';'hot(256)'};


%COLORMAP 1
map=colormap(char(maps(1)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

R1 = Red(A);
G1 = Green(A);
B1 = Blue(A);


%COLORMAP 2
map=colormap(char(maps(7)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);




%RETRIEVE POSITION OF UPPER TRIANGLE
[x,y]=find(triu(Output)==1);
Output(:,:,1) = Red(A);
Output(:,:,2) = Green(A);
Output(:,:,3) = Blue(A);

for i=1:numel(x)

        Output(x(i),y(i),1)=R1(x(i),y(i));
        Output(x(i),y(i),2)=G1(x(i),y(i));
        Output(x(i),y(i),3)=B1(x(i),y(i));

end

Output = im2uint8(Output);

%FINAL IMAGE
imshow(Output);


EXPLANATION:
Upper triangular part of the image matrix with a colormap and lower triangular part of the image matrix with different colormap.


like button Like "IMAGE PROCESSING" page

Fast Fourier Transform on 2 Dimensional matrix using MATLAB



Fast Fourier transformation on a 2D matrix can be performed using the MATLAB built in function ‘fft2()’.

Fourier transform is one of the various mathematical transformations known which is used to transform signals from time domain to frequency domain.
The main advantage of this transformation is it makes life easier for many problems when we deal a signal in frequency domain rather than time domain.

Example:
%FOURIER TRANSFORM ON A MATRIX
A  = zeros(5);
A ( : ) = 1:25;
display(A);
F_FFT  = fft2(A);
display(F_FFT);
%INVERSE FOURIER TRANSFORM

I_FFT = ifft2(F_FFT);
display(abs(I_FFT));

NOTE on ABSOLUTE VALUE:
When we use FFT2() or FFT(),the result we obtain in the frequency domain is of complex data type.
i.e It contains both the real  as well as the imaginary part.
Let   A=10+5i
A is a complex number as it contains both real and imaginary part.In this particular case ‘10’ is the real part and ‘5’ is the imaginary part.
abs(A) = 11.1803 is the absolute (also called modulus in few books or notations) value of A which is nothing but the magnitude. It can be arrived by using the below mentioned formula:
abs(A) = sqrt(real part^2+imaginary part^2).
              = sqrt(10^2+5^2)
              = sqrt(125)
             = 11.1803 (approx)
Let’s try to understand how the Fourier transform on 2 dimensional data works with a simple example.
This method will be helpful to understand the up sampling and down sampling in both spatial and frequency domain.


1.       Consider a matrix A

2.       Perform 1 D Fast Fourier transform(FFT) on each row

1D FFT on first row (Note that the absolute value is only displayed and not the actual imaginary number):


Similarly, perform  1D FFT on each row:


NOTE: The figure represents the 1 D FFT on each row and the result is the absolute value of the complex data obtained using FFT.
3.       Perform 1 D Fast Fourier transform on each column.
On the matrix obtained from the previous step, compute 1D FFT column wise.


4.       Display the results obtained.


Flow Chart for Fast Fourier Transform on 2D :

INVERSE FOURIER TRANSFORM:

1.       Perform Inverse Fourier Transform on each column

2.       Perform IFFT on each row

3.       Display the original data


MATLAB CODE:
A=[110 20 140 0 220;
   60 34 23 198 20;
   15 12 126 230 15;
   140 28 10 28 10;
   11 12 19 85 100];

FFT_row = zeros(size(A));
FFT_col = zeros(size(A));

%Perform FFT on each row
for i=1:size(A,1)
FFT_row(i,:) = fft(A(i,:));
end

display(FFT_row);
%display(abs(FFT_row));

%Perform FFT on each column

for i=1:size(A,2)
FFT_col(:,i) = fft(FFT_row(:,i));
end

display(FFT_col);
%display(abs(FFT_col));

%INVERSE FOURIER TRANSFORM

IFFT_row = zeros(size(A));
IFFT_col = zeros(size(A));

%Perform Inverse Fourier Transform on each column
for i=1:size(A,2)
IFFT_col(:,i) = ifft(FFT_col(:,i));
end



%Perform IFFT on each row

for i=1:size(A,2)
IFFT_row(:,i) = ifft(IFFT_col(:,i));
end


display(abs(A))





ALTERNATE METHOD FOR INVERSE FOURIER TRANSFORM:
Instead of using ifft2() or ifft(), we can also use the following method to obtain the original data from the Fast Fourier transformed result :
1.       Obtain the conjugate of the Forward FFT
2.       Perform Forward fast Fourier transform
3.       Obtain the conjugate of the result from step 2.
4.       Divide it by the number of elements present in the matrix
5.       Obtain the original matrix


MATLAB CODE:
Conj_F = conj(F_FFT);
Conj_FFT = fft2(Conj_F);
IFFT_conj = conj(Conj_FFT)/numel(Conj_FFT)
display(abs(IFFT_conj));



Reference: Digital Image Processing  by Rafael C.Gonzalez, fourth Chapter.


like button Like "IMAGE PROCESSING" page

Basic Intensity Transformation Functions – Part 1

Basic Intensity Transformation Functions – Part 1

Three basic types of functions used for image Enhancement are:
1.       Linear transformation
2.       Logarithmic transformation
3.       Power Law transformation
Consider an Image r with intensity levels in the range [0 L-1]
1.       Image Negatives

Equation : s = L – 1 – r
Consider  L  = 256 and r be the intensity of the image(Range 0 to 255)


MATLAB CODE:

A=imread('rice.png');
figure,imshow(A);title('Original Image');


%Image Negative
L=256;
s= (L-1)-A;
figure,imshow(s);title('Image negative -> S = L - 1 - r')

EXPLANATION:
Consider array r = [ 1 10 255 100]
  S = 256 – 1 – r gives [ 254 245 0 155]




2.       Log Transformation

Equation:  s = c log(1 + r) where c is a constant

Consider  c = 1 and r be the intensity of the image(Range 0 to 255)

%Log Transformation

%Input Image in type double
r=double(A);
C=1;
S=C*log(1+r);
Temp=255/(C*log(256));
%Display image range [0 255]
B=uint8(Temp*S);
figure,imshow(B);title('Log Transformation -> S = clog(1+r)');



EXPLANATION:
a.       Convert the image to type double
b.      Apply the log transformation
c.       Map the obtained values to the range [0 255]

3.       Power –Law (Gamma) corrections

Equation :


Where c and gamma are positive constants

Consider c = 1, gamma =0.04 and r be the intensity of the image (Range 0 to 255)


G=0.40;%Gamma =0.40
S=C*(r.^G);
Temp=255/(C*(255.^G));
%display image range [0 255]
S1=uint8(Temp*S);
figure,imshow(S1);title('Gamma corrected Image -> S = cr^\gamma  \gamma = 0.40 c = 1');





Plots of the Equation:

%Power Law(Gamma) Transformation
GRng=[0.04; 0.10; 0.20; 0.40; 0.67; 1; 1.5; 2.5; 5.0; 10.0; 25.0];
R=0:255;
figure,
for i = 1 : 11
X=C*(R.^GRng(i));
Temp=256/X(256);
s=Temp*X;
plot(R,s);
title('Plot Equation S = Cr^\gamma');
xlabel('Input Intensity Level,r');
ylabel('Output Intensity Level,s');
text(R(175),s(175),['\gamma = ',num2str(GRng(i))],'HorizontalAlignment','left');
hold all
axis([0 255 0 255]);
end


EXPLANATION:
The transformation is plotted for different values of gamma for the intensity levels [ 0 255].
The output image intensity values are mapped to the range [0 255]










like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com