使用 OpenCV 计算图像的离散余弦变换
我正在尝试使用 OpenCV 2.3 Python 包装器来计算图像的 DCT。据说,图像 == numpy 数组 == CV 矩阵,所以我认为这应该可行:
import cv2
img1 = cv2.imread('myimage.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
img2 = cv2.dct(img1)
但是,这会引发错误:
cv2.error: /usr/local/lib/OpenCV-2.3.1/modules/core/src/dxt.cpp:2247: error: (-215) type == CV_32FC1 || type == CV_64FC1 in function dct
我意识到该错误意味着输入应该是 32 位或 64 位单通道浮点矩阵。然而,我认为这就是在指定灰度时我的图像应该如何加载,或者至少它应该足够接近,以便 CV2 应该能够计算出转换。
使用 cv2 将图像转换为 DCT 的正确方法是什么?
I'm trying to use the OpenCV 2.3 Python wrapper to calculate the DCT for an image. Supposedly, images == numpy arrays == CV matrices, so I thought this should work:
import cv2
img1 = cv2.imread('myimage.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
img2 = cv2.dct(img1)
However, this throws the error:
cv2.error: /usr/local/lib/OpenCV-2.3.1/modules/core/src/dxt.cpp:2247: error: (-215) type == CV_32FC1 || type == CV_64FC1 in function dct
I realize the error means the input should be either a 32-bit or 64-bit single-channel floating point matrix. However, I thought that's how my image should have loaded when specifying grayscale, or at least it should be close enough so that CV2 should be able to figure out the conversion.
What's the appropriate way to convert an image for DCT using cv2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 cv2 似乎没有任何简单的方法可以做到这一点。我能找到的最接近的解决方案是:
There doesn't seem to be any easy way to do this with cv2. The closest solution I could find is:
我本来不想写这个答案,但当我看到一些答案虽然错误却被投票通过时,我决定写下来。
dct 运算适用于任何范围的输入,所以我真的不明白为什么其他人将其缩放到 [0, 1]。但在opencv中,你需要传递
numpy.float32
数字。但如果你缩放它,几乎所有的小值都会丢失。
以下是公式和示例的链接:
https://users.cs.cf.ac。 uk/Dave.Marshall/Multimedia/node231.html#DCTbasis
I did not want to write this answer but as I seen some answers are voted up while they are wrong, I decided to write.
The
dct
operation works on inputs in any range so I really do not understand why others scaled it to [0, 1]. But in opencv, you need to passnumpy.float32
numbers.But if you scale it, almost all small values will be lost.
Here is a link to formula and examples:
https://users.cs.cf.ac.uk/Dave.Marshall/Multimedia/node231.html#DCTbasis
这是我从 openCV 论坛获得的一个解决方案,它有效。
Here is a solution that I got from openCV forums and it worked.
好吧,当您将图像加载为灰度时,它实际上是以每像素 8 位读取的,而不是作为 32 位浮点值读取的。
以下是您的操作方法:
另外,请查看 dft.py 示例。这应该会让您了解如何使用 dft 。
Well, when you load the image as grayscale, it is actually read in at 8-bits per pixel and not as 32-bit float values.
Here is how you would do it:
Also, have a look at the dft.py example. This should give you a feel for how to use the
dft
as well.Numpy 具有切片运算符,用于在不同顺序的数组之间工作。
Numpy has slice operators for working between arrays of different orders.
下面是如何使用 scipy 执行此操作:
Heres how to do it with scipy: