原理
DB为变换后的灰度值,DA为变换前的灰度值,a为缩放率,b为截距。
a | b | 含义 |
---|---|---|
1 | 0 | 原有灰度 |
1 | !0 | 灰度上移/下移 |
-1 | 255 | 灰度反转 |
>1 | 增强对比度 | |
0<a<1 | 减小对比度 | |
<0 | 反色(底片效果) |
效果
代码
img = cv2.imread('Panda.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
rows,cols = img_gray.shape
#50
matrix_50 = np.ones((rows,cols),dtype=np.uint8)*50
img_gray1 = cv2.add(img_gray,matrix_50)
img_gray2 = img_gray*1.5
img_gray3 = img_gray*0.7
img_gray4 = -img_gray+256
img_gray5 = cv2.subtract(img_gray,matrix_50)
plt.subplot(231)
plt.imshow(img_gray,'gray',vmin=0,vmax=255)
plt.title('原图')
plt.subplot(232)
plt.imshow(img_gray1,'gray',vmin=0,vmax=255)
plt.title('a=1,b=50')
plt.subplot(233)
plt.imshow(img_gray5,'gray',vmin=0,vmax=255)
plt.title('a=1,b=-50')
plt.subplot(234)
plt.imshow(img_gray2,'gray',vmin=0,vmax=255)
plt.title('a=1.5,b=0')
plt.subplot(235)
plt.imshow(img_gray3,'gray',vmin=0,vmax=255)
plt.title('a=0.7,b=0')
plt.subplot(236)
plt.imshow(img_gray4,'gray',vmin=0,vmax=255)
plt.title('a=-1,b=255')
plt.show()
print(img_gray4)