[英]HSV2BGR conversion fails in Python OpenCV script
我的腳本應該采用灰度圖像並將值映射到色調。
#!/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的行上失敗了,我得到了這個錯誤:
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做好准備?
錯誤消息暗示cv2.cvtColor需要(顏色)深度為8或16位無符號整數(8U,16U)或32位浮點(32F)的圖像。 嘗試將astype(int)
更改為astype(numpy.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.