http://ronny.blog.51cto.com/8801997/1394115
2014-04-11 13:47:27Tags: OpenCV histogram table original works, allow reprint, when reproduced please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://ronny.blog.51cto.com/8801997/1394115, the concept of image histogram
Image histogram is a statistical table that reflects an image pixel distribution, in fact, the horizontal axis represents the type of image pixels, can be grayscale, can also be color. The ordinate represents the total number of pixels in the image for each color value or the percentage of the number of pixels.
Images are made of pixels, because histograms that reflect the distribution of pixels can often be used as an important feature of images. In practical engineering, image histogram has good application in feature extraction, image matching and so on.
Second, using OPENCV to calculate the histogram of the image
OpenCV in the calculation of the image histogram function is calchist, its parameters are more, the following analysis of its interface and usage.
1 |
void
calcHist(
const
Mat* images,
int nimages,
const
int
* channels, InputArray mask, OutputArray hist,
int
dims,
const
int
*histSize,
const
float
** ranges,
bool
uniform=
true
,
bool
accumulate=
false
);
|
Const mat* Images: A pointer to the input image.
int Nimages: The number of images to be computed for the histogram. This function can be used for multi-image histogram, we usually only for a single image, so usually nimages=1.
Const INT* Channels: The channel of the image, it is an array, if it is a grayscale image channels[1]={0}, if it is a color image channels[3]={0,1,2}, if it is only a color image of the 2nd channel histogram, Then channels[1]={1};
Iuputarray Mask: is a mask image used to determine which points to participate in the calculation, the actual application is a good parameter, by default we are set to an empty image, namely: Mat ().
Outarray hist: Calculated histogram
int dims: The dimension of the obtained histogram, the grayscale image is 1 dimensions, the color image is 3 dimensions.
Const int* Histsize: The interval number of the histogram horizontal axis. If it is 10, it divides the horizontal axis into 10 parts and then counts the total pixel points of each interval.
Const float** Ranges: This is a two-dimensional array that is used to indicate the range of each interval.
The following two parameters all have default values, the uniform parameter indicates whether the histogram is equidistant, and the last parameter is related to the display and storage of the histogram under multiple images.
Let's calculate a grayscale histogram of an image, a color histogram, and a custom grayscale distribution map.
Grayscale histogram:
123456789101112 |
int
main()
{
Mat Image=imread(
"../cat.png"
);
cvtColor(Image,Image,CV_BGR2GRAY);
const
int
channels[1]={0};
const
int
histSize[1]={256};
float
hranges[2]={0,255};
const
float
* ranges[1]={hranges};
MatND hist;
calcHist(&Image,1,channels,Mat(),hist,1,histSize,ranges);
return
0;
}
|
Color histogram:
1234567891011 |
int
main()
{
Mat Image=imread(
"../cat.png"
);
const int
channels[3]={0,1,2};
const
int
histSize[3]={256,256,256};
float
hranges[2]={0,255};
const
float
* ranges[3]={hranges,hranges,hranges};
MatND hist;
calcHist(&Image,1,channels,Mat(),hist,3,histSize,ranges);
return
0;
}
|
In the non-uniform histogram, we separately statistic the gray distribution of 0-50,50-80,80-150,150-230,230-255 interval:
123456789101112 |
int
main()
{
Mat Image=imread(
"../cat.png"
);
cvtColor(Image,Image,CV_BGR2GRAY);
const
int
channels[1]={0};
int
histSize[1]={5};
float
hranges[6]={0,50,80,150,230,255};
const float
* ranges[1]={hranges};
MatND hist;
calcHist(&Image,1,channels,Mat(),hist,1,histSize,ranges,
false
);
return
0;
}
|
Three, the display of the histogram
As we can see from the above example, the histogram calculation is actually a multidimensional array, which is not intuitive enough, and we want to be able to represent the relevant data in Excel in the form of a table.
A grayscale histogram is shown by the underscore function below:
123456789101112131415161718 |
Mat getHistImg(
const
MatND& hist)
{
double
maxVal=0;
double
minVal=0;
//找到直方图中的最大值和最小值
minMaxLoc(hist,&minVal,&maxVal,0,0);
int
histSize=hist.rows;
Mat histImg(histSize,histSize,CV_8U,Scalar(255));
// 设置最大峰值为图像高度的90%
int
hpt=
static_cast
<
int
>(0.9*histSize);
for
(
int
h=0;h
{
float
binVal=hist.at<
float
>(h);
int
intensity=
static_cast
<
int
>(binVal*hpt/maxVal);
line(histImg,Point(h,histSize),Point(h,histSize-intensity),Scalar::all(0));
}
return
histImg;
}
|
Four, histogram transformation
Histogram transformation is an important concept in image processing, the image histogram can reflect the image contrast, light and dark degree and so on, so we can use the histogram transform to adjust the image picture.
Histogram transformation in the practical application of the project is very wide, some landscaping photos of the software many tools are on the histogram of the image as an article, if there are readers interested in this aspect of the recommendation: http://www.cnblogs.com/Imageshop/
Here are two simple histogram transformation functions: histogram stretching and histogram equalization.
If the grayscale of the image is displayed on a histogram in a certain interval, then the color of the image is single, and we can extend it to a wider gray scale to give the image a more layered feel.
Transform function: Transforms a grayscale value of an image into another grayscale.
The core of the histogram transformation is the transformation function, S=t (R), R is the gray value before the transformation, S is the transformed gray value, if we want to transform [a, b] interval of gray to the [0,255] range, then the transformation function is: T (r) =255* (r-a)/(B-A).
We create such a transformation function in OPENCV:
123456789101112 |
// 创建一个1*256的矢量
Mat lut(1,256,CV_8U);
for
(
int
i=0;i<256;i++)
{
if
(lut.at<uchar>(i)<imin)
lut.at<uchar>(i)=0;
else
if
(lut.at<uchar>(i)>imax)
lut.at<uchar>(i)=255;
else
lut.at<uchar>(i)=
static_cast
<uchar>(
255.0*(i-imin)/(imax-imin)+0.5);
}
|
Where Imax,imin is the minimum grayscale and maximum grayscale in the image. We can find out from a histogram:
1234567891011 |
int
imax,imin;
for
(imin=0;imin<256;imin++)
{
if
(hist.at<uchar>(imin)>minValue)
break
;
}
for
(imax=255;imax>-1;imax--)
{
if
(hist.at<uchar>(imax)>minValue)
break
;
}
|
Finally, we apply the LUT function in OpenCV and apply the transform to the histogram.
The second parameter, like a lookup table, finds the grayscale in the original image as a table, and then replaces the grayscale value with the corresponding value in the table.
With the above gray-scale stretching example is not difficult to understand the image of the histogram equalization, histogram equalization can make the image gray distribution more evenly, so that the contrast of the image enhancement.
In OpenCV, histogram equalization does not have to construct a transform function like grayscale stretching, it has a direct corresponding function, of course, if you are interested can also try to write a transformation Function equalization transformation principle will be slightly more complex, in OpenCV this series, not too much introduction of digital Image algorithm, I'll have a chance to discuss it later.
123456789 |
int < Code class= "CPP Plain" >main () { mat image=imread ( &NBSP;&NBSP;&NBSP;&NBSP; cvtcolor (image,image,cv_bgr2gray); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; mat result; &NBSP;&NBSP;&NBSP;&NBSP; equalizehist (image,result); &NBSP;&NBSP;&NBSP;&NBSP; return 0; |
OpenCV growth path: Image histogram