//
// Created by itheima on 6/12/19.
//
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(){
// 读取图片
Mat img = imread("../assets/lena.jpg",IMREAD_COLOR);
imshow("src",img);
// 单通道处理数据
vector<Mat> bgr_channels;
// 颜色分割
split(img,bgr_channels);
int histSize=256;
float range[] = {0,256};
const float * histRange = {range};
// 用于说明bin宽度是否相等
// bool uniform=true;
// 当计算多通道直方图的时候,是否进行累加
// bool accumulate=false;
Mat b_hist,g_hist,r_hist;
calcHist(&bgr_channels[0],1,0,Mat(),b_hist,1,&histSize,&histRange);
calcHist(&bgr_channels[1],1,0,Mat(),g_hist,1,&histSize,&histRange);
calcHist(&bgr_channels[2],1,0,Mat(),r_hist,1,&histSize,&histRange);
int histsize = 256;
// 创建一个灰色图像
int hist_w = 512;
int hist_h = 400;
int bin_w = cvRound(512.0/histSize);
//进行归一化处理 最小值 最大值 模式
normalize(b_hist,b_hist,0,hist_h,NORM_MINMAX);
cout<<"归一化之后:"<<b_hist<<endl;
Mat histImage(hist_h,hist_w,CV_8UC3,Scalar(0,0,0));
// 绘制图像
for(int i=0;i<histsize;i++){
line(histImage,Point(bin_w*i,hist_h),Point(bin_w*(i),hist_h-b_hist.at<float>(i-1)),Scalar(255,0,0),2,LINE_AA);
}
imshow("histgrom",histImage);
waitKey(0);
destroyAllWindows();
}