gpt4 book ai didi

python - OpenCv CreateImage 函数不工作

转载 作者:太空狗 更新时间:2023-10-30 00:21:09 27 4
gpt4 key购买 nike

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

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

AttributeError: 'module' object has no attribute '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()

最佳答案

由于没有太多关于如何使用 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)

关于python - OpenCv CreateImage 函数不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com