资源简介
原版的Itti代码是Matlab,在本人上传列表里也有,这个是我重新调整结构,用opencv和vs2012写的,速度不快,在2-3秒,这也是由算法本身决定的。
代码片段和文件信息
//Itti视觉注意模型的opencv实现
//代码参照Matlab的代码进行编写,结构上进行了一定得优化
//Matlab代码的下载地址见本人CSDN资源
//http://download.csdn.net/detail/megaarthur/7402441
//2014.5.20——2014.5.25
//by Yin Ruoxuan
#include
#include
#include
#include
#include
#include
#define PI 3.1415926
using namespace std;
//高斯金字塔结构体
typedef struct GaussPyr
{
IplImage *lev[9];
}GaussPyr;
//将金字塔尺寸定义为全局变量
CvSize PyrSize[9] = {NULL};
//初始化金字塔结构体
void initPyr(GaussPyr *p)
{
for(int i = 0; i < 9; i++)
p->lev[i] = cvCreateImage(PyrSize[i] IPL_DEPTH_64F 1);
}
//根据层数,求第i层的尺寸
int downsample(int x int level)
{
if(level-- > 0)
{
if(x%2 == 0)
x = x/2;
else
x = (x+1)/2;
downsample(xlevel);
}
if(level == -1)
return x;
}
//计算并产生一幅图的高斯金字塔
void Gscale(GaussPyr *p IplImage *data int level double sigma)
{
for(int i = 0; i {
if(i == 0)
cvSmooth(data p->lev[0] CV_GAUSSIAN 5 5 sigma 0);
else
{
IplImage *tem = cvCreateImage(PyrSize[i-1] IPL_DEPTH_64F 1);
cvSmooth(p->lev[i-1] tem CV_GAUSSIAN 5 5 sigma 0);
for(int a = 0; a < PyrSize[i].height; a++)
for(int b = 0; b < PyrSize[i].width; b++)
((double *)(p->lev[i]->imageData + a*p->lev[i]->widthStep))[b] = ((double *)(tem->imageData + 2*a*tem->widthStep))[2*b];
}
}
}
//c-s过程中用到的跨尺度相减
void overScaleSub(IplImage *s1 IplImage *s2 IplImage *dst)
{
cvResize(s2 dst CV_INTER_LINEAR);
cvAbsDiff(s1 dst dst);
}
//求图像的局部最大值
void getLocalMaxima(IplImage *scr double thresh double *lm_sum int *lm_num double*lm_avg)
{
*lm_sum = 0.0;
*lm_num = 0;
*lm_avg = 0.0;
int count = 0;
for(int a = 1; a < ((scr->height) - 1); a++)
for(int b = 1; b< ((scr->width) - 1); b++)
{
double val = ((double *)(scr->imageData + a*scr->widthStep))[b];
if((val >= thresh) &&
(val >= ((double *)(scr->imageData + (a-1)*scr->widthStep))[b]) &&
(val >= ((double *)(scr->imageData + (a+1)*scr->widthStep))[b]) &&
(val >= ((double *)(scr->imageData + a*scr->widthStep))[b-1]) &&
(val >= ((double *)(scr->imageData + a*scr->widthStep))[b+1]))
{
if(val == 10) count++;
*lm_sum += val;
(*lm_num) ++;
}
}
if(*lm_num > count)
{
*lm_sum = *lm_sum - 10*count;
*lm_num = *lm_num - count;
if(*lm_num > 0)
*lm_avg = *lm_sum / *lm_num;
else
*lm_avg = 0;
}
else
*lm_avg = 0;
}
//N操作,包括下采样到第5层的尺寸
void N_operation(IplImage *scr IplImage *dst)
{
cvNormalize(scr scr 1.0 0.0 CV_MINMAX NULL);
cvConvertScale(scr scr 10 0);
int lm_num;
double lm_sum;
double lm_avg;
getLocalMaxima(scr 1 &lm_sum &lm_num &lm_avg);
if(lm_num > 0)
cvConvertScale(scr scr (10-lm_avg)*(10-lm_avg) 0);
//下采样,先判断目前的层数
if( scr->height == PyrSize[2].height )//scr在第三层
{
for(int a = 0; a < PyrSize[4].height; a++)
for(int b = 0; b < PyrSize[4]
- 上一篇:Itti视觉注意模型Matlab
- 下一篇:0-1背包问题C语言源码
评论
共有 条评论