http://www.klab.caltech.edu/~xhou/papers/cvpr07.pdf
因为其开辟了视觉显著性的频域分析方法的先河,虽然侯晓迪现已明言谱残差理论是错误的,不过还是不能否认这个方法在视觉显著性研究领域起到的重要作用。
%% Read image from file
inImg = imread('img.jpg');
inImg = im2double(rgb2gray(inImg));
inImg = imresize(inImg, [64, 64], 'bilinear');
%% Spectral Residual
myFFT = fft2(inImg);
myLogAmplitude = log(abs(myFFT));
myPhase = angle(myFFT);
mySmooth = imfilter(myLogAmplitude, fspecial('average', 3), 'replicate');
mySpectralResidual = myLogAmplitude - mySmooth;
saliencyMap = abs(ifft2(exp(mySpectralResidual + i*myPhase))).^2;
%% After Effect
saliencyMap = imfilter(saliencyMap, fspecial('disk', 3));
saliencyMap = mat2gray(saliencyMap);
imshow(saliencyMap, []);
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int argc,char *argv[])
{
const char *filename = (argc >= 2 ? argv[1] : "lena.jpg");
Mat I=imread(filename);
if(I.empty())
return -1;
if(I.channels()==3)
cvtColor(I,I,CV_RGB2GRAY);
Mat planes[] = { Mat_<float>(I), Mat::zeros(I.size(), CV_32F) };
Mat complexI; //复数矩阵
merge(planes, 2, complexI); //把单通道矩阵组合成复数形式的双通道矩阵
dft(complexI, complexI); // 使用离散傅立叶变换
//对复数矩阵进行处理,方法为谱残差
Mat mag,pha,mag_mean;
Mat Re,Im;
split(complexI,planes); //分离复数到实部和虚部
Re=planes[0]; //实部
Im=planes[1]; //虚部
magnitude(Re,Im,mag); //计算幅值
phase(Re,Im,pha); //计算相角
float *pre,*pim,*pm,*pp;
//对幅值进行对数化
for(int i=0;i<mag.rows;i++)
{
pm=mag.ptr<float>(i);
for(int j=0;j<mag.cols;j++)
{
*pm=log(*pm);
pm++;
}
}
blur(mag, mag_mean, Size(5, 5)); //对数谱的均值滤波
mag = mag - mag_mean; //求取对数频谱残差
//把对数谱残差的幅值和相角划归到复数形式
for(int i=0;i<mag.rows;i++)
{
pre=Re.ptr<float>(i);
pim=Im.ptr<float>(i);
pm=mag.ptr<float>(i);
pp=pha.ptr<float>(i);
for(int j=0;j<mag.cols;j++)
{
*pm=exp(*pm);
*pre=*pm * cos(*pp);
*pim=*pm * sin(*pp);
pre++;
pim++;
pm++;
pp++;
}
}
Mat planes1[] = { Mat_<float>(Re),Mat_<float>(Im) };
merge(planes1, 2, complexI); //重新整合实部和虚部组成双通道形式的复数矩阵
idft(complexI, complexI, DFT_SCALE); // 傅立叶反变换
split(complexI, planes); //分离复数到实部和虚部
Re=planes[0];
Im=planes[1];
magnitude(Re,Im,mag); //计算幅值和相角
for(int i=0;i<mag.rows;i++)
{
pm=mag.ptr<float>(i);
for(int j=0;j<mag.cols;j++)
{
*pm=(*pm) * (*pm);
pm++;
}
}
GaussianBlur(mag,mag,Size(7,7),2.5,2.5);
Mat invDFT, invDFTcvt;
normalize(mag,invDFT,0,255,NORM_MINMAX); //归一化到[0,255]供显示
invDFT.convertTo(invDFTcvt, CV_8U); //转化成CV_8U型
imshow("SpectualResidual", invDFTcvt);
imshow("Original Image", I);
waitKey(0);
return 0;
}