本实验为计算机科学与技术学院计算机专业大四上限选课,2023-2024-1年度课程实验,较以往实验内容发生较大变化
本实验使用vs2019,c++语言,需要提前安装opencv,具体方法请自行搜索。
实验5:图像统计特征
实现灰度图的直方图均衡化,并测试对不同输入图像的效果
#include<opencv2\opencv.hpp>
#include<cmath>
#include<iostream>
using namespace cv;
using namespace std;
//灰度图的直方图均衡化
Mat MyequalizeHist(Mat& Source_Image)
{
int Rows = Source_Image.rows;
int Cols = Source_Image.cols;
int Frequency[256];//频率
double Probability[256];//概率
double Cumulative_probability[256];//累积概率
int Result_Pix[256];
for (int i = 0; i < 256; i++)//对每个灰度初始化
{
Frequency[i] = 0;
Probability[i] = 0.0;
Cumulative_probability[i] = 0.0;
Result_Pix[i] = 0;
}
for (int i = 0; i < Rows; i++)//遍历源图像每个像素点,根据每个像素点的灰度更新频率图
for (int j = 0; j < Cols; j++)
Frequency[(int)Source_Image.at<uchar>(i, j)]++;
for (int i = 0; i < 256; i++)//根据频率图得到概率图,累加得到累积概率图
{
Probability[i] = (double)Frequency[i] / (Rows * Cols);
if (i == 0)
Cumulative_probability[0] = Probability[0];
else
Cumulative_probability[i] = Cumulative_probability[i - 1] + Probability[i];
Result_Pix[i] = cvRound((double)Cumulative_probability[i] * 255);//映射并四舍五入取整
}
Mat resultImage(Rows, Cols, Source_Image.type());//结果矩阵输出直方图均匀化后的结果
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
resultImage.at<uchar>(i, j) = Result_Pix[(int)Source_Image.at<uchar>(i, j)];
return resultImage;
}
int main()
{
Mat src = imread("C:/Users/13441/Desktop/数字图像/back3.png");
Mat srcGray;
//转化为灰度图并且显示
cvtColor(src, srcGray, COLOR_BGR2GRAY);
imshow("original_Image", srcGray);
Mat resultImage = MyequalizeHist(srcGray);
imshow("res_Image", resultImage);
waitKey(0);
return 0;
}

•彩色图像的直方图均衡化该如何实现?测试并分析你的想法。
#include<opencv2\opencv.hpp>
#include<cmath>
#include<iostream>
using namespace cv;
using namespace std;
//彩色图像的直方图均衡化
Mat MyequalizeHist(Mat& Source_Image)
{
int Rows = Source_Image.rows;
int Cols = Source_Image.cols;
int Frequency[256];//频率
double Probability[256];//概率
double Cumulative_probability[256];//累积概率
int Result_Pix[256];
Mat resultImage(Rows, Cols, Source_Image.type());//结果矩阵输出直方图均匀化后的结果
for (int k = 0; k < 3; k++)
{
for (int i = 0; i < 256; i++)//对每个灰度初始化
{
Frequency[i] = 0;
Probability[i] = 0.0;
Cumulative_probability[i] = 0.0;
Result_Pix[i] = 0;
}
for (int i = 0; i < Rows; i++)//遍历源图像每个像素点,根据每个像素点的灰度更新频率图
for (int j = 0; j < Cols; j++)
Frequency[(int)Source_Image.at<Vec3b>(i, j)[k]]++;
for (int i = 0; i < 256; i++)//根据频率图得到概率图,累加得到累积概率图
{
Probability[i] = (double)Frequency[i] / (Rows * Cols);
if (i == 0)
Cumulative_probability[0] = Probability[0];
else
Cumulative_probability[i] = Cumulative_probability[i - 1] + Probability[i];
Result_Pix[i] = cvRound((double)Cumulative_probability[i] * 255);//映射并四舍五入取整
}
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
resultImage.at<Vec3b>(i, j)[k] = (uchar)Result_Pix[(int)Source_Image.at<Vec3b>(i, j)[k]];
}
return resultImage;
}
int main()
{
Mat src = imread("C:/Users/13441/Desktop/数字图像/back7.png");
Mat srcGray;
//转化为灰度图并且显示
//cvtColor(src, srcGray, COLOR_BGR2GRAY);
imshow("original_Image", src);
Mat resultImage = MyequalizeHist(src);
imshow("res_Image", resultImage);
waitKey(0);
return 0;
}

1942

被折叠的 条评论
为什么被折叠?



