【问题标题】:OpenCv CreateImage Function isn't workingOpenCv CreateImage 函数不起作用
【发布时间】:2012-03-31 10:01:00
【问题描述】:

我正在尝试使用 opencv v 2.1 创建图像,但出现此错误:

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

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

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    由于没有太多好的示例如何使用 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)
    

    【讨论】:

      【解决方案2】:

      您正在对名称空间进行书写。只使用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 )>
      

      【讨论】:

        猜你喜欢
        • 2016-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-03
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-02
        相关资源
        最近更新 更多