c++代码基于cvui绘制
#include<iostream>
#include <opencv2\opencv.hpp>
#include<opencv.hpp>
#include"cvui.h"
#define CVUI_IMPLEMENTATION
using namespace cv;
using namespace std;
using namespace cvui;
class histogram
{
private:
Mat src_image=Mat::zeros(5,5, CV_8UC3);
vector <int>HistogramValue_num = {1};
vector<double>histogramVaule_chance = {1};
public:
histogram( Mat& A) {
src_image = A;
//cvtColor(src_image, src_image,COLOR_BGR2GRAY);
HistogramValue_num.assign(256, 0);
histogramVaule_chance.assign(256, 0);
histogram_calc();
histogramVaule_chance_calc();
}
histogram(string text) {
src_image = imread(text, IMREAD_GRAYSCALE);
HistogramValue_num.assign(256,0);
histogramVaule_chance.assign(256,0);
histogram_calc();
histogramVaule_chance_calc();
}
//灰度直方图计算像素值出现次数
void histogram_calc()
{
for (int i = 0; i < src_image.rows; i++)
{
uchar* src_data = src_image.ptr<uchar>(i);
for (int j = 0; j < src_image.cols; j++)
{
HistogramValue_num[*(src_data+j)]++;
}
}
cout << "1" << endl;
}
//计算灰度值概率
void histogramVaule_chance_calc()
{
int sum = 0;
for (int i = 0; i < 256; i++)
{
histogramVaule_chance[i] = HistogramValue_num[i] *1.0/(src_image.rows*src_image.cols) ;
sum += histogramVaule_chance[i];
}
}
//绘制直方图
Mat draw_histogram()
{
Mat behind_image = Mat::zeros(800,800, CV_8UC3);
Point o = Point(100, 500);
Point x = Point(800,500);
Point y = Point(100, 50);
line(behind_image, o, x, Scalar(255, 255, 255), 2, 8, 0);
line(behind_image, o, y, Scalar(255, 255, 255), 2, 8, 0);
Point num_value[256];
auto tmp = max_element(histogramVaule_chance.begin(), histogramVaule_chance.end());
for (int i = 0; i < histogramVaule_chance.size(); i++)
{
num_value[i].x = i * 2+ 100;
num_value[i].y = 500- (histogramVaule_chance[i] *450/(*tmp));
if ((i ) % 25== 0)
{
putText(behind_image, format("%d",i), Point(num_value[i].x, 520), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));
}
if ((i)%255==0)
{
putText(behind_image, format("%d", 1), Point(100, 50), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255));
}
}
for (int i = 1; i < 255; i++)
{
line(behind_image, num_value[i -1],num_value[i]/* Point(num_value[i].x,500)*/, Scalar(36, 163, 56), 2);
}
return behind_image;
}
double average_value()//均值
{
double sum = 0;
for (int i = 0; i < 255; i++)
{
sum += i * histogramVaule_chance[i];
}
return sum;
}
double two_matrix()//二阶矩阵
{
double sum = 0;
for (int i = 0; i < 255; i++)
{
sum += i*i * histogramVaule_chance[i];
}
return sum;
}
double variance()//方差
{
double sum = 0;
for (int i = 0; i < 255; i++)
{
sum+= pow((i - average_value()), 2) * histogramVaule_chance[i];
}
return sum;
}
double Skewness()//偏度
{
double sum = 0;
for (int i = 0; i < 255; i++)
{
sum += pow((i - average_value()), 3) * histogramVaule_chance[i]/pow(average_value(),3);
}
return sum;
}
double Kurtosis()//峰度
{
double sum = 0;
for (int i = 0; i < 255; i++)
{
sum += pow((i - average_value()), 4) * histogramVaule_chance[i] / pow(average_value(), 4);
}
return sum;
}
double energy()//能量
{
double sum = 0;
for (int i = 0; i < 255; i++)
{
sum += pow(histogramVaule_chance[i], 2);
}
return sum;
}
double entropy()//熵
{
double sum = 0;
for (int i = 0; i < 255; i++)
{
sum += histogramVaule_chance[i] * log2(histogramVaule_chance[i])*-1;
}
return sum;
}
void draw_num_picture()
{
Mat dst_image = draw_histogram();
init("灰度直方图");
printf(dst_image, 110, 550, 0.6, 0xff0000, "averagevalue:%.0lf",average_value());
printf(dst_image, 110, 600, 0.6, 0xff0000, "two_matrix:%.4lf", average_value());
printf(dst_image, 110, 650, 0.6, 0xff0000, "variance:%4lf", variance());
printf(dst_image, 400, 550, 0.6, 0xff0000, " Skewness:%.4lf", Skewness());
printf(dst_image, 410, 600, 0.6, 0xff0000, "Kurtosis:%.4lf", Kurtosis());
printf(dst_image, 410, 650, 0.6, 0xff0000, "energy:%.4lf", energy());
printf(dst_image, 410, 700, 0.6, 0xff0000, "entropy:%.4lf", entropy());
update();
imshow("灰度直方图", dst_image);
}
};
int main()
{
Mat src_image = imread("E://1.png");
histogram a(src_image);
Mat dst_image = a.draw_histogram();
a.draw_num_picture();
waitKey();
return 0;
}
程序截图: