(215:断言失败)函数'DFT&#39中的type==CV_32FC1||type==CV_32FC2||type==CV_64FC1||type==CV_64FC2;

2022-04-02 00:00:00 python opencv image-processing fft

问题描述

我正在尝试使用傅立叶校正图像中的曝光。 这就是我面临的错误

  5     padded = np.log(padded + 1) #so we never have log of 0
  6     global complex
  7     complex = cv2.dft(np.float32(padded)/255.0, flags = cv2.DFT_COMPLEX_OUTPUT)
  8     complex = np.fft.fftshift(complex)
  9     img = 20 * np.log(cv2.magnitude(complex[:,:,0], complex[:,:,1]))

  error: OpenCV(4.4.0) /tmp/pip-req-build-njn2fp78/opencv/modules/core/src/dxt.cpp:3335: error: (-215:Assertion failed) type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2 in function 'dft'

我的代码:

import cv2
import numpy as np
from math import exp, sqrt

image = cv2.imread("2.png")
# grayimg = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
height, width, alpha = image.shape
dft_M = cv2.getOptimalDFTSize(height)
dft_N = cv2.getOptimalDFTSize(width)

#Filter parameters
yh, yl, c, d0, = 0, 0, 0, 0
#User parameters
y_track, d0_track, c_track = 0, 0, 0
complex = 0

def main():
    #copyMakeBorder(src, top, bottom, left, right, borderType[, dst[, value]]) 
    #BORDER_CONSTANT = Pad the image with a constant value (i.e. black or 0)
    padded = cv2.copyMakeBorder(image, 0, dft_M - height, 0, dft_N - width, cv2.BORDER_CONSTANT, 0) 
    padded = np.log(padded + 1) #so we never have log of 0
    global complex
    complex = cv2.dft(np.float32(padded)/255.0, flags = cv2.DFT_COMPLEX_OUTPUT)
    complex = np.fft.fftshift(complex)
    img = 20 * np.log(cv2.magnitude(complex[:,:,0], complex[:,:,1]))

    cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
    cv2.imshow("Image", image)
    cv2.resizeWindow("Image", 400, 400)

    cv2.namedWindow('DFT', cv2.WINDOW_NORMAL)
    cv2.imshow("DFT", np.uint8(img))
    cv2.resizeWindow("DFT", 250, 250)

    cv2.createTrackbar("YL", "Image", y_track, 100, setyl)
    cv2.createTrackbar("YH", "Image", y_track, 100, setyh)
    cv2.createTrackbar("C", "Image", c_track, 100, setc)
    cv2.createTrackbar("D0", "Image", d0_track, 100, setd0)

    cv2.waitKey(0)     
    cv2.destroyAllWindows()

def homomorphic():
    global yh, yl, c, d0, complex
    du = np.zeros(complex.shape, dtype = np.float32)
    #H(u, v)
    for u in range(dft_M):
        for v in range(dft_N):
            du[u,v] = sqrt((u - dft_M/2.0)*(u - dft_M/2.0) + (v - dft_N/2.0)*(v - dft_N/2.0))

    du2 = cv2.multiply(du,du) / (d0*d0)
    re = np.exp(- c * du2)
    H = (yh - yl) * (1 - re) + yl
    #S(u, v)
    filtered = cv2.mulSpectrums(complex, H, 0)
     #inverse DFT (does the shift back first)
    filtered = np.fft.ifftshift(filtered)
    filtered = cv2.idft(filtered)
    #normalization to be representable 
    filtered = cv2.magnitude(filtered[:, :, 0], filtered[:, :, 1])
    cv2.normalize(filtered, filtered, 0, 1, cv2.NORM_MINMAX)
    #g(x, y) = exp(s(x, y))
    filtered = np.exp(filtered)
    cv2.normalize(filtered, filtered,0, 1, cv2.NORM_MINMAX)

    cv2.namedWindow('homomorphic', cv2.WINDOW_NORMAL)
    cv2.imshow("homomorphic", filtered)
    cv2.resizeWindow("homomorphic", 600, 550)

def setyl(y_track):
    global yl
    yl = y_track
    if yl == 0:
        yl = 1
    if yl > yh:
        yl = yh - 1
    homomorphic()

def setyh(y_track):
    global yh
    yh = y_track
    if yh == 0:
        yh = 1
    if yl > yh:
        yh = yl + 1
    homomorphic()

def setc(c_track):
    global c
    c = c_track/100.0
    if c == 0:
        c_track = 1    
    homomorphic()

def setd0(d0_track):
    global d0
    d0 = d0_track
    if d0 == 0:
        d0 = 1
    homomorphic()

main()

我不理解我面临的问题。我如何解决此问题?

说明

理想的大小我们可以为图像设置新的边框大小,在底部和右侧(无论如何都可以这样做),值不变。填充后,我们可以变换到频域,然后进行移位

然后,当用户更改轨迹栏时,负责更改其参数的函数调用同态函数。 跟踪条在主函数处定义,并接受一个限制,一个与条相关的函数和一个对应于实际值的变量。这些条形按模式保持在0-100范围内,并负责更改过滤器的参数。


解决方案

是的,是3D而不是2D,以下是快速解决方案:

img = cv2.cvtColor(np.float32(image), cv2.COLOR_BGR2GRAY)
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)

相关文章