[英]HSV2BGR conversion fails in Python OpenCV script
My script is supposed to take a greyscale image and map the values to hues. 我的脚本应该采用灰度图像并将值映射到色调。
#!/usr/bin/env python
import cv2
import numpy
infile = cv2.imread('Lenna.png')
infile = infile[:,:,0]
hues = (numpy.array(infile)/255.)*179
outimageHSV = numpy.array([[[b,255,255] for b in a] for a in hues]).astype(int)
outimageBGR = cv2.cvtColor(outimageHSV, cv2.COLOR_HSV2BGR)
cv2.imshow('dst_rt', outimageBGR)
cv2.waitKey(0)
cv2.destroyAllWindows()
It fails on the line with cvtColor and I get this error: 它在使用cvtColor的行上失败了,我得到了这个错误:
OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cvtColor, file /tmp/opencv20150506-38415-u2kidu/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3644
Traceback (most recent call last):
File "luma2hue.py", line 16, in <module>
outimageBGR = cv2.cvtColor(outimageHSV, cv2.COLOR_HSV2BGR)
cv2.error: /tmp/opencv20150506-38415-u2kidu/opencv-2.4.11/modules/imgproc/src/color.cpp:3644: error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor
Do I need to do something else to my outimageHSV array to make it ready for cvtColor? 我是否需要对我的outimageHSV数组执行其他操作以使其为cvtColor做好准备?
The error message implies that cv2.cvtColor expects an image with a (color) depth of 8 or 16 bit unsigned int (8U, 16U) or 32 bit float (32F). 错误消息暗示cv2.cvtColor需要(颜色)深度为8或16位无符号整数(8U,16U)或32位浮点(32F)的图像。 Try changing astype(int)
to astype(numpy.uint8)
尝试将astype(int)
更改为astype(numpy.uint8)
outimageHSV needs to be casted as uint8. outimageHSV需要作为uint8进行转换。
import numpy as np
outimageHSV = np.uint8(outimageHSV)
outimageBGR = cv2.cvtColor(outimageHSV, cv2.COLOR_HSV2BGR)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.