Python-OpenCV cv2 OpenCV 错误:断言失败 [英] Python-OpenCV cv2 OpenCV Error: Assertion failed
问题描述
我正在尝试使用 openCV 在 python 中创建一个图像.我制作了一个列表列表,每个列表有 16 个数字,从 0 到 255(16 个列表).然后我将大列表转换为 numpy ndarray,并尝试使用 cv2.imwrite() 将其写入图像.这是我的代码:
I'm trying to create an image in python using openCV. I make a list of lists, each list having 16 numbers, from 0 to 255 (16 lists). Then I convert the big list in a numpy ndarray, and try to write that into an image using cv2.imwrite(). This is my code:
import cv2
import numpy as np
colours = []
numbers = []
a=0
for i in range(256):
numbers.append(a)
a+=1
for x in range(16):
new_list = [numbers[16*x:16*x+16]]
colours.append(new_list)
col = np.asarray(colours)
new_image = cv2.imwrite("rainbow.png",col)
它运行良好,直到最后一行.然后它给了我这个错误:
It runs well until the last line. Then it gives me this error:
OpenCV Error: Assertion failed (image.channels() == 1 || image.channels() == 3 || image.channels() == 4) in cv::imwrite_, file C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp, line 600
Traceback (most recent call last):
File "kormou.py", line 16, in <module>
new_image = cv2.imwrite("rainbow.png",col)
cv2.error: C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:600: error: (-215) image.channels() == 1 || image.channels() == 3 || image.channels() == 4 in function cv::imwrite_
总的来说,我是一个初学者,所以我遗漏了一些非常明显的东西,但我在这里找不到类似的错误问题.
In generally I'm a beginner so it may be something very obvious which I'm missing but I haven't been able to find similar error question here.
推荐答案
你犯了一个非常小的错误.从以下工作代码中找出 -
There is this very minute mistake you are doing. Figure out from the following working code -
import cv2
import numpy as np
colours = []
numbers = []
a=0
for i in range(256):
numbers.append(a)
a+=1
for x in range(16):
new_list = numbers[16*x:16*x+16]
colours.append(new_list)
print colours
col = np.asarray(colours)
new_image_flag = cv2.imwrite("rain.png",col)
检查编辑是否有提示.
这篇关于Python-OpenCV cv2 OpenCV 错误:断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!