我正在尝试从本文中实现S1度量(III -A节的光谱度量)。在这里,我们必须计算图像的幅度光谱的斜率(alpha),以测量清晰度。我能够编写算法的另一部分,但无法计算斜率。这是我的代码。函数'alpha'是我计算幅度_spectrum的地方,我认为使用它可以计算斜率,但不确定如何做 -
def aplha(image_block):
img_float32 = np.float32(image_block)
dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
return output (??)
其余代码:
def S1_calc(alpha):
tou1 = -3
tou2 = 2
output = 1 - (1 / (1 + np.exp(tou1 * (alpha - tou2))))
return output
def lx(image_block):
b = 0.7656
k = 0.0364
y = 2.2
return np.power((b + k * image_block), y)
def contrast(lx_val):
T1 = 5
T2 = 2
max_val = np.max(lx_val)
min_val = np.min(lx_val)
mean_val = np.mean(lx_val)
return (((max_val - min_val) < T1) or (mean_val < T2))
def image_gray(image_RGB):
output = (0.2989 * image_RGB[:,:,0] +
0.5870 * image_RGB[:,:,1] +
0.1140 * image_RGB[:,:,2])
return output
def S1(gray_image, m = 32, d = 24):
### SPECTRAL MEASURE OF SHARPNESS ###
# m = each block size
# d = overlapping pixels of neighbouring blocks
h,w = gray_image.shape
output = gray_image.copy()
row = 0
while (row < h):
col = 0
while (col < w):
top = row
bottom = min(row + m, h)
left = col
right = min(col + m, w)
image_block = gray_image[top : bottom, left : right]
lx_val = lx(image_block)
contrast_bool = contrast(lx_val)
if contrast_bool==True:
output[top : bottom, left : right] = 0
else:
alpha_val = aplha(image_block)
output[top : bottom, left : right] = S1_calc(alpha_val)
col = col + m - d
row = row + m - d
return output
正在使用jupyter笔记本,python 3.6
您可以检查此MATLAB代码。另请参阅另一个MATLAB代码。
根据后一个,我们需要知道freq
和power
值,然后我们可以使用线性函数拟合这两个VAR,该行的斜率是我们需要的。我们可以使用np.polyfit
的斜率。
现在,我们的问题是如何获取图像的freq
,您可以做到这一点:
from skimage.data import camera
import numpy as np
image = camera()
height, width = image.shape
u, v = np.meshgrid(np.arange(height // 2), np.arange(width // 2))
freq = np.round(np.sqrt(u**2 + v**2)).astype(np.int64)
现在freq
应该与输入图像的FFT变换相同。您需要总结magnitude_spectrum
具有相同freq的所有值,例如:
freq_uniq = np.unique(freq.flatten())
y = []
for value in f_uniq:
y.append(magnitude_spectrum[f == value].sum())
y = np.array(y)
最后,您可以拟合freq_uniq
和y
并获得斜率。您可能需要先用np.log
进行缩放。