使用 Python 创建一个新的 RGB OpenCV 图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12881926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a new RGB OpenCV image using Python?
提问by Phil
Possible Duplicate:
OpenCv CreateImage Function isn't working
I wish to create a new RGB image in OpenCV using Python. I don't want to load the image from a file, just create an empty image ready to do operations on.
我希望使用 Python 在 OpenCV 中创建一个新的 RGB 图像。我不想从文件加载图像,只需创建一个准备对其进行操作的空图像。
采纳答案by hjweide
The new cv2interface for Python integrates numpyarrays into the OpenCV framework, which makes operations much simpler as they are represented with simple multidimensional arrays. For example, your question would be answered with:
cv2Python的新接口将numpy数组集成到 OpenCV 框架中,这使得操作更加简单,因为它们用简单的多维数组表示。例如,您的问题将得到以下回答:
import cv2 # Not actually necessary if you just want to create an image.
import numpy as np
blank_image = np.zeros((height,width,3), np.uint8)
This initialises an RGB-image that is just black. Now, for example, if you wanted to set the left half of the image to blue and the right half to green , you could do so easily:
这将初始化一个纯黑色的 RGB 图像。现在,例如,如果您想将图像的左半部分设置为 blue ,将右半部分设置为 green ,则可以轻松完成:
blank_image[:,0:width//2] = (255,0,0) # (B, G, R)
blank_image[:,width//2:width] = (0,255,0)
If you want to save yourself a lot of trouble in future, as well as having to ask questions such as this one, I would strongly recommend using the cv2interface rather than the older cvone. I made the change recently and have never looked back. You can read more about cv2at the OpenCV Change Logs.
如果您想在将来为自己省去很多麻烦,并且不得不提出诸如此类的问题,我强烈建议您使用该cv2界面而不是旧的界面cv。我最近进行了更改,并且从未回头。您可以cv2在OpenCV 更改日志中阅读更多相关信息。
回答by b_m
CreateImage(size, depth, channels)
CreateImage(大小,深度,通道)
https://opencv.willowgarage.com/documentation/python/core_operations_on_arrays.html#CreateImage
https://opencv.willowgarage.com/documentation/python/core_operations_on_arrays.html#CreateImage

