0% found this document useful (0 votes)
67 views

Histogram

The document discusses image histograms and how to construct them in OpenCV. It first defines what an image histogram is and focuses on the algorithm to construct one. It then explains key functions used like calcHist() to calculate the histogram, normalize() to normalize histogram values, and rectangle() to draw the histogram. The document provides code examples and explanations of parameters to call these functions to successfully construct an image histogram.

Uploaded by

api-303634380
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Histogram

The document discusses image histograms and how to construct them in OpenCV. It first defines what an image histogram is and focuses on the algorithm to construct one. It then explains key functions used like calcHist() to calculate the histogram, normalize() to normalize histogram values, and rectangle() to draw the histogram. The document provides code examples and explanations of parameters to call these functions to successfully construct an image histogram.

Uploaded by

api-303634380
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Image Histogram

Image Histogram
Li Yicheng
LIACS
[email protected]
Abstract
This one here introduces the image Histogram. Normally when we talk about image histogram we talk
about image enhancement. We normalize the histogram or we reulationize the histogram. Since it is not
hard to understand what an image histogram is , you can just refer to it as a statistic way to analyse an
image. I basicly focus more on how to constract a histogram in this part.

I.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Algorithm in this project

double a , b ;
if ( contrast > 0 )
{
double d e l t a = 1 2 7 . c o n t r a s t / 1 0 0 ;
a = 2 5 5 . / (2 5 5 . delta 2) ;
b = a ( brightness delta ) ;
}
else
{
double d e l t a = 128. c o n t r a s t / 1 0 0 ;
a = (256. d e l t a 2 ) / 2 5 5 . ;
b = a ( brightness + delta ) ;
}
Mat dst , h i s t ;
image . convertTo ( dst , CV_8U , a , b ) ;
imshow ( " image " , d s t ) ;

Ok, first what it is to change the contrast


of an image? Just give two exmaples, a image
with a high contrast may only consist of two
value of its pixels, in a imgae with depth 8, the
value of a pixel can be either 0 or 255. a picture
with small contrast, you can assume that each
pixel enjoys the same value, their differences
are so subtle that there is no contrast among
them.
The delta in the code actually can be understood as step size between two adjacent
values that one pixel can choose from. The
valuable contrast here varies from -100 to 100.
When contrast increases, appearly a increases.
What matters here is the function of variables
tutorial

a and b. And they matters to the function image.convertTo and this would be discussed in
the later in API.

II.

Code and API

I. imread
The function is used in each of the previous
project but I did not talk a lot about it.
1 Mat imread ( c o n s t S t r i n g& filename , i n t
f l a g s =IMREAD_COLOR )

Flags specifying the color type of a loaded


image:
CV_LOAD_IMAGE_ANYDEPTH->If set, return 16-bit/32-bit image when the input has
the corresponding depth, otherwise convert it
to 8-bit.
CV_LOAD_IMAGE_COLOR->If set, always
convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE->If set,
always convert image to the grayscale one.
Here I just use
1 imread ( filename , 0 ) ;

Then, only the grayscale was read in to the


matrix.

II. convertTo

referred to sebastians work check his work in PACKT.com

Image Histogram

1 void Mat : : convertTo ( OutputArray m, i n t


rtype , double alpha =1 , double b e t a =0 )

In our program:
1 image . convertTo ( dst , CV_8U , a , b ) ;

The arithmatic expression of the fucntion


m( x, y) = saturatec ast < rtype > (
this( x, y) + )

What is the s aturate_casthere,


accroding to the
openCV document:
For example, to store r, the result of an operation, to an 8-bit image, you find the nearest
value within the 0..255 range:
I ( x, y) = min(max (round(r ), 0), 255)
Ok, now let us analyse the algorithm. Take
contrast is 100 (the highest).And brightness is 0
(no change on the original image)
let us review the algorithm :(contrast > 0)
double d e l t a = 1 2 7 . c o n t r a s t / 1 0 0 ;
a = 2 5 5 . / (2 5 5 . delta 2) ;
b = a ( brightness delta ) ;

1
2
3

r = a this( x, y) + b
= a this( x, y) + a (brightness delta)
= a (this( x, y) delta + brightness)
= a (this( x, y) delta)
Since the contrast is 100 then the delta is 127
and a is 255
According to the saturate_cast,
if this( x, y) > 127,
this( x, y) delta > 1,
r > 255,
I ( x, y) = 255,
else this( x, y) < 127,
this( x, y) delta < 0,
r < 0,
I ( x, y) = 0
And that is how the constrast is formed. if
delta = 0 then a = 1, and r can get all the
values from 0 to 255.

III.

calcHist

1 void c a l c H i s t ( c o n s t Mat images , i n t


nimages , c o n s t i n t channels ,
InputArray mask , OutputArray h i s t , i n t
dims , c o n s t i n t h i s t S i z e , c o n s t

f l o a t ranges , bool uniform=true ,


bool accumulate= f a l s e )

And let review our code :


1 c a l c H i s t (& dst , 1 , 0 , Mat ( ) , h i s t , 1 , &
histSize , 0) ;

Parameters:
channels -> List of the dims channels
used to compute the histogram. The first
array channels are numerated from 0 to
images[0].channels()-1 , the second array channels are counted from images[0].channels() to
images[0].channels() + images[1].channels()-1,
and so on.
mask -> Optional mask. If the matrix is not
empty, it must be an 8-bit array of the same
size as images[i] . The non-zero mask elements
mark the array elements counted in the histogram.
hist -> Output histogram, which is a dense or
sparse dims -dimensional array.
dims -> Histogram dimensionality that
must be positive and not greater than
CV_MAX_DIMS (equal to 32 in the current
OpenCV version).
histSize -> Array of histogram sizes in each
dimension.
uniform -> Flag indicating whether the
histogram is uniform or not (see above).
accumulate -> Accumulation flag. If it is set,
the histogram is not cleared in the beginning
when it is allocated. This feature enables you
to compute a single histogram from several
sets of arrays, or to update the histogram in
time.
so the code
1 c a l c H i s t (& dst , 1 , 0 , Mat ( ) , h i s t , 1 , & h i s t S i z e
,0) ;

means : Ithe image to be processed is the


where the dst points
Ionly one image
I calculate channal 0
Ino mask
Ioutput to hist
I hist only has one dimension
I how much bars in each dimension, (because
it can calculate more histogram at one time so

Image Histogram

it uses the pointer of an array to indentify the 3


S c a l a r : : a l l ( 0 ) , 1, 8 , 0 ) ;
parameter)
IhistImage is where we draw the rectangles
Iranges is default, uniform is true,and accuIScalar:: all (0) means we draw the rectangle
mulate is false.
black;
I-1 means the rectangle will be filled with
color
IV. normalize
Iline_type is 8(default)
Ishift is 0 (default)

1 void normalize ( InputArray s r c ,


InputOutputArray dst , double alpha =1 ,
double b e t a =0 , i n t norm_type=NORM_L2,
i n t dtype = 1, InputArray mask=noArray
() )
1 normalize ( h i s t , h i s t , 0 , h i s t I m a g e . rows ,
CV_MINMAX, CV_32F ) ;

Since we construct a background with 200


300 and there can be many points in one value
like 30000 pixels with the value 128, so we
have to normalize them in to the range 0 to
histimage.rows to control the histimage.

And now comes to the point, we need two


opposite point to specify how big the rectangle
is.
Clearly, binW is the width of the bar and remember the left-top corner is the origin point.
So if we want the histogram show in our normal way the coodinate of the rectangle should
be calculated like what is showed in the code.
I drew the following figure to explain the coordinate:

V. rectangle
1 void r e c t a n g l e ( InputOutputArray img , P o i n t
pt1 , P o i n t pt2 , c o n s t S c a l a r& c o l o r ,
i n t t h i c k n e s s =1 , i n t l i n e T y p e=LINE_8 ,
i n t s h i f t =0 )
2 void r e c t a n g l e ( Mat& img , Rect rec , c o n s t
S c a l a r& c o l o r , i n t t h i c k n e s s =1 , i n t
l i n e T y p e=LINE_8 , i n t s h i f t =0 )
1 r e c t a n g l e ( histImage , P o i n t ( i binW ,
h i s t I m a g e . rows ) ,
2
P o i n t ( ( i +1) binW , h i s t I m a g e .
rows cvRound ( h i s t . at <
f l o a t >( i ) ) ) ,

Figure 1: rectangle coordinate explanation

You might also like