opencv图像特效之颜色反转

灰度图像进行颜色反转

本质即将对应点的像素值置换为 [255 - 原像素值]

import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\0.jpg", 0)
img_info = img.shape
height = img_info[0]
width = img_info[1]
dst = np.zeros((height, width), np.uint8)
for i in range(height):
    for j in range(width):
        gray_pixel = img[i,j]
        dst[i,j] = 255 - gray_pixel
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()

在这里插入图片描述

彩色图像颜色反转

import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\0.jpg", 1)
img_info = img.shape
height = img_info[0]
width = img_info[1]
dst = np.zeros((height, width,3), np.uint8)
for i in range(height):
    for j in range(width):
        (b,g,r) = img[i,j]
        dst[i,j] = (255-b, 255-g, 255-r)
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.waitKey()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/cyj5201314/article/details/113661982
今日推荐