# IMAGE PROCESSING

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

### 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: