赞
踩
- #include<opencv2/core.hpp>
- #include<opencv2/imgcodecs.hpp>
- #include<opencv2/highgui.hpp>
- #include<opencv2/imgproc.hpp>
- #include <iostream>
- using namespace cv;
- using namespace std;
-
- //@para gray:需要统计的图 Hist:用于存放统计数据
- void GetHist(Mat gray,Mat &Hist) //统计8Bit量化图像的灰度直方图
- {
- const int channels[1] = { 0 }; //通道索引
- float inRanges[2] = { 0,255 }; //像素范围
- const float* ranges[1] = {inRanges};//像素灰度级范围
- const int bins[1] = { 256 }; //直方图的维度
- calcHist(&gray, 1, channels,Mat(), Hist,1, bins, ranges);
- }
-
- void ShowHist(Mat &Hist)
- {
- //准备绘制直方图
- int hist_w = 512;
- int hist_h = 400;
- int width = 2;
- Mat histImage = Mat::zeros(hist_h,hist_w,CV_8UC3); //准备histImage为全黑背景色
- for (int i = 0; i < Hist.rows; i++)
- {
- rectangle(histImage,Point(width*(i),hist_h-1),Point(width*(i+1),hist_h-cvRound(Hist.at<float>(i)/100)),
- Scalar(255,255,255),-1);
- //cout << "像素值为" << i << "的个数:" << Hist.at<float>(i) << endl;
- }
- namedWindow("histImage", WINDOW_AUTOSIZE);
- imshow("histImage", histImage);
- //waitKey(0);
- }
-
- //获得
- void GetHistList(const Mat src,float *bin,int num)
- {
- //遍历所有的像素值
-
- for (int r=0;r<src.rows;r++)
- {
- for (int c=0;c<src.cols;c++)
- {
- int index = src.at<uchar>(r,c);
- bin[index]++; //bin为灰度级像素数的统计数组
- }
- }
- //归一化操作
- for (int i=0;i<num;i++)
- {
- bin[i]=bin[i]/src.rows/src.cols;
- }
- //累计操作
- for (int i=0;i<num;i++)
- {
- if (i == 0)
- {
- bin[i] = bin[i];
- continue;
- }
- bin[i] = bin[i] + bin[i - 1];
- }
- for (int i = 0; i < num; i++)
- {
- cout << bin[i] << endl;
- }
- }
-
- //
- /*直方图均衡化操作
- 参数: Mat src:原图
- float* bin:src灰度概率统计的累计和数组
- int num:bin数组的大小
- Mat &des:直方图均衡化后的图像
- */
- void MyHisteq(const Mat src,float *bin,int num, Mat& des)
- {
- //首先,先为des开辟src相同大小的空间
- des.create(src.rows,src.cols,CV_8UC1);
- //针对每个像素值计算直方图均衡化的结果
- for (int r=0;r<src.rows;r++)
- {
- for (int c=0;c<src.cols;c++)
- {
- int index=src.at<uchar>(r,c);
- des.at<uchar>(r, c) = int(255 * bin[index]);
- }
- }
- }
- int main(int argc,char *argv)
- {
- Mat src,gray,hist1; //hist用于统计gray的直方图
- src=imread("dark.jpg");
- cvtColor(src,gray,CV_BGR2GRAY);
-
- GetHist(gray,hist1);//获得直方图
- ShowHist(hist1);
- namedWindow("gray");
- imshow("gray",gray);
-
- //获得灰度级统计矩阵
- float bin[256] = {0};
- int num = sizeof(bin) / sizeof(bin[0]);
- GetHistList(gray,bin,num); //bin存放的是灰度值概率的累计和
- Mat my_hist,hist2; //my_hist用于存放直方图均衡化的图像
- MyHisteq(gray, bin, num, my_hist);
- namedWindow("my_hist");
- imshow("my_hist",my_hist);
- GetHist(my_hist, hist2);
- ShowHist(hist2);
- waitKey(0);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。