Problems with Matlab Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you Matlab Codes for Registered Members of this site only, at free service...Follow Me.

Histogram of an image using matlab


    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);

0 comments:

Post a Comment

Recent Comments

Popular Matlab Topics

Share your knowledge - help others

Crazy over Matlab Projects ? - Join Now - Follow Me

Sites U Missed to Visit ?

Related Posts Plugin for WordPress, Blogger...

Latest Articles

Special Search For Matlab Projects

MATLAB PROJECTS

counter

Bharadwaj. Powered by Blogger.