python图像处理:图像线性灰度变换

图像线性灰度变换

原理

在这里插入图片描述
DB为变换后的灰度值,DA为变换前的灰度值,a为缩放率,b为截距。

ab含义
10原有灰度
1!0灰度上移/下移
-1255灰度反转
>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)
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值