OpenCv CreateImage功能不起作用

时间:2012-03-14 21:26:06

标签: python opencv

我正在尝试使用opencv v 2.1创建一个图像,但是我收到了这个错误:     图像= cv.CreateImage((W,H),no_of_bits,通道) AttributeError:'module'对象没有属性'CreateImage'

代码是

#!/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()

2 个答案:

答案 0 :(得分:18)

由于没有很多很好的例子如何用cv2创建一个用颜色填充的新空白图像,所以这里有一个:

创建某些(R,G,B)颜色的OpenCV图像:

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)

答案 1 :(得分:5)

您正在撰写名称空间。只使用import cv,而不是其他。

>>> 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 )>