灰度变换、图像反转、幂律变换、对比度变换
import cv2
import numpy as np
OriginalImg = cv2.imread('cns.jpg')
R ,G, B = cv2.split(OriginalImg)
# print(R.shape) # print(G.shape) # print(B.shape)
#灰度变换
NewImg = R*0.299 + G*0.587 + B*0.114
NewImg1=np.uint8(NewImg)
print(NewImg1.max())
print(NewImg1.min())
#图像反转
NewImg2 = 255-NewImg1
#伽马变换 uint8数据整形化
NewImg3 = np.uint8(((NewImg1/NewImg1.max())**0.4)*NewImg1.max())#必须先将灰度值收缩到0-1之间再进行幂律变换
#对比度拉伸
min = 100
max = 200
temp = np.zeros((NewImg1.shape[0],NewImg1.shape[0]))
for i in range(NewImg1.shape[0]):
for j in range(NewImg1.shape[1]):
if NewImg1[i,j] < min:
temp[i,j] = 0
elif NewImg1[i,j] > max:
temp[i,j] = 254
else:
temp[i,j] = (254/(max-min))*(NewImg1[i,j]-230)+254
# print(temp)
# print(NewImg1)
cv2.namedWindow('OriginalImg',0)#新建一个图形显示窗口,0表示可按规则缩放图片
cv2.imshow('OriginalImg',OriginalImg)
cv2.namedWindow('NewImg',0)
cv2.resizeWindow('NewImg',(280,280))
cv2.imshow('NewImg',NewImg1)
cv2.namedWindow('NewImg2',0)
cv2.resizeWindow('NewImg2',(280,280))
cv2.imshow('NewImg2',NewImg2)
cv2.namedWindow('NewImg3',0)
cv2.resizeWindow('NewImg3',(280,280))
cv2.imshow('NewImg3',NewImg3)
cv2.namedWindow('NewImg4',0)
cv2.resizeWindow('NewImg4',(280,280))
cv2.imshow('NewImg4',np.uint8(temp))
cv2.waitKey(0)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1e767a47c224794d3bf2f77226886ee0.png#pic_center)