1

EDIT: After some more testing and a response form the scipy mailing list, the issue appears to be with fspecial(). To get the same output I need to generate the same kind of kernel in Python as the Matlab fspecial command is producing. For now I will try to export the kernel from matlab and work from there. Added as a edit since question has been "closed"


I am trying to port the following MATLAB code to Python. It seems to work but the output is different form MATLAB. I think the problem is with apply a "mean" filter to the log(amplituide). Any help appreciated.

The MATLAB code is from: http://www.klab.caltech.edu/~xhou/projects/spectralResidual/spectralresidual.html

%% Read image from file
inImg = im2double(rgb2gray(imread('1.jpg')));
inImg = imresize(inImg, 64/size(inImg, 2));

%% Spectral Residual
myFFT = fft2(inImg);
myLogAmplitude = log(abs(myFFT));
myPhase = angle(myFFT);
mySpectralResidual = myLogAmplitude - imfilter(myLogAmplitude, fspecial('average', 3), 'replicate');
saliencyMap = abs(ifft2(exp(mySpectralResidual + i*myPhase))).^2;

%% After Effect
saliencyMap = mat2gray(imfilter(saliencyMap, fspecial('gaussian', [10, 10], 2.5)));
imshow(saliencyMap);

Here is my attempt in python:

from skimage import img_as_float                                                       
from skimage.io import imread
from skimage.color import rgb2gray                                                    
from scipy import fftpack, ndimage, misc                                        
from scipy.ndimage import uniform_filter
from matplotlib.pyplot as plt

# Read image from file
image = img_as_float(rgb2gray(imread('1.jpg')))
image = misc.imresize(image, 64.0 / image.shape[0])

# Spectral Residual
fft = fftpack.fft2(image)                                                   
logAmplitude = np.log(np.abs(fft))                                          
phase = np.angle(fft)                                                       
avgLogAmp = uniform_filter(logAmplitude, size=3, mode="nearest") #Is this same a applying "mean" filter            
spectralResidual = logAmplitude - avgLogAmp                                 
saliencyMap = np.abs(fftpack.ifft2(np.exp(spectralResidual + 1j * phase))) ** 2

# After Effect
saliencyMap = ndimage.gaussian_filter(sm, sigma=2.5)
plt.imshow(sm)
plt.show()

For completness here is a input image and the output from MATLAB and python.

Input Image, Island Saliency Map (MATLAB) Saliency Map (PYTHON)

4

1 回答 1

2

我怀疑任何人都能够给你一个明确的答案。它可能是任何数量的东西......可能是一个 FFT 以 0 为中心,而另一个不是,可能是某个地方的浮点数与双精度数,可能是对绝对值的错误处理,可能是过滤器设置,......

如果我是你,我会为这两种计算写出一些中间值,并找到一种比较它们的方法。从中间开始,如果比较好就往下走,如果比较不好就往上走。也许将 python 脚本中的中间值写入文件,导入 matlab,获取元素差异,然后绘制图形。如果它们的尺寸不同,那就是线索#1。

于 2013-05-15T21:17:07.763 回答