今天是第二课啦——Numpy数组操作
下面是一些关键代码
import cv2 as cv
import numpy as np
def access_pixels(image): # 遍历图像像素
print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("Height : %s, Width : %s, Channels : %s" % (height, width, channels))
for row in range(height): # 开始遍历
for col in range(width):
for c in range(channels):
pv = image[row, col, c] # 也可以写成 pv = image[row][col][c]
image[row][col][c] = 255 - pv # 这里我的理解是三个通道的每个数值x都进行了运算255-x
cv.imshow("New Image", image)
def inverse(image): # 完成与上个函数相同的功能
dst = cv.bitwise_not(image) # 该函数是直接调用opencv库中的API,速度较自己遍历快许多许多,因为API调用的是C代码
cv.imshow("New image", dst)
def create_image(): # 通过Numpy数组创建图像
img = np.zeros([400, 400, 3], np.uint8) # 创建400*400*3的数值为0的三维数组,相当于一个RGB图像
img[:, :, 0] = np.ones([400, 400]) * 255 # blue, green, red, 255其实表示程度,0-255程度由深至浅,因为np.ones()相当于创建的矩阵的数值为1,":"默认表示所有,添上数值后可以表示赋值范围
cv.imshow("New Image1", img)
i