山东大学数字图像处理实验(五) 计算机学院(2020级)

本实验为计算机科学与技术学院计算机专业大四上限选课,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;
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值