前言
图像颜色的反转,比较简单的思路就是使用255减去当前值,从而得到反转后的图像.原始图片:
1.灰度图像的颜色反转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import cv2 import numpy as np # 灰度 0-255 255-当前灰度值 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) for i in range (height): for j in range (width): graypixel = 255 - gray[i, j] dst[i, j] = graypixel cv2.imshow( 'image' , dst) cv2.waitkey( 0 ) |
用255减去当前灰度值,得到反转后的图像.图像如下:
2.bgr图像的反转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 (height): for j in range (width): (b, g, r) = img[i, j] b = 255 - b g = 255 - g r = 255 - r dst[i, j] = (b, g, r) cv2.imshow( 'image' , dst) cv2.waitkey( 0 ) |
bgr图像反转也是一样,同样是使用255减去每一个通道的当前值.效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/missyougoon/article/details/81133070