- 为什么使用numpy作图像处理的计算操作?
NumPy是使用Python进行科学计算的基础包,相对于不使用numpy的循环遍历像素操作,numpy的运算效率和速度非常快,所以numpy的基本数组操作需要会,之前学的不够明白,最近看Opencv看到后面觉得有必要重新看一下再做个总结。 - shape操作
shape操作在对图像进行分割和缩小扩大有一定的用处,它可以读取图像的行row、列column、通道数channel。
def img_pixel(image = img):
print(image.shape)
row = img.shape[0]
column = img.shape[1]
channel = img.shape[2]# 彩色图像一般都是3通道
- 创建画布
def create_image():
canvas = np.ones([400, 400, 3], np.uint8)# 创建400*400的画布
canvas[:, :, 1] = np.ones([400, 400]) * 255# 400*400的长和宽
cv2.imshow('canvas', canvas)
canvas列表第一个元素是行,第二个是列,第三个为通道。这里是RGB色彩空间,所以通道二为绿色。
- 矩阵变换
3*3矩阵变换为1维矩阵,类似于三通道转为单通道。
def create_matrix():
matrix = np.ones([3, 3, 1], np.uint8)
matrix.fill(100)
print(matrix)# 3*3矩阵
matrix = matrix.reshape([1, 9])
print(matrix)# 1*9矩阵