反转原理:像素值 = 255-当前像素值
主要分为灰度图像反转和彩色图像反转
import cv2import numpy as npimg = cv2.imread('D:/pythonob/imageinpaint/img/zidan.jpg',1)gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)imgInfo = gray.shapeheight = imgInfo[0]width = imgInfo[1]revGray = np.zeros((height,width,1),np.uint8)#灰度图像颜色反转for i in range(0,height): for j in range(0,width): grayPixel = gray[i,j] revGray[i,j] = 255-grayPixelrevColor = np.zeros((height,width,3),np.uint8)#彩色图像颜色反转for i in range(0,height): for j in range(0,width): (b,g,r) = img[i,j] revColor[i,j] = (255-b,255-g,255-r)cv2.imshow('src',img)cv2.imshow('gray',gray)cv2.imshow('reversedGray',revGray)cv2.imshow('reversedColor',revColor)cv2.waitKey(0)