图像直方图就是用表示数字图像中亮度分布的直方图,标绘了图像中每个亮度值的像素数,可以借助观察该直方图了解如何调整亮度分布。
直方图意义如下:
.直方图是图像中像素强度分布的图形表达式
.它统计了每一个强度值所具有的像素个数
绘制H-S直方图示例:
#include"stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include<opencv2/photo/photo.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("E:\\pictures\\For_Project\\Opencv_Test\\beauty.jpg", -1);
if (!image.data)cout << "Please check the fold exit or not!" << endl;
Mat hsvImage;
//转换为HSV颜色模型
cvtColor(image, hsvImage, COLOR_BGR2HSV);
//将色调量化为30个等级
int hueBinNum = 30;
//将饱和度量化为32个等级
int saturationBinNum = 32;
int histSize[] = { hueBinNum,saturationBinNum };
//色调变化范围为0-179
float hueRanges[] = { 0,180 };
//饱和度变化范围为0-255
float saturationRanges[] = { 0,256 };
const float*ranges[] = { hueRanges,saturationRanges };
MatND dstHist;
//计算第0和第1通道的直方图
int channels[] = { 0,1 };
//进行直方图的计算
calcHist(&hsvImage, 1, channels, Mat(), dstHist, 2, histSize, ranges, true, false);
double maxValue = 0;
//查找数组和子数组的全局最小值和最大值存入maxValue中
minMaxLoc(dstHist, 0, &maxValue, 0, 0);
int scale = 10;
Mat histImage = Mat::zeros(saturationBinNum*scale, hueBinNum * 10, CV_8UC3);
//双重循环进行绘制
for (int hue = 0;hue < hueBinNum;hue++) {
for (int saturation = 0;saturation < saturationBinNum;saturation++) {
float binValue = dstHist.at<float>(hue, saturation);
int intensity = cvRound(binValue * 255 / maxValue);
rectangle(histImage, Point(hue*scale, saturation*scale), Point((hue + 1)*scale - 1,
(saturation + 1)*scale - 1), Scalar::all(intensity), FILLED);
}
}
imshow("原图", image);
imshow("H_S直方图", histImage);
while((char)waitKey(0)!='q'){}
return 0;
}
绘制一维直方图示例:
#include"stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include<opencv2/photo/photo.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("E:\\pictures\\For_Project\\Opencv_Test\\beauty.jpg", 0);
if (!image.data)cout << "Please check the fold exit or not!" << endl;
imshow("原图", image);
MatND dstHist;
int dims = 1;
float hranges[] = { 0,255 };
const float*ranges[] = { hranges };
int size = 256;
int channels = 0;
calcHist(&image, 1, &channels, Mat(), dstHist, dims, &size, ranges);
int scale = 1;
Mat dstImage(size*scale, size, CV_8U, Scalar(0));
double minValue = 0;
double maxValue = 0;
minMaxLoc(dstHist, &minValue, &maxValue, 0, 0);
int hpt = saturate_cast<int>(0.9*size);
for (int i = 0;i < 256;i++) {
float binValue = dstHist.at<float>(i);
int realValue = saturate_cast<int>(binValue*hpt / maxValue);
rectangle(dstImage, Point(i*scale, size - 1), Point((i + 1)*scale - 1, size - realValue), Scalar(255));
}
imshow("一维直方图", dstImage);
while((char)waitKey(0)!='q'){}
return 0;
}
绘制真彩色RGB图像直方图示例:
#include"stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include<opencv2/photo/photo.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("E:\\pictures\\For_Project\\Opencv_Test\\tree.jpg", 1);
if (!image.data)cout << "Please check the fold exit or not!" << endl;
imshow("原图", image);
//参数准备
int bins = 256;
int hist_size[] = { bins };
float range[] = { 0,256 };
const float*ranges[] = { range };
MatND redHist, grayHist, blueHist;
//计算红色分量直方图
int channels_r[] = { 0 };
calcHist(&image, 1, channels_r, Mat(), redHist, 1, hist_size, ranges, true, false);
//计算绿色分量直方图
int channels_g[] = { 1 };
calcHist(&image, 1, channels_g, Mat(), grayHist, 1, hist_size, ranges, true, false);
//计算蓝色分量直方图
int channels_b[] = { 2 };
calcHist(&image, 1, channels_b, Mat(), blueHist, 1, hist_size, ranges, true, false);
//参数准备
double maxValue_red, maxValue_green, maxValue_blue;
minMaxLoc(redHist, 0, &maxValue_red, 0, 0);
minMaxLoc(grayHist, 0, &maxValue_green, 0, 0);
minMaxLoc(blueHist, 0, &maxValue_blue, 0, 0);
int scale = 1;
int histHeight = 256;
Mat histImage = Mat::zeros(histHeight, bins * 3, CV_8UC3);
//绘制
for (int i = 0;i < bins;i++) {
float binValue_red = redHist.at<float>(i);
float binValue_green = grayHist.at<float>(i);
float binValue_blue = blueHist.at<float>(i);
//分别得到要绘制的RGB三分量的高度
int intensity_red = cvRound(binValue_red*histHeight / maxValue_red);
int intensity_green = cvRound(binValue_green*histHeight / maxValue_green);
int intensity_blue = cvRound(binValue_blue*histHeight / maxValue_blue);
//分别绘制三分量的直方图
rectangle(histImage, Point(i*scale, histHeight - 1), Point((i + 1)*scale - 1,
histHeight - intensity_red), Scalar(0, 0, 255));
rectangle(histImage, Point((i+bins)*scale, histHeight - 1), Point((i + bins + 1)*scale - 1,
histHeight - intensity_green), Scalar(0, 255, 0));
rectangle(histImage, Point((i+2*bins)*scale, histHeight - 1), Point((i + 2*bins + 1)*scale - 1,
histHeight - intensity_blue), Scalar(255, 0, 0));
}
imshow("RGB直方图", histImage);
while((char)waitKey(0)!='q'){}
return 0;
}