Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

python - OpenCv CreateImage Function isn't working

I'm trying to create an image using opencv v 2.1, but I get this 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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...