python - DCT with cv2和scipy

标签 python image opencv scipy dct

我需要使用图像色彩空间的DCT。

我有RGB imagem,需要将其转换为YUV Space并进行DCT。

这是我的代码:

import cv2
import scipy

# reading the imagem and salving it at bgr var.
  bgr = cv2.imread('C:\imgtest\DSC_0091.jpg')
# Vars B, G, R, receive the information of bgr.
  b,g,r = cv2.split(bgr)
# bgr will split the colorspace information in their respective variables.
  bgr = cv2.merge((b,g,r))
# Var YUV will receive the result of the cvtColor function, which is transforming the RGB(now bgr) image in YUV
  yuv = cv2.cvtColor(bgr,cv2.COLOR_BGR2YUV)
# Y, U, V will receive the YUV information, which is the oringinal image in YUV color channel.
  y,u,v = cv2.split(yuv)
# The information will be shared in their respective color channels.
  yuv = cv2.merge ((y,u,v))
# Then, i try make the DCT of the var R.
  scipy.fftpack.dct(r, type=2, n=None, axis=-1, norm=None, overwrite_x=False)
# Print r
  print r

但是,当我运行此代码时,出现此错误:
ValueError                                Traceback (most recent call last)
C:\Python\6.00.1x Files\cvtcolor.py in <module>()
     20 # Mostra a Imagem em YUV na Tela.
     21 # cv2.imshow('YUV',yuv)
---> 22 scipy.fftpack.dct(r, type=2, n=None, axis=-1, norm=None, overwrite_x=False)
     23 print r

C:\Users\Tiago\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\fftpack\realtransforms.pyc in dct(x, type, n, axis, norm, overwrite_x)
    133         raise NotImplementedError(
    134               "Orthonormalization not yet supported for DCT-I")
--> 135     return _dct(x, type, n, axis, normalize=norm, overwrite_x=overwrite_x)
    136 
    137 

C:\Users\Tiago\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\fftpack\realtransforms.pyc in _dct(x, type, n, axis, overwrite_x, normalize)
    243             raise ValueError("Type %d not understood" % type)
    244     else:
--> 245         raise ValueError("dtype %s not supported" % tmp.dtype)
    246 
    247     if normalize:

ValueError: dtype uint8 not supported 

我不知道代码中是否有错误,或者是因为B,G,R,Y变量未正确显示。

有什么可以帮助我的吗?

谢谢。

最佳答案

您应该将图像数组类型转换为“float”,不支持“uint8”类型。

bgr = cv2.imread('C:\imgtest\DSC_0091.jpg').astype('float32')
那就OK了

关于python - DCT with cv2和scipy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32105569/

相关文章:

Android - 如何加载大图像(将其转换为较小的尺寸)并将其作为 Base64 字节数组发送(避免 OutOfMemory 错误)

c# - 将图片从文件导入图片框

java - cvtColor 崩溃 android

opencv - 如何从 GPU 设备获取 YUV 组件?

python - 如何检查文件是python中的目录还是常规文件?

python nltk 下载给出解析器错误

python - 使用 F2PY 在 numpy 数组上使用 Fortran 函数时结果不一致

python - 创建 edu.stanford.nlp.time.TimeExpressionExtractorImpl 时出错

java - "javax.swing.JPanel cannot be cast"异常,令人困惑

java - 如何对openCV中的图片应用高斯模糊?