【问题标题】:How to get dct of an image in python using opencv如何使用opencv在python中获取图像的dct
【发布时间】:2023-04-10 07:28:01
【问题描述】:

我一直在尝试找出一种获取图像 DCT 的方法。得到图像并进行一堆过滤后,我想计算 DCT。以下是代码片段:

imgcv1 = cv2.split(imgcv)[0] 
cv2.boxFilter(imgcv1, 0, (7,7), imgcv1, (-1,-1), False, cv2.BORDER_DEFAULT)
#resize image to 32x32
cv2.resize( imgcv1, (32, 32 ) ,imgcv1)

我一直在尝试使用here 给出的描述,但没有成功。 当我尝试类似:

dst = cv2.dct(imgcv1)

我收到如下错误:

cv2.error: /build/buildd/opencv-2.3.1/modules/core/src/dxt.cpp:2247: error: (-215) type == CV_32FC1 || type == CV_64FC1 in function dct

从错误中我可以理解的是,输入不是 32 位浮点数而是 8 位。我如何转换它并将其作为 32 位浮点数传递?我不确定这个问题。我会很感激你的帮助。如何获得 DCT?我对 openCV 和 python 很陌生。我在同一主题上搜索了其他 2/3 线程,但没有用。

【问题讨论】:

    标签: python opencv dct


    【解决方案1】:

    假设您的图像是灰度图像,此函数在计算 DCT 时效果很好。

    import cv2 as cv2
    import numpy as np
    
    def doDct(inputMatrix):
        fltMatrix = np.float32(inputMatrix)/255.0
        fltDct = cv2.dct(fltMatrix)
        return np.uint8(fltMatrix * 255)
    

    【讨论】:

      【解决方案2】:

      我找到了解决方案。正如我所说,我们也需要在 0 到 1 的范围内将其转换为浮点数。然后我们可以在计算 DCT 后将其转换回来:

      imf = np.float32(imgcv1)/255.0  # float conversion/scale
      dct = cv2.dct(imf)              # the dct
      imgcv1 = np.uint8(dct*255.0)    # convert back to int
      

      【讨论】:

      • 不应该先乘以255再转成uint8吗?
      猜你喜欢
      • 2011-10-29
      • 2019-01-02
      • 2021-11-20
      • 2020-07-10
      • 2018-03-14
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 2020-08-11
      相关资源
      最近更新 更多