如何向OpenCV函数输入复杂值干膜厚度()?

2024-04-24 02:47:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力实现一篇研究论文中所述的内容。它描述了如何从图像中提取傅立叶特征。我试图在编码时遵循这些步骤,但是反复遇到与输入数组的数据类型和维度相关的错误。 所以我要问的是如何向函数输入复值

我遵循了研究论文中的以下指示

傅立叶描述子:傅立叶描述子提供了一种编码的方法 通过将每个像素位置(x,y)映射到 复数(x+iy)。

  1. 按顺序记录每个像素的坐标值 (沿形状顺时针移动)
  2. 用坐标值构造复值向量 记录在步骤1中,即(x,y)→(x+i y)。在
  3. 取复值向量的DFT

我的问题出现在第三步

这就是我的实现

def get_dft(image):
    coordinates = cv.findNonZero(image)
    # the code below removes an unnecessary dimension
    coordinates = coordinates.reshape(coordinates.shape[0], 2)
    y = coordinates[:, 1] * 1j  # convert to complex numbers
    # the code below removes an unnecessary dimension
    y = y.reshape(coordinates.shape[0], 1)
    x = coordinates[:, 0].reshape(coordinates.shape[0], 1)
    # the statement below will convert from two separate arrays
    # to a single array with each element  
    # of the form [a + jb]
    t = x + y
    # below is where the error occurs
    dft = cv.dft(t, flags=cv.DFT_COMPLEX_INPUT) 

这是我得到的错误

^{pr2}$

当我转换为

a = numpy.ndarray(t)

我明白了

ValueError: sequence too large; cannot be greater than 32

它想说有超过32个维度。我不明白为什么会这样

当我试着

a = numpy.ndarray([t])

我知道错误了

TypeError: only integer scalar arrays can be converted to a scalar index

简而言之,我想按照文中提到的步骤,把复值向量

[[a+jb],[c+jd]...]    

并传递给DFT函数。


Tags: theto函数编码错误步骤像素向量
1条回答
网友
1楼 · 发布于 2024-04-24 02:47:31

我找到了解决问题的方法

def get_dft(image):
    coordinates = cv.findNonZero(image)
    coordinates = coordinates.reshape(coordinates.shape[0], 2).astype(float)
    y = coordinates[:, 1].reshape(coordinates.shape[0], 1)
    x = coordinates[:, 0].reshape(coordinates.shape[0], 1)
    t = cv.merge([x, y])  # used to convert to 2 channel
    dft = cv.dft(t, flags=cv.DFT_COMPLEX_INPUT)

我尝试了所有的numpy api,所有失败的原因我都不明白,但幸运的是OpenCV

^{pr2}$

成功了。在

它需要多个输入数组和连接来实现多通道输出。在

我还试着在opencvapi函数中输入复数

cv.dft(...)

这不是输入复数的正确方法。 OpenCV documentation explains complex input here

上面写着国旗,cv.DFT_复数输入在

specifies that input is complex input. If this flag is set, the input must have 2 channels. On the other hand, for backwards compatibility reason, if input has 2 channels, input is already considered complex

注意,我还面临转换为双通道的问题,这是由于我没有正确理解作为函数输入所需的结构cv::UMat()。在

总结是,
如果要在OpenCV API函数中输入复数

cv.dft(...)

您的输入必须由2个通道组成,要完成双通道阵列,OpenCV函数

^{pr2}$

link to its documentation,当您尝试组合多个单独的频道时,似乎可以正确地完成任务。在

相关问题 更多 >