HSV2BGR转换在Python OpenCV脚本中失败 [英] HSV2BGR conversion fails in Python OpenCV script

查看:307
本文介绍了HSV2BGR转换在Python OpenCV脚本中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本应该拍摄灰度图像并将其值映射为色调.

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()

它在使用cvtColor的行上失败,并且出现此错误:

It fails on the line with cvtColor and I get this error:

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

我需要对outimageHSV数组进行其他操作以使其准备用于cvtColor吗?

Do I need to do something else to my outimageHSV array to make it ready for cvtColor?

推荐答案

错误消息暗示cv2.cvtColor要求图像的(颜色)深度为8或16位无符号整数(8U,16U)或32位浮点数(32F).尝试将astype(int)更改为astype(numpy.uint8)

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). Try changing astype(int) to astype(numpy.uint8)

这篇关于HSV2BGR转换在Python OpenCV脚本中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆