matlab获取直方图熵,matlab – 在opencv中查找熵

我需要在matlab中使用像entropyfilt()这样的函数,它在opencv中不存在.

在matlab中,J = entropyfilt(I)返回数组J,其中每个输出像素包含输入图像I中相应像素周围的9×9邻域的熵值.

我写了一个函数在c中实现它,foreach像素得到它的熵如下:

>使用cvCalHist并将mask参数适当设置为获取图像ROI(这是一个9 * 9的矩形).

>标准化直方图,使其总和等于1.

>使用(香农)熵的公式.

我列出下面的C代码:

GetLocalEntroyImage( const IplImage*gray_src,IplImage*entopy_image){

int hist_size[]={256};

float gray_range[]={0,255};

float* ranges[] = { gray_range};

CvHistogram * hist = cvCreateHist( 1, hist_size, CV_HIST_SPARSE, ranges,1);

for(int i=0;i

for(int j=0;j

//calculate entropy for pixel(i,j)

//1.set roi rect(9*9),handle edge pixel

CvRect roi;

int threshold=Max(0,i-4);

roi.x=threshold;

threshold=Max(0,j-4);

roi.y=threshold;

roi.width=(i-Max(0,i-4))+1+(Min(gray_src->width-1,i+4)-i);

roi.height=(j-Max(0,j-4))+1+(Min(gray_src->height-1,j+4)-j);

cvSetImageROI(const_cast(gray_src),roi);

IplImage*gray_src_non_const=const_cast(gray_src);

//2.calHist,here I chose CV_HIST_SPARSE to speed up

cvCalcHist( &gray_src_non_const, hist, 0, 0 );*/

cvNormalizeHist(hist,1.0);

float total=0;

float entroy=0;

//3.get entroy

CvSparseMatIterator it;

for(CvSparseNode*node=cvInitSparseMatIterator((CvSparseMat*)hist- >bins,&it);node!=0;node=cvGetNextSparseNode(&it)){

float gray_frequency=*(float*)CV_NODE_VAL((CvSparseMat*)hist->bins,node);

entroy=entroy-gray_frequency*(log(gray_frequency)/log(2.0f));//*(log(gray_frequency)/log(2.0))

}

((float*)(local_entroy_image->imageData + j*local_entroy_image->widthStep))[i]=entroy;

cvReleaseHist(&hist);

}

}

cvResetImageROI(const_cast(gray_src));

}

但是,代码太慢了.我在600 * 1200图像中测试它并且花费120秒,而在matlab中的entroyfilt只花费5秒.

有谁知道如何加快它或知道任何其他良好的实施?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值