|
发表于 2018-10-12 15:06:04
|
显示全部楼层
以下是如何在Python中使用cv2:
- # Create a blank 300x300 black image
- image = np.zeros((300, 300, 3), np.uint8)
- # Fill image with red color(set each pixel to red)
- image[:] = (0, 0, 255)
复制代码
下面是更完整的示例,了解如何创建填充了某种RGB颜色的新空白图像:
- 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)
复制代码 |
|