如何进行傅立叶变换多张图像并将输出保存到单个对象中

时间:2019-11-25 08:56:57

标签: python loops numpy opencv fft

我有96张不同尺寸和像素大小的jpeg无人机正射影像。我正在尝试通过使用基于非参考的图像质量技术来确定每张正射照片的质量。这需要使用傅立叶变换将图像从空间域转换到频域。分析频率的分布可以深入了解图像中的模糊和噪点数量,从而了解图像的质量,因为模糊会减少高频数量。

但是,为了相互比较不同图像的质量,我需要创建一个索引,该索引必须针对我的整个数据集进行规范化,这将使我能够比较数据集中的图像之间的图像质量,但是而不是我的数据集之外的图像(可以)。为此,我需要分析所有96 jpeg频率的分布,并确定最高15%最高频率的值。如果我知道数据集的最高频率,则可以使用它来定义一个阈值来创建我的图像质量指数。

所以我的问题是。如何创建一个循环以读取所有图像,应用高斯模糊,应用傅立叶变换,将所有频率的分布保存到一个对象中,然后确定数据集中最高频率的前15%? >

这是我到目前为止创建的用于处理一张图像的代码:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('Image1.jpg', 0)

# Smooting by gaussian blur to remove noise
imgSmooth = cv2.GaussianBlur(img, (5, 5), 0)

# Fourier transform
dft = cv2.dft(np.float32(imgSmooth), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))

我对python非常陌生,因为我通常使用R编写代码。对您的帮助或建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

据我了解的问题,您想计算每个图像的平均频率,然后获取那些图像中前15%的频率值。我们可以通过以下方式做到这一点:

import numpy as np
import cv2
import os
from matplotlib import pyplot as plt

images_dir = 'folder/containing/images/'
images_list = os.listdir(images_dir)
images_mean_freq = []

for img_file in images_list:
    img_path = os.path.join(images_dir, img_file)
    img = cv2.imread(img_path, 0)

    # Smooting by gaussian blur to remove noise
    imgSmooth = cv2.GaussianBlur(img, (5, 5), 0)

    # Fourier transform
    dft = cv2.dft(np.float32(imgSmooth), flags=cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(dft)
    magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
    # get the standard deviation of the frequencies
    img_mean_freq = np.mean(magnitude_spectrum)
    images_mean_freq.append(img_mean_freq)
# then we can get the value of the top 15% highest frequencies, that is the 85% percentile of the distribution
top_15 = np.percentile(images_mean_freq, 85)
# finally we can select which images are above (or below) this top 15% threshold
images_above_top_15 = [images_list[idx] for idx in range(len(images_list)) if images_mean_freq[idx] > top_15]