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

Image Rotation in MATLAB - Examples without imrotate function – Part 2


To flip an image left to right without using ‘fliplr’
Grayscale:
 A = imread('cameraman.tif');
 LR = A(1:end,end:-1:1);
 figure, subplot(1,2,1);imshow(A);title('original');         
         subplot(1,2,2);imshow(LR);title('Flipped left to right');



RGB Image:
RGB = imread('peppers.png');
 LR  = RGB(1:end,end:-1:1,:);
 figure, subplot(1,2,1);imshow(RGB);title('original');         
         subplot(1,2,2);imshow(LR);title('Flipped left to right');



To flip an image upside down without using ‘flipud’
Gray Scale:
 A = imread('liftingbody.png');
 UD = A(end:-1:1,1:end);
 figure, subplot(1,2,1);imshow(A);title('original');         
         subplot(1,2,2);imshow(UD);title('Upside Down');


RGB Image:
 UD = RGB(end:-1:1,1:end,:);
 figure, subplot(1,2,1);imshow(RGB);title('original');         
         subplot(1,2,2);imshow(UD);title('Upside Down');


MATLAB code to rotate a gray scale Image:
A = imread('cameraman.tif');


%Specify the degree
deg = 35;



%Find the midpoint
if(deg > 155)
    midx = ceil((size(A,1))/2);
    midy = ceil((size(A,2))/2);
else
    midx = ceil((size(A,2))/2);
    midy = ceil((size(A,1))/2);
end

[y,x] = meshgrid(1:size(A,2), 1:size(A,1));
[t,r] = cart2pol(x-midx,y-midy);
t1 = radtodeg(t)+deg;

%Convert from degree to radians
t = degtorad(t1);

%Convert to Cartesian Co-ordinates
[x,y] = pol2cart(t,r);

%Add the mid points to the new co-ordinates
tempx = round(x+midx);
tempy = round(y+midy);

if ( min(tempx(:)) < 0 )
   
newx = max(tempx(:))+abs(min(tempx(:)))+1;
tempx = tempx+abs(min(tempx(:)))+1;
else
    newx = max(tempx(:));
end

if( min(tempy( : )) < 0 )
   
newy = max(tempy(:))+abs(min(tempy(:)))+1;
tempy = tempy+abs(min(tempy(:)))+1;
else
    newy = max(tempy(:));
end
tempy(tempy==0) = 1;
tempx(tempx==0) = 1;

C = uint8(zeros([newx newy]));


for i = 1:size(A,1)
    for j = 1:size(A,2)
        C(tempx(i,j),tempy(i,j)) = A(i,j);
       
    end
  
end

figure,imshow(C);

Output = C;
%FILL THE HOLES OR GAPS-NEAREST NEIGHBOR
for i = 2:size(C,1)-1
    for j = 2:size(C,2)-1
       
        temp = C(i-1:i+1,j-1:j+1);
        if(temp(5)==0&&sum(temp(:))~=0)
            pt = find(temp~=0);
           
            [~,pos] = sort(abs(pt-5));
            Output(i,j) = temp(pt(pos(1)));
          
        end
       
    end
end
figure,imshow(uint8(Output));


EXPLANATION:
This is a faster implementation of the older version and also the gaps are filled with the nearest neighbor .



MATLAB code to rotate a RGB Image
A=imread('peppers.png');


%Specify the degree
deg=45;

%Find the midpoint
if(deg>155)
    midx = ceil((size(A,1))/2);
    midy = ceil((size(A,2))/2);
else
    midx = ceil((size(A,2))/2);
    midy = ceil((size(A,1))/2);
end

[y,x] = meshgrid(1:size(A,2), 1:size(A,1));
[t,r] = cart2pol(x-midx,y-midy);
t1 = radtodeg(t)+deg;
%Convert from degree to radians
t = degtorad(t1);
%Convert to Cartesian Co-ordinates
[x,y] = pol2cart(t,r);
tempx = round(x+midx);
tempy = round(y+midy);

if ( min(tempx ( : ) ) < 0 )
   
newx = max(tempx(:))+abs(min(tempx(:)))+1;
tempx = tempx+abs(min(tempx(:)))+1;
else
    newx = max(tempx(:));
end

if( min(tempy ( : ) ) < 0 )
   
newy = max(tempy(:)) + abs(min(tempy(:)))+1;
tempy = tempy + abs(min(tempy(:)))+1;
else
    newy = max(tempy(:));
end
tempy(tempy==0) = 1;
tempx(tempx==0) = 1;


C = uint8(zeros([newx newy 3]));





for i = 1:size(A,1)
    for j = 1:size(A,2)
        
        C( tempx(i,j),tempy(i,j) ,:) = A(i,j,:);
      
       
    end
  
end

figure,imshow(C);


Output=C;
%FILL THE HOLES OR GAPS - NEAREST NEIGHBOR
for i = 2:size(C,1)-1
    for j = 2:size(C,2)-1
       
        temp = C(i-1:i+1,j-1:j+1,:);
        if(temp(5)==0 && sum(temp(:))~=0)
           
             pt = find(temp(:,:,1)~=0);
            [~,pos] = sort(abs(pt-5));
           
            temp = C(i-1:i+1,j-1:j+1,1);
            Output(i,j,1) = temp(pt(pos(1)));
           
            temp = C(i-1:i+1,j-1:j+1,2);
            Output(i,j,2) = temp(pt(pos(1)));
           
            temp = C(i-1:i+1,j-1:j+1,3);
            Output(i,j,3) = temp(pt(pos(1)));
        end
       
    end
end


figure,imshow(uint8(Output));title([num2str(deg),'Degree']);


like button Like "IMAGE PROCESSING" page

Upsampling in MATLAB

Upsampling is the process of inserting zeros in between the signal value in order to increase the size of the matrix.  We will discuss about upsampling in both spatial and time domain.
1.1  Upsampling using MATLAB built-in function
1.2  Upsampling in 1D
1.3  Upsampling in 2D or image matrix
2.1  Upsampling a 1D signal
2.2  Upsampling a image matrix

UPSAMPLING IN SPATIAL DOMAIN:

Given: 1-D array of size 1xN
Upsample by factor of 2:  

Output: 1-D array of size 1x(2*N)
Where N represents length of the array, n represents the index starting from 0,1,2…,N
%UPSAMPLING USING MATLAB BUILT-IN FUNCTION 'UPSAMPLE'

A = 1:25;
B = upsample(A,2);

EXPLANATION:

The above MATLAB function will insert zeros in between the samples.

To upsample an array by ratio 2, update the output array as follows:
1.      If index(n) of the output array is divisible by 2(ratio), then update the output array with the value of the input array with index n/2
2.      Otherwise, insert zero

STEPS TO PERFORM:
1.      Consider an array A of size 1xM
2.      Obtain the upsample Ratio N
3.      Pre-allocate an array B of size 1x(M*N)
4.      If the index is divisible by N then update the array B with value of A else zero


MATLAB CODE:

%UPSAMPLING IN SPATIAL DOMAIN

A = 1:10;

%UPSAMPLING WITH RATIO 2
B = zeros([1 size(A,2)*2]);
B(1:2:end)=A;



%UPSAMPLING WITH RATIO N
N=3;
B=zeros([1 size(A,2)*N]);
B(1:N:end)=A;

EXPLANATION:
Let A = [1 2 3 4 5]
Let us upsample try to upsample A with ratio 2
Pre-allocate output matrix B = [0 0 0 0 0 0 0 0 0 0]
Substitute the value of the matrix A in indices divisible by the ratio i.e. 2 of matrix B
B(1:2:end) = A
Now B(1,3,5,7,9) =[1 2 3 4 5]
Thus B = [1 0 2 0 3 0 4 0 5 0]

NOTE:
Definition of upsampling is usually given assuming the index starts from zero. But in case of MATLAB, the index starts with one that is why the odd positions are considered instead of even.

STEPS TO PERFORM TO UPSAMPLE A 2D MATRIX:


MATLAB CODE:



%IMAGE UPSAMPLING

A = imread('cameraman.tif');

M = 2;
N = 3;

B = zeros([size(A,1)*M size(A,2)*N]);
B(1:M:end,1:N:end) = A;

figure,imagesc(B);colormap(gray);


sz = M*N;
H = fspecial('average',[sz sz]);
C = conv2(B,H,'same');

figure,imagesc(C);colormap(gray);


EXPLANATION:


The above image is the pixel representation of the zero inserted image. In each row, two zeros are inserted between the pixels and in the each column; single zero is inserted between the pixels. In spatial domain, inserting zeros will be quite visible so it’s advisable to perform any low pass filtering or approximation like spatial averaging or applying Gaussian.
In the above example, spatial averaging is done. In order to understand how spatial averaging is done using convolution check the following link:








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