美文网首页
04图片颜色反转

04图片颜色反转

作者: 犬夜叉写作业 | 来源:发表于2019-07-14 14:29 被阅读0次

包括 灰度/彩色 图像颜色反转

1、灰度图像反转

#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)  #转为灰度
dst = np.zeros((height,width,1),np.uint8)  #1为灰色,3为彩色
for i in range(0,height):
    for j in range(0,width):
        grayPixel = gray[i,j]   #每个像素点的灰度值
        dst[i,j] = 255-grayPixel  
cv2.imshow('dst',dst)
cv2.waitKey(0)
image.png

2、彩色图像反转

#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]
dst = 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]
        dst[i,j] = (255-b,255-g,255-r)

cv2.imshow('dst',dst)
cv2.waitKey(0)
image.png

相关文章

网友评论

      本文标题:04图片颜色反转

      本文链接:https://www.haomeiwen.com/subject/tqwbkctx.html