OpenCv CreateImage Function isn't working


Question

I'm trying to create an image using opencv v 2.1, but I get that error : image=cv.CreateImage((w,h),no_of_bits,channels) AttributeError: 'module' object has no attribute 'CreateImage'

The code is

#!/usr/bin/python

import cv 
from opencv import *
from opencv.cv import *
from opencv.highgui import *
import sys

import PIL

w=500
h=500
no_of_bits=8
channels=3
image=cv.CreateImage((w,h),no_of_bits,channels) 

cv.ShowImage('WindowName',image) 
cvWaitKey()
1
5
3/14/2012 9:26:05 PM

Accepted Answer

You are writing over the name space. Only use the import cv, not the other ones.

>>> import cv 
>>> w=500
>>> no_of_bits=8
>>> channels=3
>>> h=500
>>> image=cv.CreateImage((w,h),no_of_bits,channels) 
>>> print image
<iplimage(nChannels=3 width=500 height=500 widthStep=1500 )>
5
3/14/2012 9:34:53 PM

Since there aren't that many good examples how to create new blank image filled with a color using cv2, here's one:

Create OpenCV image of certain (R, G, B) color:

import cv2
import numpy as np

def create_blank(width, height, rgb_color=(0, 0, 0)):
    """Create new image(numpy array) filled with certain color in RGB"""
    # Create black blank image
    image = np.zeros((height, width, 3), np.uint8)

    # Since OpenCV uses BGR, convert the color first
    color = tuple(reversed(rgb_color))
    # Fill image with color
    image[:] = color

    return image

# Create new blank 300x300 red image
width, height = 300, 300

red = (255, 0, 0)
image = create_blank(width, height, rgb_color=red)
cv2.imwrite('red.jpg', image)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon