【发布时间】:2014-01-07 07:12:49
【问题描述】:
以下 C++ 代码(原样)来自http://rosettacode.org/wiki/Entropy。有错误 - 任何人都可以纠正它们吗?
#include <string>
#include <map>
#include <iostream>
#include <algorithm>
#include <cmath>
double log2( double number ) {
return log( number ) / log( 2 ) ;
}
int main( int argc , char *argv[ ] ) {
std::string teststring( argv[ 1 ] ) ;
std::map<char , int> frequencies ;
for ( char c : teststring )
frequencies[ c ] ++ ;
int numlen = teststring.length( ) ;
double infocontent = 0 ;
for ( std::pair<char , int> p : frequencies ) {
double freq = static_cast<double>( p.second ) / numlen ;
infocontent += freq * log2( freq ) ;
}
infocontent *= -1 ;
std::cout << "The information content of " << teststring
<< " is " << infocontent << " !\n" ;
return 0 ;
}
第一个错误似乎通过以下方式修复:
double log2( double n )
{
// log(n)/log(2) is log2.
return log( n ) / log( 2. );
}
我不确定他们想说什么:
for ( char c : teststring )
【问题讨论】:
-
for循环称为“for each”循环 -
您能否解释一下您想要哪种图像以及您的标准/措施/...? L2 够用吗?如果在生成中是 1D,那么原始信号是什么?通常,规范是不够的。请提供其他条件。 - - 我认为您的熵函数不足以研究图像的熵。它大约是一维信号。我很想听听您为什么认为您的 1D 方法及其对字符的扩展可能足以用于图像。我认为没有考虑措施和位基数。你的来源是有限的。相关cs.stackexchange.com/q/4935/10350,但仍然是一个存根。
-
您在图像中使用香农熵的目标是什么?