图像颜色反转

反转原理:像素值 = 255-当前像素值

主要分为灰度图像反转和彩色图像反转

import cv2
import numpy as np
img = cv2.imread('D:/pythonob/imageinpaint/img/zidan.jpg',1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgInfo = gray.shape
height = 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-grayPixel
revColor = 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)

效果图:

 

转载于:https://www.cnblogs.com/cxxBoo/p/11454349.html

你可能感兴趣的:(图像颜色反转)