OpenCV-统计图像的直方图,绘制直方图

xiaoxiao2021-02-28  5

绘制灰色直方图

函数原型(有三个重载类型): OpenCV3帮助文档

void cv::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 ) void cv::calcHist ( const Mat * images, int nimages, const int * channels, InputArray mask, SparseMat & hist, int dims, const int * histSize, const float ** ranges, bool uniform = true, bool accumulate = false ) void cv::calcHist ( InputArrayOfArrays images, const std::vector< int > & channels, InputArray mask, OutputArray hist, const std::vector< int > & histSize, const std::vector< float > & ranges, bool accumulate = false ) 关于第一个函数原型的详细参数如下: const Mat* images:为输入图像的指针 int nimages:要计算直方图的图像的个数。此函数可以为多图像求直方图,我们通常情况下都只作用于单一图像,所以通常nimages=1 const int* channels:图像的通道,它是一个数组,如果是灰度图像则channels[1]={0};如果是彩色图像则channels[3]={0,1,2};如果是只是求彩色图像第2个通道的直方图,则channels[1]={1} IuputArraymask:是一个遮罩图像用于确定哪些点参与计算,实际应用中是个很好的参数,默认情况我们都设置为一个空图像,即:Mat()。 OutArrayhist:计算得到的直方图int dims:得到的直方图的维数,灰度图像为1维,彩色图像为3维 constint*histSize:直方图横坐标的区间数。如果是10,则它会横坐标分为10份,然后统计每个区间的像素点总和constfloat** ranges:这是一个二维数组,用来指出每个区间的范围后面两个参数都有默认值,

uniform参数表明直方图是否等距,最后一个参数与多图像下直方图的显示与存储有关

Samples

version 1

#include <opencv.hpp> #include <iostream> #include"time.h" #include <vector> #include <map> using namespace std; using namespace cv; int main( ) { Mat srcImage = imread("baboon.jpg"); imshow("【原图】", srcImage); Mat src_gray; cvtColor(srcImage, src_gray, COLOR_BGR2GRAY); imshow("【灰度图】", src_gray); //需要计算图像的哪个通道(bgr空间需要确定计算 b或g或r空间) const int channels[1] = { 0 }; //直方图的每一个维度的 柱条的数目(就是将灰度级分组) int histSize[] = { 256 }; //如果这里写成int histSize = 256; 那么下面调用计算直方图的函数的时候,该变量需要写 &histSize //定义一个变量用来存储 单个维度 的数值的取值范围 float midRanges[] = { 0, 256 }; //确定每个维度的取值范围,就是横坐标的总数 const float *ranges[] = { midRanges }; //输出的结果存储的 空间 ,用MatND类型来存储结果 MatND dstHist; calcHist(&src_gray, 1, channels, Mat(), dstHist, 1, histSize, ranges, true, false); //calcHist 函数调用结束后,dstHist变量中将储存了 直方图的信息 用dstHist的模版函数 at<Type>(i)得到第i个柱条的值 at<Type>(i, j)得到第i个并且第j个柱条的值 //首先先创建一个黑底的图像,为了可以显示彩色,所以该绘制图像是一个8位的3通道图像 Mat drawImage = Mat::zeros(Size(256, 256), CV_8UC3); //一个图像的某个灰度级的像素个数(最多为图像像素总数),可能会超过显示直方图的所定义的图像的尺寸,因此绘制直方图的时候,让直方图最高的地方只有图像高度的90%来显示 //先用minMaxLoc函数来得到计算直方图后的像素的最大个数 double g_dHistMaxValue; minMaxLoc(dstHist, 0, &g_dHistMaxValue, 0, 0); //遍历直方图得到的数据 for (int i = 0; i < 256; i++) { int value = cvRound(256 * 0.9 *(dstHist.at<float>(i) / g_dHistMaxValue)); line(drawImage, Point(i, drawImage.rows - 1), Point(i, drawImage.rows - 1 - value), Scalar(255, 0, 0)); } imshow("【直方图】", drawImage); waitKey(0); return 0; }

version 2

//绘制灰度直方图 int main( ) { Mat src,gray; src=imread("baboon.jpg"); cvtColor(src,gray,CV_RGB2GRAY); int bins = 256; int hist_size[] = {bins}; float range[] = { 0, 256 }; const float* ranges[] = { range}; MatND hist; int channels[] = {0}; calcHist( &gray, 1, channels, Mat(), // do not use mask hist, 1, hist_size, ranges, true, // the histogram is uniform false ); double max_val; minMaxLoc(hist, 0, &max_val, 0, 0); int scale = 2; int hist_height=256; Mat hist_img = Mat::zeros(hist_height,bins*scale, CV_8UC3); for(int i=0;i<bins;i++) { float bin_val = hist.at<float>(i); int intensity = cvRound(bin_val*hist_height/max_val); //要绘制的高度 rectangle(hist_img,Point(i*scale,hist_height-1), Point((i+1)*scale - 1, hist_height - intensity), CV_RGB(255,255,255)); } imshow( "Source", src ); imshow( "Gray Histogram", hist_img ); waitKey(10000000000); return 0; }

可以调直方图间距

我还发现一个问题,如果图片的格式是bmp的,会得到下列的直方图

绘制RGB三色直方图

//绘制RGB三色分量直方图 int main( ) { Mat src; src=imread("baboon.jpg"); int bins = 256; int hist_size[] = {bins}; float range[] = { 0, 256 }; const float* ranges[] = { range}; MatND hist_r,hist_g,hist_b; int channels_r[] = {0}; calcHist( &src, 1, channels_r, Mat(), // do not use mask hist_r, 1, hist_size, ranges, true, // the histogram is uniform false ); int channels_g[] = {1}; calcHist( &src, 1, channels_g, Mat(), // do not use mask hist_g, 1, hist_size, ranges, true, // the histogram is uniform false ); int channels_b[] = {2}; calcHist( &src, 1, channels_b, Mat(), // do not use mask hist_b, 1, hist_size, ranges, true, // the histogram is uniform false ); double max_val_r,max_val_g,max_val_b; minMaxLoc(hist_r, 0, &max_val_r, 0, 0); minMaxLoc(hist_g, 0, &max_val_g, 0, 0); minMaxLoc(hist_b, 0, &max_val_b, 0, 0); int scale = 1; int hist_height=256; Mat hist_img = Mat::zeros(hist_height,bins*3, CV_8UC3); for(int i=0;i<bins;i++) { float bin_val_r = hist_r.at<float>(i); float bin_val_g = hist_g.at<float>(i); float bin_val_b = hist_b.at<float>(i); int intensity_r = cvRound(bin_val_r*hist_height/max_val_r); //要绘制的高度 int intensity_g = cvRound(bin_val_g*hist_height/max_val_g); //要绘制的高度 int intensity_b = cvRound(bin_val_b*hist_height/max_val_b); //要绘制的高度 rectangle(hist_img,Point(i*scale,hist_height-1), Point((i+1)*scale - 1, hist_height - intensity_r), CV_RGB(255,0,0)); rectangle(hist_img,Point((i+bins)*scale,hist_height-1), Point((i+bins+1)*scale - 1, hist_height - intensity_g), CV_RGB(0,255,0)); rectangle(hist_img,Point((i+bins*2)*scale,hist_height-1), Point((i+bins*2+1)*scale - 1, hist_height - intensity_b), CV_RGB(0,0,255)); } imshow( "Source", src ); imshow( "RGB Histogram", hist_img ); waitKey(10000000000); return 0; }

绘制二维直方图

version 1

#include<opencv2/opencv.hpp> #include<iostream> #include<vector> using namespace cv; using namespace std; int main() { Mat srcImage = imread("baboon.jpg"); imshow("【原图】", srcImage); Mat hsvImage; //因为要计算H-S的直方图,所以需要得到一个HSV空间的图像 cvtColor(srcImage, hsvImage, CV_BGR2HSV); imshow("【HSV空间的原图】", hsvImage); //为计算直方图配置变量 //首先是需要计算的图像的通道,就是需要计算图像的哪个通道(bgr空间需要确定计算 b或g货r空间) int channels[] = { 0, 1 }; //然后是配置输出的结果存储的 空间 ,用MatND类型来存储结果 MatND dstHist; //接下来是直方图的每一个维度的 柱条的数目(就是将数值分组,共有多少组) //如果这里写成int histSize = 256; 那么下面调用计算直方图的函数的时候,该变量需要写 &histSize int histSize[] = { 30, 32 }; //最后是确定每个维度的取值范围,就是横坐标的总数 //首先得定义一个变量用来存储 单个维度的 数值的取值范围 float HRanges[] = { 0, 180 }; float SRanges[] = { 0, 256 }; const float *ranges[] = { HRanges, SRanges }; calcHist(&hsvImage, 1, channels, Mat(), dstHist, 2, histSize, ranges, true, false); //calcHist 函数调用结束后,dstHist变量中将储存了 直方图的信息 用dstHist的模版函数 at<Type>(i)得到第i个柱条的值 //at<Type>(i, j)得到第i个并且第j个柱条的值 //开始直观的显示直方图——绘制直方图 //首先先创建一个黑底的图像,为了可以显示彩色,所以该绘制图像是一个8位的3通道图像 Mat drawImage = Mat::zeros(Size(300, 320), CV_8UC3); //因为任何一个图像的某个像素的总个数,都有可能会有很多,会超出所定义的图像的尺寸,针对这种情况,先对个数进行范围的限制 //先用 minMaxLoc函数来得到计算直方图后的像素的最大个数 double g_dHistMaxValue; minMaxLoc(dstHist, 0, &g_dHistMaxValue, 0, 0); //将像素的个数整合到 图像的最大范围内 //遍历直方图得到的数据 for (int i = 0; i < 30; i++) { for (int j = 0; j < 32; j++) { int value = cvRound(dstHist.at<float>(i, j) * 256 / g_dHistMaxValue); rectangle(drawImage, Point(10 * i, j * 10), Point((i + 1) * 10 - 1, (j + 1) * 10 - 1), Scalar(value), -1); } } imshow("【直方图】", drawImage); waitKey(0); return 0; }

version 2

//绘制H-S二维直方图 int main( ) { Mat src,hsv; src=imread("baboon.jpg"); cvtColor(src, hsv, CV_BGR2HSV); // Quantize the hue to 30 levels // and the saturation to 32 levels int hbins = 256, sbins = 180; int histSize[] = {hbins, sbins}; // hue varies from 0 to 179, see cvtColor float hranges[] = { 0, 180 }; // saturation varies from 0 (black-gray-white) to // 255 (pure spectrum color) float sranges[] = { 0, 256 }; const float* ranges[] = { hranges, sranges }; MatND hist; // we compute the histogram from the 0-th and 1-st channels int channels[] = {0, 1}; calcHist( &hsv, 1, channels, Mat(), // do not use mask hist, 2, histSize, ranges, true, // the histogram is uniform false ); double maxVal=0; minMaxLoc(hist, 0, &maxVal, 0, 0); int scale = 2; Mat histImg = Mat::zeros(sbins*scale, hbins*scale, CV_8UC3); for( int h = 0; h < hbins; h++ ) for( int s = 0; s < sbins; s++ ) { float binVal = hist.at<float>(h, s); int intensity = cvRound(binVal*255/maxVal); rectangle( histImg, Point(h*scale, s*scale), Point( (h+1)*scale - 1, (s+1)*scale - 1), Scalar::all(intensity), CV_FILLED ); } namedWindow( "Source", 1 ); imshow( "Source", src ); namedWindow( "H-S Histogram", 1 ); imshow( "H-S Histogram", histImg ); waitKey(10000000000); return 0; }

主要参考博客:

参考博客1:http://blog.csdn.net/u011574296/article/details/70880423

参考博客2http://blog.csdn.net/xiaowei_cqu/article/details/8833799

参考博客3 :http://blog.csdn.net/qq_23880193/article/details/49669265

转载请注明原文地址: https://www.6miu.com/read-450191.html

最新回复(0)