gpt4 book ai didi

python - (215 :Assertion failed) type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2 in function 'dft'

转载 作者:行者123 更新时间:2023-12-02 17:56:28 35 4
gpt4 key购买 nike

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

  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()

我不明白我面临的问题。我该如何解决这个问题?

描述

The ideal sizes 我们可以为图像制作新的边框尺寸,在底部和右侧(无论如何都可以做到)具有恒定值。 padding之后我们可以变换到频域,然后做一个shift

同态函数随后由负责在用户更改轨迹栏时更改其参数的函数调用。轨迹条是在主函数中定义的,并带有一个限制,一个与条相关的函数和一个对应于实际值的变量。条形图按模式保持在 0-100 范围内,负责更改过滤器的参数。

最佳答案

是的,情况是 3d 而不是 2d,这里是快速解决方案:

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

关于python - (215 :Assertion failed) type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2 in function 'dft' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64604533/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com