栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 点锐度 灰度方差 峰值信噪比

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 点锐度 灰度方差 峰值信噪比

#include 
#include 
#include 
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"


using namespace cv;
using namespace std;

double PVA_Grad(Mat &img);
double GrayAverage(Mat &img);
double GrayVariance(Mat &img);
double getPSNR(Mat &src, Mat &res);
int main()
{
	Mat imgIn, imgOut;
	imgIn = imread("D:\user\1.jpg", IMREAD_GRAYSCALE);
	imgOut = imread("D:\user\4.jpg", IMREAD_GRAYSCALE);

	if (imgIn.empty()) 
	{

		return -1;
	}

	if (imgOut.empty())
	{

		return -1;
	}


	double EVA1, EVA2;
	EVA1 = PVA_Grad(imgIn);
	EVA2 = PVA_Grad(imgOut);
	cout << "原始图像的点锐度: " << EVA1 << endl;
	cout << "增强后图像的点锐度: " << EVA2 << endl;
	
	double AVE1, AVE2;
	AVE1 = GrayAverage(imgIn);
	AVE2 = GrayAverage(imgOut);
	cout << "原始图像的灰度平均值: " << AVE1 << endl;
	cout << "增强后图像的灰度平均值: " << AVE2 << endl;

	double VAR1, VAR2;
	VAR1 = GrayVariance(imgIn);
	VAR2 = GrayVariance(imgOut);
	cout << "原始图像的灰度方差: " << VAR1 << endl;
	cout << "增强后图像的灰度方差: " << VAR2 << endl;

	double PSNR;
	PSNR = getPSNR(imgIn, imgOut);
	cout << "图像的峰值信噪比: " << PSNR << endl;

	waitKey(0);
	system("pause");
	return 0;
}

// 点锐度
double PVA_Grad(Mat &img)
{
	double P = 0;
	for (int i = 1; i < img.rows - 1; i++)
	{
		//定义行指针
		uchar *current_ptr = (uchar*)img.data + i * img.cols;
		uchar *pre_ptr = (uchar*)img.data + (i - 1)*img.cols;
		uchar *next_ptr = (uchar*)img.data + (i + 1)*img.cols;
		for (int j = 1; j < img.cols - 1; j++)
		{

			
			// wj,公式是先取绝对值再求和,原程序得到的值会小很多
			P += abs(pre_ptr[j - 1] - current_ptr[j])*0.7 + abs(pre_ptr[j] - current_ptr[j] )+ abs(pre_ptr[j + 1] - current_ptr[j])*0.7
				+ abs(current_ptr[j - 1] - current_ptr[j]) + abs(current_ptr[j + 1] - current_ptr[j])
				+ abs(next_ptr[j - 1] - current_ptr[j])*0.7 + abs(next_ptr[j] - current_ptr[j]) + abs(next_ptr[j + 1] - current_ptr[j])*0.7;

		}
	}
	return P / (img.cols - 2) / (img.rows - 2);
}

//灰度平均值
double GrayAverage(Mat &img)
{
	double U = 0;
	for (int i = 0; i < img.rows; i++)
	{
		//定义行指针
		uchar* ptr = (uchar*)img.data + i *img.cols;
		for (int j = 0; j 
			U += ptr[j];
			
		}
	} 
	return U / (img.cols ) / (img.rows );
}
	

//灰度方差
double GrayVariance(Mat &img)
{
	double aver = GrayAverage(img);
	double var = 0;
	for (int i = 0; i < img.rows; i++)
	{
		//定义行指针
		uchar* ptr = (uchar*)img.data + i *img.cols;
		for (int j = 0; j 
			var += (ptr[j]-aver)*(ptr[j] - aver);

		}
	}
	return  var / (img.cols) / (img.rows);
}

// 峰值信噪比
double getPSNR( Mat &src,  Mat &res)
{
	
	
		Mat s1;
		absdiff(src, res, s1);       // |I1 - I2|AbsDiff函数是 OpenCV 中计算两个数组差的绝对值的函数
		s1.convertTo(s1, CV_32F);  // 这里我们使用的CV_32F来计算,因为8位无符号char是不能进行平方计算
		s1 = s1.mul(s1);           // |I1 - I2|^2
		
		Scalar s = sum(s1);         //对每一个通道进行加和

		double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels

		if (sse <= 1e-10) // 对于非常小的值我们将约等于0
			return 0;
		else
		{

			double MSE = sse / (src.cols) / (src.rows); //均方根误差
			int MAX = 255;		// 图像颜色的最大数值
			double psnr = 10.0*log10((MAX * MAX) / MSE);
			return psnr;//返回PSNR
		}
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号