Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new RGB OpenCV image using Python? [duplicate]

Tags:

python

opencv

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.

like image 307
Phil Avatar asked Oct 14 '12 11:10

Phil


People also ask

How do I duplicate an image in OpenCV?

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.

How do you duplicate an image in Python?

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.

How do I convert an image to RGB in OpenCV?

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.


1 Answers

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.

like image 73
hjweide Avatar answered Sep 23 '22 09:09

hjweide