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
Previous Post Next Post Home
Google ping Hypersmash.com