// calculate entropy of an image
double Entropy(Mat img)
{
// 将输入的矩阵为图像
double temp[256];
// 清零
for(int i=0;i<256;i++)
{
temp[i] = 0.0;
}
// 计算每个像素的累积值
for(int m=0;m
{// 有效访问行列的方式
const uchar* t = img.ptr(m);
for(int n=0;n
{
int i = t[n];
temp[i] = temp[i]+1;
}
}
// 计算每个像素的概率
for(int i=0;i<256;i++)
{
temp[i] = temp[i]/(img.rows*img.cols);
}
double result = 0;
// 根据定义计算图像熵
for(int i =0;i<256;i++)
{
if(temp[i]==0.0)
result = result;
else
result = result-temp[i]*(log(temp[i])/log(2.0));
}
return result;
}
本文介绍如何使用OpenCV库计算Mat矩阵图像的熵,通过统计像素频率,计算每个像素的概率,并根据信息论定义求得图像的不确定性。适合理解图像特征和信息论在计算机视觉中的应用。
7675

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



