Image color inversion

Reverse principle: the pixel values ​​of the current pixel value = 255-

It is divided into a grayscale image and a color reversal image reversal

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)

Guess you like

Origin www.cnblogs.com/cxxBoo/p/11454349.html