如何计算字节二元组的香农熵

如何计算字节二元组的香农熵

本文介绍了如何计算字节二元组的香农熵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将图像文件读入了这样的数组

I have read a image file into a array like this

A = imread(fileName);

现在我要计算香农熵. maltab中的Shannon熵实现是字节级熵分析,它认为文件由256个字节级组成.

and now i want to calculate shannon entropy. The shannon entropy implementation found in maltab is a byte level entropy analysis which considers a file to be composed of 256 byte levels.

wentropy(x,'shannon')

但是我需要执行一个bigram熵分析,该分析需要查看一个由65536个级别组成的文件.谁能建议我一个完成此任务的好方法.

But i need to perform a bigram entropy analysis which would need to view a file as consisting of 65536 levels. Could anyone suggest me a good method of accomplishing this.

推荐答案

可以使用以下公式计算随机变量的熵:

The entropy of a random variable can be calculated using the following formula:

p(x)Prob(X=x)的地方.

给出一组n观测值(x1, x2, .... xn)然后,对所有x值的范围计算P(X=x)(在您的情况下,该值将在(0 and 65535)之间,然后求和所有值.这是最简单的方法正在使用hist

Given a set of n observations (x1, x2, .... xn) You then compute P(X=x) for the range all x values (in your case it would be between (0 and 65535) and then sum across all values. The easiest way to do this is using hist

byteLevel = 65536
% count the observations

observationHist = hist(observations, byteLevel);
% convert to a probability
probXVal = observationHist ./ sum(observationHist);

% compute the entropy
entropy = - sum( probXVal .* log2(probXVal) );

有一些值得一试的在文件交换上的实现.

There are several implementations of this on the file exchange that are worth checking out.

注意:您从哪里获得wentropy正在使用256字节级别的信息?我在码头的任何地方都看不到吗?请记住,在Matlab中,彩色图像的像素具有3个通道(R,G,B),每个通道需要8位(或256字节级别?).

Note: where are you getting that wentropy is using 256 byte levels? I don't see that anywhere in the docks? Remember that in Matlab the pixels of a color image have 3 channels (R,G,B) with each channel requiring 8 bits (or 256 byte levels?).

此外,由于每个通道都绑定在[0 256)之间,因此您可以创建从P(R=r,G=g,B=b)P(X=x)的映射,如下所示:

Also because each channel is bound between [0 256) you could create a mapping from P(R=r,G=g,B=b) to P(X=x) as follows:

data = imageData(:,:,1);
data = data + (imgData(:,:,2) * 256);
data = data + (imgData(:,:,3) * 256 * 256);

我相信您可以使用data来计算每个通道独立的图像的总熵.

I believe you can then use data to calculate the total entropy of the image where each channel is independent.

这篇关于如何计算字节二元组的香农熵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:50