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.
If you use cv2 , correct method is to use . copy() method in Numpy. It will create a copy of the array you need. Otherwise it will produce only a view of that object.
Python PIL | copy() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL. Image. copy() method copies the image to another image object, this method is useful when we need to copy the image but also retain the original.
OpenCV uses BGR image format. So, when we read an image using cv2. imread() it interprets in BGR format by default. We can use cvtColor() method to convert a BGR image to RGB and vice-versa.
The new cv2
interface for Python integrates numpy arrays 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:
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:
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 cv2
interface rather than the older cv
one. I made the change recently and have never looked back. You can read more about cv2
at the OpenCV Change Logs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With