opencv中,怎么计算彩色图像的直方图opencv calchist函数

图像(10)
calcHist函数的channels参数和narrays以及dims共同来确定用于计算直方图的图像;
首先dims是最终的直方图维数,narrays指出了arrays数组中图像的个数,其中每一幅图像都可以是任意通道的【只要最终dims不超过32即可】
Channels为图像通道数,rgb为三个通道也就是2(012),
如果channels参数为0,则narrays和dims必须相等,否则弹出assert,此时计算直方图的时候取数组中每幅图像的第0通道。
当channels不是0的时候,用于计算直方图的图像是arrays中由channels指定的通道的图像,channels与arrays中的图像的对应关系,如channels的参数说明的,将arrays中的图像从第0幅开始按照通道摊开排列起来,然后channels中的指定的用于计算直方图的就是这些摊开的通道;
意思是本来index=r*256*256+g*256+b
顺序为rgb(opencv为bgr,所以应该为2 1 0)
假设有arrays中只有一幅三通道的图像image,那么narrays应该为1,如果是想计算3维直方图【最大也只能是3维的】,想将image的通道2作为第一维,通道0作为第二维,通道1作为第三维,则可以将channels设置为channesl={2,0,1};这样calcHist函数计算时就按照这个顺序来统计直方图。
可以看出channels不为0时narrays可以和dims不相等,只要保证arrays中至少有channels指定的通道就可以。
Opencv 计算直方图主要在函数calcHist_8u里面,而其中又有一个重要的函数
calcHistLookupTables_8u()。他会计算出_tab[],这个数组表示每个数应该处于的位置index。比如1 2 3 4 5 6 ;均匀3个size的直方图,那么1 2的index=0,3 4的index=1;5 6的index=2;直接通过hist[index[image[I,j]]]即可得到直方图,大大的加快了速度,
对于多维情况同样如此,二维计算方法应该是ima
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:16176次
排名:千里之外
原创:60篇
转载:40篇
(3)(5)(14)(9)(11)(2)(1)(11)(3)(10)(2)(4)(1)(2)(7)(16)彩色图像的直方图均衡化--基于OpenCV中EqualizeHist_Demo实现
彩色图像的直方图均衡化--基于OpenCV中EqualizeHist_Demo实现
[摘要:本文给出基于彩色图象曲圆图平衡化的OpenCV代码取效果示例! 详细内容包括: 1. 灰度图象曲圆图平衡化 2. 对RGB三通讲各自平衡化后,再组开输出效果 3. RGB图象转化为HSI,YUV,YCbCr色彩]

本文给出基于彩色图像直方图均衡化的OpenCV代码与结果示例!
具体内容包含:
1. 灰度图像直方图均衡化
2. 对RGB三通道各自均衡化后,再组合输出结果
3. RGB图像转化为HSI,YUV,YCbCr颜色空间后,对亮度通道进行均衡化运算后再转回RGB空间
view plaincopyprint?
&&&&&&&&#include&&opencv2/imgcodecs.hpp&&&#include&&opencv2/highgui/highgui.hpp&&&#include&&opencv2/imgproc/imgproc.hpp&&&#include&&iostream&&&#include&&stdio.h&&&&&using&namespace&&&using&namespace&&&&&&&Mat&equalizeChannelHist(const&Mat&&&inputImage)&&{&&&&&&if(&inputImage.channels()&&=&3&)&&&&&&{&&&&&&&&&&vector&Mat&&&&&&&&&&&&split(inputImage,&channels);&&&&&&&&&&&&Mat&B,G,R;&&&&&&&&&&&&equalizeHist(&channels[0],&B&);&&&&&&&&&&equalizeHist(&channels[1],&G&);&&&&&&&&&&equalizeHist(&channels[2],&R&);&&&&&&&&&&&&vector&Mat&&&&&&&&&&&&combined.push_back(B);&&&&&&&&&&combined.push_back(G);&&&&&&&&&&combined.push_back(R);&&&&&&&&&&&&Mat&&&&&&&&&&&merge(combined,&result);&&&&&&&&&&&&return&&&&&&&}&&&&&&&&return&Mat();&&}&&&&Mat&equalizeIntensityHist(const&Mat&&&inputImage)&&{&&&&&&if(inputImage.channels()&&=&3)&&&&&&{&&&&&&&&&&Mat&&&&&&&&&&&&&cvtColor(inputImage,&ycrcb,&COLOR_BGR2YCrCb);&&&&&&&&&&&&vector&Mat&&&&&&&&&&&&split(ycrcb,&channels);&&&&&&&&&&&&equalizeHist(channels[0],&channels[0]);&&&&&&&&&&&&Mat&&&&&&&&&&&merge(channels,ycrcb);&&&&&&&&&&&&cvtColor(ycrcb,&result,&COLOR_YCrCb2BGR);&&&&&&&&&&&&return&&&&&&&}&&&&&&&&return&Mat();&&}&&&&void&getGrayImageHistImage(const&Mat&&&src,&Mat&&&histImage)&&{&&&&&&Mat&&&&&&&int&histSize&=&256;&&&&&&&&calcHist(&src,&1,&0,&Mat(),&hist,&1,&&histSize,&0);&&&&&&normalize(hist,&hist,&0,&histImage.rows,&NORM_MINMAX,&CV_32F);&&&&&&&&histImage&=&Scalar::all(255);&&&&&&int&binW&=&cvRound((double)histImage.cols/histSize);&&&&&&&&for(&int&i&=&0;&i&&&histS&i++&)&&&&&&&&&&rectangle(&histImage,&Point(i*binW,&histImage.rows),&&&&&&&&&&Point((i+1)*binW,&histImage.rows&-&cvRound(hist.at&float&(i))),&&&&&&&&&&Scalar::all(0),&-1,&8,&0&);&&}&&&&&&int&main(&int,&char**&argv&)&&{&&&&&&Mat&src,&&&&&&&Mat&intensity_color_&&&&&&Mat&channel_color_&&&&&&&&const&char*&source_gray_window&=&&Source&Gray&Image&;&&&&&&const&char*&equalized_gray_window&=&&Equalized&Gray&Image&;&&&&&&const&char*&source_color_window&=&&Source&Color&Image&;&&&&&&const&char*&equalized_intensity_color_window&=&&Equalized&Intensity&Color&Image&;&&&&&&const&char*&equalized_channels_color_window&=&&Equalized&Channels&Color&Image&;&&&&&&&&&&&&&&src&=&imread(&argv[1],&1&);&&&&&&&&if(&src.empty()&)&&&&&&{&&&&&&&&&&&cout&&&Usage:&./Histogram_Demo&&path_to_image&&&&&&&&&&&&&&return&-1;&&&&&&}&&&&&&&&&&&&&&{&&&&&&&&&&intensity_color_dst&=&equalizeIntensityHist(src);&&&&&&&&&&&&&&&&&&&&namedWindow(&source_color_window,&WINDOW_AUTOSIZE&);&&&&&&&&&&namedWindow(&equalized_intensity_color_window,&WINDOW_AUTOSIZE&);&&&&&&&&&&&&imshow(&source_color_window,&src&);&&&&&&&&&&imshow(&equalized_intensity_color_window,&intensity_color_dst&);&&&&&&}&&&&&&&&&&&&&&{&&&&&&&&&&channel_color_dst&=&equalizeChannelHist(src);&&&&&&&&&&namedWindow(&equalized_channels_color_window,&WINDOW_AUTOSIZE&);&&&&&&&&&&imshow(&equalized_channels_color_window,&channel_color_dst&);&&&&&&}&&&&&&&&&&&&&&{&&&&&&&&&&cvtColor(&src,&src,&COLOR_BGR2GRAY&);&&&&&&&&&&equalizeHist(&src,&dst&);&&&&&&&&&&&&namedWindow(&source_gray_window,&WINDOW_AUTOSIZE&);&&&&&&&&&&namedWindow(&equalized_gray_window,&WINDOW_AUTOSIZE&);&&&&&&&&&&&&imshow(&source_gray_window,&src&);&&&&&&&&&&imshow(&equalized_gray_window,&dst&);&&&&&&&&&&&&&&&&&&&&&&Mat&graySrc_histImage&=&Mat::ones(200,&260,&CV_8U)*255;&&&&&&&&&&getGrayImageHistImage(src,&graySrc_histImage);&&&&&&&&&&imshow(&source&gray&image&histogram&,&graySrc_histImage);&&&&&&&&&&&&&&&&&&&&&&Mat&grayDst_histImage&=&Mat::ones(200,&260,&CV_8U)*255;&&&&&&&&&&getGrayImageHistImage(dst,&grayDst_histImage);&&&&&&&&&&imshow(&Equalized&gray&image&histogram&,&grayDst_histImage);&&&&&&}&&&&&&&&&&&&&&waitKey(0);&&&&&&&&return&0;&&}&&
* @function EqualizeHist_Demo.cpp
* @brief Demo code for equalizeHist function
* @author OpenCV team
#include &opencv2/imgcodecs.hpp& #include &opencv2/highgui/highgui.hpp& #include &opencv2/imgproc/imgproc.hpp& #include &iostream& #include &stdio.h&
// add by frank,
Mat equalizeChannelHist(const Mat & inputImage) {
if( inputImage.channels() &= 3 )
vector&Mat&
split(inputImage, channels);
Mat B,G,R;
equalizeHist( channels[0], B );
equalizeHist( channels[1], G );
equalizeHist( channels[2], R );
vector&Mat&
combined.push_back(B);
combined.push_back(G);
combined.push_back(R);
merge(combined, result);
return Mat(); }
Mat equalizeIntensityHist(const Mat & inputImage) {
if(inputImage.channels() &= 3)
cvtColor(inputImage, ycrcb, COLOR_BGR2YCrCb);
vector&Mat&
split(ycrcb, channels);
equalizeHist(channels[0], channels[0]);
merge(channels,ycrcb);
cvtColor(ycrcb, result, COLOR_YCrCb2BGR);
return Mat(); }
void getGrayImageHistImage(const Mat & src, Mat & histImage) {
int histSize = 256;
calcHist(&src, 1, 0, Mat(), hist, 1, &histSize, 0);
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, CV_32F);
histImage = Scalar::all(255);
int binW = cvRound((double)histImage.cols/histSize);
for( int i = 0; i & histS i++ )
rectangle( histImage, Point(i*binW, histImage.rows),
Point((i+1)*binW, histImage.rows - cvRound(hist.at&float&(i))),
Scalar::all(0), -1, 8, 0 ); }
int main( int, char** argv ) {
Mat intensity_color_
Mat channel_color_
const char* source_gray_window = &Source Gray Image&;
const char* equalized_gray_window = &Equalized Gray Image&;
const char* source_color_window = &Source Color Image&;
const char* equalized_intensity_color_window = &Equalized Intensity Color Image&;
const char* equalized_channels_color_window = &Equalized Channels Color Image&;
/// Load image
src = imread( argv[1], 1 );
if( src.empty() )
cout&&&Usage: ./Histogram_Demo &path_to_image&&&&
return -1;
/// color image intensity equalization
intensity_color_dst = equalizeIntensityHist(src);
namedWindow( source_color_window, WINDOW_AUTOSIZE );
namedWindow( equalized_intensity_color_window, WINDOW_AUTOSIZE );
imshow( source_color_window, src );
imshow( equalized_intensity_color_window, intensity_color_dst );
/// color image each channel equalization
channel_color_dst = equalizeChannelHist(src);
namedWindow( equalized_channels_color_window, WINDOW_AUTOSIZE );
imshow( equalized_channels_color_window, channel_color_dst );
/// gray image equalization
cvtColor( src, src, COLOR_BGR2GRAY );
equalizeHist( src, dst );
namedWindow( source_gray_window, WINDOW_AUTOSIZE );
namedWindow( equalized_gray_window, WINDOW_AUTOSIZE );
imshow( source_gray_window, src );
imshow( equalized_gray_window, dst );
/// get source gray image Histogram
Mat graySrc_histImage = Mat::ones(200, 260, CV_8U)*255;
getGrayImageHistImage(src, graySrc_histImage);
imshow(&source gray image histogram&, graySrc_histImage);
/// get equalized gray image Histogram
Mat grayDst_histImage = Mat::ones(200, 260, CV_8U)*255;
getGrayImageHistImage(dst, grayDst_histImage);
imshow(&Equalized gray image histogram&, grayDst_histImage);
/// Wait until user exits the program
waitKey(0);
return 0; }
执行结果,如下图所示:
&&&&&&&&&& 原灰度图像&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 直方图均衡化增强后图像
&&&&&&&&&&&&&原彩色图像&&&&&&&&&&&&&&&RGB各通道直方图均衡化后图像&&&&&&&&&&&YCbCr 亮度通道Y直方图均衡化后图像
分RGB通道均衡化后图像颜色有失真情况,而亮度通道均衡化结果不会,主要原因:
Histogram equalization is a non-linear process. Channel splitting and equalizing each channel separately is not the proper way for equalization of contrast. Equalization involves Intensity values of the image not the color components. So for
a simple RGB color image, HE should not be applied individually on each channel. Rather, it should be applied such that intensity values are equalized without disturbing the color balance of the image. So, the first step is to convert the color space of the
image from RGB into one of the color space which separates intensity values from color components. Some of these are:
Convert the image from RGB to one of the above mentioned color spaces. YCbCr is preferred as it is designed for digital images.Perform HE of the intensity plane Y. Convert the image back to RGB.
感谢关注 Ithao123精品文库频道,是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!
Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。
Hadoop是一个由Apache基金会所开发的分布式系统基础架构。
用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。
Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上;而且它提供高吞吐量(high throughput)来访问应用程序的数据,适合那些有着超大数据集(large data set)的应用程序。HDFS放宽了(relax)POSIX的要求,可以以流的形式访问(streaming access)文件系统中的数据。
Hadoop的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。
产品设计是互联网产品经理的核心能力,一个好的产品经理一定在产品设计方面有扎实的功底,本专题将从互联网产品设计的几个方面谈谈产品设计
随着国内互联网的发展,产品经理岗位需求大幅增加,在国内,从事产品工作的大部分岗位为产品经理,其实现实中,很多从事产品工作的岗位是不能称为产品经理,主要原因是对产品经理的职责不明确,那产品经理的职责有哪些,本专题将详细介绍产品经理的主要职责
IThao123周刊opencv 图片计算直方图 结果有问题_百度知道
opencv 图片计算直方图 结果有问题
float hranges[2],做项目需要比较两个图片的相似度;iostream&stdafx,纠结了两天了;\ getchar(); ranges[2] = int histSize[3],imgproc/&lt,histScmpresult& calcHist(&img1;&#92.jpg&quot,3,Mat(); ranges[0] = hranges,channels.hpp& histSize[0]=histSize[1]=histSize[2]=16;&core/#include &lt.0.h&quot,RoiHist1,1,3; cout& MatND RoiHist1; double cmpresult.h&;D,均匀分块之后相同的两个块的相似度竟然只有77%,CV_COMP_INTERSECT),结果发现一张像素的图片和自己比较,RoiHist2;highgui/stdio.jpg&quot,相似的像素数只有109万个左右?#include & cmpresult=compareHist(RoiHist1,没有这个结果后面的都不能进行下去;opencv2/opencv2&#47,用的都是最基本的Opencv中自带的计算直方图和比较直方图的函数,1,ranges),RoiHist2; ranges[1] = Mat img2=imread(&quot.hpp& hranges[0] = 0; calcHist(&img2; return 0,1,RoiHist2;\);IMAG0007:\&#92,为什么会出错呢;D;int main(){ int channels[]={0;opencv2&#47,ranges);#include &lt.hpp&gt,histSIMAG13-45\#include &lt:\#include & Mat img1=imread(&quot.0; hranges[1] = 255; const float* ranges[3];#include &using namespace std急求啊,Mat();)
好吧……我找到问题所在了……已经改好了……
提问者采纳
额,居然看完了
提问者评价
其他类似问题
为您推荐:
opencv的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁opencv-直方图计算R,G,B - 博客频道 - CSDN.NET
Share Knowledge
分类:OpenCV
图像的直方图不单单可以表示强度值(即像素值)分布情况,还可以表示图像像素点的梯度,运动方向等信息。
dims:表示要处理的参数数目,可以是强度值,梯度值,方向值等。本文只对像素点的强度值进行计算直方图操作,故dim=1.
bins:每一个dim下亚分割的箱子个数。本文中的bins=16.
range:像素值的测量范围。本文中range=[0,255]
具体的实现过程参考一下代码,及部分注释内容,未注释的部分已经在之前的文章中有所注释。本文下面给出了原图像和程序运行结果图像。
#include &opencv2/highgui/highgui.hpp&
#include &opencv2/imgproc/imgproc.hpp&
#include &iostream&
#include &stdio.h&
int main( int argc, char** argv )
//加载彩色图像
src = imread( argv[1], 1 );
if( !src.data )
{ return -1; }
//分割图像为3个通道即:B, G and R
vector&Mat& bgr_
split( src, bgr_planes );
//创建箱子的数目
int histSize = 256;
//设置范围 ( for B,G,R) )
float range[] = { 0, 256 } ;//不包含上界256
const float* histRange = { range };
//归一化,起始位置直方图清除内容
bool uniform = bool accumulate =
Mat b_hist, g_hist, r_
//计算每个平面的直方图
//&bgr_planes[]原数组,1原数组个数,0只处理一个通道,
//Mat()用于处理原来数组的掩膜,b_hist将要用来存储直方图的Mat对象
//1直方图的空间尺寸,histsize每一维的箱子数目,histrange每一维的变化范围
//uniform和accumulate箱子的大小一样,直方图开始的位置清除内容
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
//画直方图( B, G and R)
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
//归一化结果为 [ 0, histImage.rows ]
//b_hist输入数组,b_hist输出数组,
//0和histImage.rows归一化的两端限制值,
//NORM_MINMAX归一化类型 -1输出和输入类型一样,Mat()可选掩膜
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
//为每个通道画图
for( int i = 1; i & histS i++ )
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at&float&(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(b_hist.at&float&(i)) ),
Scalar( 255, 0, 0), 2, 8, 0
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at&float&(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(g_hist.at&float&(i)) ),
Scalar( 0, 255, 0), 2, 8, 0
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at&float&(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(r_hist.at&float&(i)) ),
Scalar( 0, 0, 255), 2, 8, 0
//显示输出结果
namedWindow(&calcHist Demo&, CV_WINDOW_AUTOSIZE );
imshow(&calcHist Demo&, histImage );
waitKey(0);
tianzhaixing
排名:第3407名
(29)(4)(10)(19)(48)(9)(69)(53)(14)(14)(16)(7)(7)(2)(6)(28)OpenCV 2 学习笔记(15): 绘制图像直方图
图像由许多的像素组成。这些像素的分布和值包含了图像的许多重要的信息。利用这些信息我们可以计算出图像的直方图,并且去改善图片的效果,检测图像的纹理等。下面我们就来看一下怎么得到图像的直方图。
uniform=true, boolaccu-mulate=false)
– Source arrays. They all should have the same depth,CV_8UorCV_32F, and thesame size. Each of them can have an arbitrary number of channels. nimages– Number of source images. channels – List of the dims channels used to compute the histogram. The first ar-ray channels are numerated from 0 to images[0].channels()-1 , the second ar-raychannels are counted from images[0].channels() to images[0].channels() +images[1].channels()-1, and so on.
– 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 sparsedims-dimensional array. dims– Histogram dimensionality that must be positive and not greater thanCV_MAX_DIMS(equal to 32 in the current OpenCV version). histSize– Array of histogram sizes in each dimension. ranges– Array of the dims arrays of the histogram bin boundaries in each dimension.When the histogram is uniform (uniform=true), then for each dimensioniit is&enough to specify the lower (inclusive) boundary L& histSize[i]-1. That is, in case of a&uniform histogram each of ranges[i]is an array of 2 elements. When the histogram is not&uniform (uniform=false), then &each of ranges[i] contains histSize[i]+1 elements: L0;U0 =L1;U1 =L2;:::;UhistSize[i]-2 =LhistSize[i]-1;UhistSize[i]-1. The array&elements, that are not between L0&and UhistSize[i]-1, are not counted in the histogram. 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 singlehistogram from several&sets of arrays, or to update the histogram in time.
累计标识。如果设置,则直方图在开始时不被清零。这个特征保证可以为多个图像计算一个单独的直方图,或者在线更新直方图。
class Histogram
int histSize[1] ; //灰度级数
float hranges[2];//像素最大值和最小值
const float *ranges[1];
int channels[1];//在这里只使用一个信道
Histogram()
histSize[0] = 256;
hranges[0] = 0.0;//最小值初始化为0
hranges[1] = 255.0;//最大值,因为一般图像都是8位
ranges[0] =//灰度值范围
channels[0] = 0;//如果是彩色图像,我们默认检测0信道
void setChannel( int c)
channels[0] =
int getChannel()
return channels[0];
void setRange(float minValue, float maxValue)
hranges[0] = minV
hranges[1] = maxV
float getMinValue()
return hranges[0];
float getMaxValue()
return hranges[1];
void setNBins(int nbins)
histSize[0] =
int getNBins()
return histSize[0];
cv::MatND getHistogram(const cv::Mat &image);
cv::MatND getHisotgramImage(const cv::Mat &image);
现在要做的就是打开图像,然后创建一个Histogram对象,获得直方图。
cv::Mat img,histoImage, thresholdI
img = cv::imread(&E:\\StandardImage\\lena.jpg&);
if(!img.data)
histoImage = h.getHisotgramImage(img);
// Computes the 1D histogram and returns an image of it.
cv::Mat getHistogramImage(const cv::Mat &image){
// Compute histogram first
cv::MatND hist= getHistogram(image);
// Get min and max bin values
double maxVal=0;
double minVal=0;
cv::minMaxLoc(hist, &minVal, &maxVal, 0, 0);
// Image on which to display histogram
cv::Mat histImg(histSize[0], histSize[0],
CV_8U,cv::Scalar(255));
// set highest point at 90% of nbins
int hpt = static_cast&int&(0.9*histSize[0]);
// Draw a vertical line for each bin
for( int h = 0; h & histSize[0]; h++ ) {
float binVal = hist.at&float&(h);
int intensity = static_cast&int&(binVal*hpt/maxVal);
// This function draws a line between 2 points
cv::line(histImg,cv::Point(h,histSize[0]),
cv::Point(h,histSize[0]-intensity),
cv::Scalar::all(0));
return histI
有时候在灰度直方图中,可以看做两类的像素,一类是灰色的,另一类是比较黑的。这两组像素分别表示两类,一类是背景,一类是前景。我们可以通过阈值分割方法把这两类像素分割开来,而阈值就是两峰的交界处。这就是。OpenCV提供了这样一个函数cv::threshold。它的作用是将像素两类并且将图像二值化进行图像分割,我们需要提供一个阈值。让我们来看下图的灰度直方图,在这个灰度直方图中可以看到一个明显的峰值,在下面这张图片中,可以把分界处看做是高峰的起点,大约在160左右。那么使用cv::threshold方法就可以得到分割后的图像。
cv::threshold(image,thresholded,160,255,cv::THRESH_BINARY);
cv::calcHist函数的许多参数都可以以多种形式应用。大多数时候,直方图是一幅有1信道或者3信道的图像。但是,它也允许你指定一个分布在许多图像中的多通道图像。第六个参数指定直方图的维数。例如,如果是1就是一个1维直方图, 在许多OpenCV函数里面,可以指定一个掩码,指定想包含的像素。另外在本函数里还有两个默认的参数,布尔型的参数,一个是指定直方图是否是规范化,默认是。另一个是是否将直方图进行累积,也就是计算多个图片的直方图。关于这个函数的详细信息可以看相关说明。
&&&&&&&& 返回的直方图储存在一个cv::MatND对象中这是一个用来存储N维矩阵的类。有at方法。需要记住的是其中存储的是float的值。
&&&&&&&&但是我们在输入一个彩色图像时,如果单纯的给出一个一维的直方图就不太对了,我们可以选择将图像按信道分开,分别给出每一个信道的直方图。
也可以使用cv::calcHist计算得到一个256三维的cv::MatND,这样算的话需要256*256*256个元素,一般情况下这个矩阵式稀疏矩阵,所以我们选择用稀疏矩阵cv::SparseMat存储。因为元素太多,所以有时候我们也是用前面说过的减少图像位深的方法,如果div选择64,那么元素个数就变成了4*4*4,减少了2^18数量级,当然,这时候的直方图没有使用的意义了。
我们使用第一种方法对lena彩色图处理结果如下:
本分类共有文章29篇,更多信息详见
& 2012 - 2016 &
&All Rights Reserved. &
/*爱悠闲图+*/
var cpro_id = "u1888441";
/*爱悠闲底部960*75*/
var cpro_id = "u1888128";

我要回帖

更多关于 opencv 图像直方图 的文章

 

随机推荐