def do_stuff():
    image = get_image()
    dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
    # shift the zero-frequncy component to the center of the spectrum
    dft_shift = np.fft.fftshift(dft)
    # save image of the image in the fourier domain.
    magnitude_spectrum = 20 * np.log(
        cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
    # magnitude_spectrum = 20 * np.log(np.sqrt(dft_shift[:, :, 0] + dft_shift[:, :, 1]))
    cv2.imwrite("magnitude_spectrum.png", magnitude_spectrum)

    rows, cols = image.shape
    crow, ccol = rows // 2, cols // 2
    # create a mask first, center square is 1, remaining all zeros
    mask = np.zeros((rows, cols, 2), np.uint8)
    mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1
    # mask -= 1
    # mask = np.abs(mask)
    # apply mask and inverse DFT
    fshift = dft_shift * mask
    f_ishift = np.fft.ifftshift(fshift)
    img_back = cv2.idft(f_ishift)
    img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
    img_back = img_back / img_back.max() * 255
    cv2.imwrite("filtered_image_low.png", img_back)
def hpf(img):
    gray = BGR_to_GRAY(img)
    dft = cv2.dft(np.float32(gray), flags=cv2.DFT_COMPLEX_OUTPUT)
    dftShift = np.fft.fftshift(dft)
    R, C = gray.shape
    r, c = int(R / 2), int(C / 2)
    out = hpk(r, c, 8, dftShift)
    out = (out / out.max() * 255).astype(np.uint8)
    return out
def HPF_OpenCv(image):
    dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
    dftShift = np.fft.fftshift(dft)

    R, C = image.shape
    r, c = int(R / 2), int(C / 2)

    Plt_Contrast(image, HPK_OpenCv(r, c, 1, dftShift),
                 HPK_OpenCv(r, c, 2, dftShift), HPK_OpenCv(r, c, 4, dftShift),
                 HPK_OpenCv(r, c, 6, dftShift), HPK_OpenCv(r, c, 8, dftShift))
def DFT_IDFT_OpenCv(image):
    dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
    dftShift = np.fft.fftshift(dft)
    fourier = 20 * np.log(cv2.magnitude(dftShift[:, :, 0], dftShift[:, :, 1]))

    ishift = np.fft.ifftshift(dftShift)
    iimage = cv2.idft(ishift)
    iimage = cv2.magnitude(iimage[:, :, 0], iimage[:, :, 1])

    Plt_Contrast(image, fourier, iimage)
def LPF_OpenCv(image):
    dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
    dftShift = np.fft.fftshift(dft)

    R, C = image.shape
    mask = np.zeros((R, C, 2), np.uint8)
    r, c = int(R / 2), int(C / 2)

    Plt_Contrast(image, LPK_OpenCv(mask, r, c, 5, dftShift),
                 LPK_OpenCv(mask, r, c, 10, dftShift),
                 LPK_OpenCv(mask, r, c, 15, dftShift),
                 LPK_OpenCv(mask, r, c, 25, dftShift),
                 LPK_OpenCv(mask, r, c, 50, dftShift))
def PQFT(prev_img, next_img, map_size = 64):
    """
        Computing saliency using phase spectrum of quaternion fourier transform.
        Images are 3 channel.
    """

    new_shape = (int(next_img.shape[1]/(next_img.shape[0]/map_size)), map_size)
    next_img = cv2.resize(next_img, new_shape, cv2.INTER_LINEAR)
    (b, g, r) = cv2.split(next_img)

    # color channels
    R = r-(g+b)/2
    G = g-(r+b)/2
    B = b-(r+g)/2
    Y = (r+g)/2-abs(r-g)/2-b

    red_green = R-G
    blue_yellow = B-Y

    # intensity
    intensity = np.sum(next_img, axis=-1)

    # motion
    prev_img = cv2.resize(prev_img, new_shape, cv2.INTER_LINEAR)
    prev_intensity = np.sum(prev_img, axis=-1)
    movement = abs(intensity-prev_intensity)

    planes = [
        movement.astype(np.float64),
        red_green.astype(np.float64),
    ]
    f1 = cv2.merge(planes)
    f1 = cv2.dft(f1)
    planes = cv2.split(f1)

    magnitude1 = cv2.magnitude(planes[0], planes[1])
    magnitude1 = cv2.multiply(magnitude1, magnitude1)

    planes = [
        blue_yellow.astype(np.float64),
        intensity.astype(np.float64),
    ]

    f2 = cv2.merge(planes)
    f2 = cv2.dft(f2)
    planes = cv2.split(f2)

    magnitude2 = cv2.magnitude(planes[0], planes[1])
    magnitude2 = cv2.multiply(magnitude2, magnitude2)

    magnitude = magnitude1+magnitude2
    magnitude = cv2.sqrt(magnitude)

    planes[0] = planes[0]/magnitude
    planes[1] = planes[1]/magnitude
    f2 = cv2.merge(planes)

    planes = cv2.split(f1)
    planes[0] = planes[0]/magnitude
    planes[1] = planes[1]/magnitude
    f1 = cv2.merge(planes)

    cv2.dft(f1, f1, cv2.DFT_INVERSE)
    cv2.dft(f2, f2, cv2.DFT_INVERSE)

    planes = cv2.split(f1)
    magnitude1 = cv2.magnitude(planes[0], planes[1])
    magnitude1 = cv2.multiply(magnitude1, magnitude1)

    planes = cv2.split(f2)
    magnitude2 = cv2.magnitude(planes[0], planes[1])
    magnitude2 = cv2.multiply(magnitude2, magnitude2)

    magnitude = magnitude1 + magnitude2

    magnitude = cv2.GaussianBlur(magnitude, (5, 5), 8, None, 8)
    saliency = np.zeros((new_shape[0], new_shape[1], 1), np.uint8)
    saliency = cv2.normalize(magnitude, saliency, 0,
                             255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)

    return saliency
Esempio n. 7
0
def apply_fourier_transform(img):
    dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(dft)
    return dft_shift
Esempio n. 8
0
from cv2 import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np

img = cv.imread('tree.jpg', 0)
dft = cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT)

dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20 * \
    np.log(cv.magnitude(dft_shift[:,:, 0], dft_shift[:,:, 1]))

plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Source'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

rows, cols = img.shape
crow, ccol = int(rows / 2), int(cols / 2)

mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1

fshift = dft_shift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv.idft(f_ishift)

plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Source'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(img_back, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
Esempio n. 9
0
from cv2 import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('blackmagician,jpg', 0)
img_float32 = np.float32(img)

dft = cv.dft(img_float32, flags == cv.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

magnitude_spectrum = 20 * np.log(
    cv.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))