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

Color Palette

This is my first attempt on designing a color palette. Of course, this looks quite simple.
The palette contains primary colors R,G and B.
%size of the square
sz=50;
%Number of rows
xvalue=10;
%Number of Columns
yvalue=10;
%Initialize the matrix with zeros
RGB=zeros([sz+2 sz+2 3]);
%The image shade from darker to lighter.Instead of 255 give values lesser than 255 and see the different shades
v=round(255/yvalue);

clear C
 m=sz+2;
 n=1;
    %The same color repetition. If you specify val=1 then the colors  R,G,B
    %will occur repeatedly
    val=floor(xvalue/3);
   
    inc=1;
for i=1:xvalue
    n1=1;
    m1=sz+2;
    if(inc==1)
        %Red color will be displayed
    num1=20;
    num2=1;
    num3=1;
    elseif(inc==2)
        %Green color will be displayed
    num1=1;
    num2=20;
    num3=1;
    elseif(inc==3)
        %Blue color will be displayed
    num1=1;
    num2=1;
    num3=25;
   
    end
    for j=1:yvalue
        RGB(2:sz+1,2:sz+1,1)=num1*v*j;
        RGB(2:sz+1,2:sz+1,2)=num2*v*j;
        RGB(2:sz+1,2:sz+1,3)=num3*v*j;
       
              
        C(n:m,n1:m1,:)=RGB;
             
        m1=m1+sz;
        n1=n1+sz;
            
      
    end
    n=n+sz;
    m=m+sz;
    if(mod(i,val)==0)
       
        inc=inc+1;
    
        if(inc>3)
            inc=1;
        end
    end
   
   
end

C=uint8(C);
imshow(C);

10X10

5X10
             



like button Like "IMAGE PROCESSING" page

Histogram of an image

                 I was trying to calculate the histogram of a 2d array and ended up in finding a method to calculate the histogram of an image.
Consider a 2d matrix:
B=[0 1 2 4 4 5; 0 0 2 2 4 5; 1 1 2 0 5 5; 4 4 2 0 1 5;];

B =
     0     1     2     4     4     5
     0     0     2     2     4     5
     1     1     2     0     5     5
     4     4     2     0     1     5


I used reshape function to convert the 2d matrix to 1 dimensional matrix.


C=reshape(B,[],1);

size of the matrix 24x1
This value 24 represents  no.of rows of B x no.of cols of B

Output of C:
C =

     0
     0
     1
     4
     1
     0
     1
     4
     2
     2
     2
     2
     4
     2
     0
     0
     4
     4
     5
     1
     5
     5
     5
     5

If you use C=reshape(B,1,[]), the size of the matrix will be 1 x 24

C =

  Columns 1 through 16

     0     0     1     4     1     0     1     4     2     2     2     2     4     2     0     0

  Columns 17 through 24

     4     4     5     1     5     5     5     5

I typecasted the variable ‘C’ to double.

C=double(C);

To find the histogram of the values, I specified the range 0 to n values.

D=hist(C,0:5)
D =
     5     4     5     0     5     5


The output shows the value 0 occurs 5 times.

B =
     0     1     2     4     4     5
     0     0     2     2     4     5
     1     1     2     0     5     5
     4     4     2     0     1     5
And 1 occurs 4 times, 2 occurs 5 times and so on.

Now, the code to find the histogram for an image. This will be handy when we try to calculate the histogram of the image, thresholding the image and histogram equalization.
MATLAB CODE:
A=imread('img.jpg');
B=rgb2gray(A);
C=reshape(B,[],1);
C=double(C);
D=hist(C,0:255);
like button Like "IMAGE PROCESSING" page

Cartesian to Polar co-ordinates:


            To find the angle of a point in the Cartesian co-ordinates, convert the co-ordinates to polar co-ordinates using the function cart2pol(x,y) . The output will be radius and the angle. The radius is calculated from the origin to the given point. The theta value will be in radians. The function rad2deg(theta) can be used to convert the radians to degree.

MATLAB CODE:
I am using this plot function to display the points in the Cartesian co-ordinates.
plot([-10 -3 10 0 0 0 0 0 2 ] ,[0 0  0 0 6 10 -10 0 3],'--rs','LineWidth',2,'MarkerFaceColor','k');
%180 degrees
[theta,radius]=cart2pol(-2,0);
deg=rad2deg(theta);

%display the radius , angle and the co-ordinates in the axes.

degree=strcat('deg:',num2str(deg));
rad=strcat('radius:',num2str(radius));
text(-3,-2,degree);
text(-3,-3,rad);
           
%90 degrees
[theta,radius]=cart2pol(0,6);
deg=rad2deg(theta);

degree=strcat('deg:',num2str(deg));
rad=strcat('radius:',num2str(radius));
text(-3,6,degree);
text(-3,5,rad);

%45 degrees
[theta,radius]=cart2pol(2,2);
deg=rad2deg(theta);

degree=strcat('deg:',num2str(deg));
rad=strcat('radius:',num2str(radius));
text(4,3,degree);
text(4,2,rad);



like button Like "IMAGE PROCESSING" page

Image Rotation in MATLAB - Examples without imrotate function

180 degree
                                                   We can develop our own code to rotate an Image.In this article, I have discussed about the built in functions and the code to rotate an image without using imrotate function. At the end of the article you can find the matlab code.  First I tried to rotate an Image by using built in functions in Matlab.                                                                    

            Matlab built_in function rot90(A,k) can be used to rotate images in 90 degrees.
Here is an example using rot90:  Assign K=1 for 90 degree, 2 for 180, 3 for 270 and 4 for 360.





A=imread('flower1.jpg');

figure,imshow(A);

R=rot90(A(:,:,1),1);
G=rot90(A(:,:,2),1);
B=rot90(A(:,:,3),1);

C(:,:,1)=rot90(A(:,:,1),1);
C(:,:,2)=rot90(A(:,:,2),1);
C(:,:,3)=rot90(A(:,:,3),1);


figure,imshow(C);



90 degree


The output image will be rotated 90 degrees.



Another matlab built_in function flipud(A) can be used to rotate the image 90 degrees. The actual function of flipud is to flip matrix up and down.


A=imread('flower1.jpg');

C=uint8(zeros(size(A)));
imshow(A);

R=flipud(A(:,:,1));
G=flipud(A(:,:,2));
B=flipud(A(:,:,3));

C(:,:,1)=R;
C(:,:,2)=G;
C(:,:,3)=B;


imshow(C);

180 degree


To flip the image from left to right we can use the function fliplr

Original Image

A=imread('horse2.jpg');
C=uint8(zeros(size(A)));
imshow(A);

R=fliplr(A(:,:,1));
G=fliplr(A(:,:,2));
B=fliplr(A(:,:,3));

C(:,:,1)=R;
C(:,:,2)=G;
C(:,:,3)=B;


imshow(C);













We can combine both fliplr and flipud to rotate the image (90, 180 , 270 and 360)


Now let’s see how to rotate an image without using matlab built in function imrotate.

Steps to be performed:

a.     Find the midpoints of the image.
b.     Convert the each pixel co-ordinate to polar co-ordinate.
c.      The result of conversion will yield angle and radius.
d.     Convert the angle which is in radians into degree by using the function rad2deg(theta)
e.      Add the degree value to be rotated to the value obtained in the above step.
f.       Now again convert the degree to radian by using rad2deg function
g.     Finally, convert to Cartesian co-ordinate by using the function pol2cart (theta,radius)


MATLAB CODE:

A=imread('panda2.jpg');


x1=zeros([size(A,1)*size(A,2) 1]);
x2=zeros([size(A,2)*size(A,1) 1]);

%Specify the degree
deg=90;
%Change the image size
C=uint8(zeros([size(A,1) size(A,2) 3 ]));

m=1;
%Find the midpoint
midx=ceil((size(C,1)+1)/2);
midy=ceil((size(C,2)+1)/2);

for i=1:size(A,1)
    i1=i-midx;
    for j=1:size(A,2)
        %convert from cartesian to polar
        [t,r]=cart2pol(i1,j-midy);
        %Convert from radians to degree and add the degree value
        t1=radtodeg(t)+deg;
        %Convert from degree to radians
        t=degtorad(t1);
        %Convert to Cartesian Co-ordinates
        [x,y]=pol2cart(t,r);
        x1(m)=round(x+midx);
        x2(m)=round(y+midy);
       
         
        m=m+1;
       
       
       
    end
   
end
%check whether the values are within the image size.
x1(find(x1 < 1))=1;
x2(find(x2 < 1))=1;

n=1;
for i=1:size(A,1)
    for j=1:size(A,2)
        C(x1(n),x2(n),:)=A(i,j,:);
       
        n=n+1;
    end
   
end
imshow(C);


I got holes in between when I rotated at 45 degrees. So I used ceil , floor  and round function  to convert the decimals into whole numbers. 
45 degrees
210 degrees




NOTE: Here I didn't re-size the image. May be in another post, I will concentrate on both scaling and rotating.
             Use Meshgrid to make rotation faster.


Faster Implementation: ImageRotation_part 2
like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com