Python武器库 - 科研中常用的python图像操作 - 创建纯色图像
应用场景:需要创建纯色图像,作为背景图,
在此基础上添加文字、形状、新的图片等等
原理:
-
cv2的读取图片操作本质上是将图片转换为
uint8的numpy.ndarray类型,
后续的其他图像操作,本质上也是对于这个ndarray对象的操作
![]()
-
首先用
numpy创建一个形状为(224,224,3)、元素值为均为0的ndarray对象,
这个ndarray其实对应一张纯黑的图像,
img_black = np.zeros((256,256,3), np.uint8)
然后选择一个BGR值(因为opencv默认采用BGR颜色通道模式),
对于该ndarray前两维每个元素的三通道,均赋这个值,
img_black[:] = [60,20,220]
代码示例:
import numpy as np
import cv2
img_black = np.zeros((256,256,3), np.uint8)
cv2.imshow('lingdu show img_black', img_black)
img_black[:] = [60,20,220] # BGR值(Crimson猩红色)
cv2.imshow('lingdu show img_color', img_black)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
运行效果:


浙公网安备 33010602011771号