#完成图像的颜色反转 #RGB 255-R = newR #灰度图的颜色反转:0-255 255-当前 import cv2 import numpy as np img = cv2.imread('image0.jpg',1) imgInfo = img.shape height = imgInfo[0] width = imgInfo[1] #转化为灰度图片 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #3代表彩色图片,1代表灰色图片,定义空白矩阵,类似画布 dst1 = np.zeros((height,width,1), np.uint8) dst = np.zeros((height,width,3),np.uint8) for i in range(0,height): for j in range(0,width): #灰度图反转 grayPixel = gray[i,j] dst1[i, j] = 255 - grayPixel (b, g, r) = img[i, j] dst[i,j] = (255-b, 255-g, 255-r) #打印彩色图片,灰度图不打印 cv2.imshow('dst', dst) cv2.waitKey(0)