DCT With cv2 and scipy



我需要取图像颜色空间的dct。

我有RGB图像,需要将其转换为YUV空间并制作DCT。

下面是我的代码:

import cv2
import scipy
# reading the imagem and salving it at bgr var.
  bgr = cv2.imread('C:imgtestDSC_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:Python6.00.1x Filescvtcolor.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:UsersTiagoAppDataLocalEnthoughtCanopyUserlibsite-packagesscipyfftpackrealtransforms.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:UsersTiagoAppDataLocalEnthoughtCanopyUserlibsite-packagesscipyfftpackrealtransforms.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:imgtestDSC_0091.jpg').astype('float32')

那就没问题了

最新更新