OpenCV实现图片亮度增强或减弱
作者:?VVcat? 时间:2022-09-16 00:15:04
本文实例为大家分享了OpenCV实现图片亮度增强或减弱的具体代码,供大家参考,具体内容如下
对每个像素点的三通道值进行同步放大,同时保持通道值在0-255之间
将图像中的像素限制在最小值和最大值之间,超过此区间的值赋值为最小值或最大值
图片亮度增强
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('1.png', 1)
height, width = img.shape[:2]
dst = np.zeros((height, width, 3), np.uint8)
for i in range(0, height):
? ? for j in range(0, width):
? ? ? ? (b, g, r) = img[i, j]
? ? ? ? bb = int(b) + 50
? ? ? ? gg = int(g) + 50
? ? ? ? rr = int(r) + 50
? ? ? ? if bb > 255:
? ? ? ? ? ? bb = 255
? ? ? ? if gg > 255:
? ? ? ? ? ? gg = 255
? ? ? ? if rr > 255:
? ? ? ? ? ? rr = 255
? ? ? ? dst[i, j] = (bb, gg, rr)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(14, 6), dpi=100) ?# 设置绘图区域的大小和像素
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(dst)
plt.show()
运行结果:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('1.png', 1)
height, width = img.shape[:2]
dst = np.zeros((height, width, 3), np.uint8)
for i in range(0, height):
? ? for j in range(0, width):
? ? ? ? (b, g, r) = img[i, j]
? ? ? ? bb = int(b * 1.3) + 10
? ? ? ? gg = int(g * 1.2) + 15
? ? ? ? if bb > 255:
? ? ? ? ? ? bb = 255
? ? ? ? if gg > 255:
? ? ? ? ? ? gg = 255
? ? ? ? dst[i, j] = (bb, gg, r)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(14, 6), dpi=100) ?# 设置绘图区域的大小和像素
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(dst)
plt.show()
运行结果:
图片亮度减弱
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('1.png', 1)
height, width = img.shape[:2]
dst = np.zeros((height, width, 3), np.uint8)
for i in range(0, height):
? ? for j in range(0, width):
? ? ? ? (b, g, r) = img[i, j]
? ? ? ? bb = int(b) - 50
? ? ? ? gg = int(g) - 50
? ? ? ? rr = int(r) - 50
? ? ? ? if bb < 0:
? ? ? ? ? ? bb = 0
? ? ? ? if gg < 0:
? ? ? ? ? ? gg = 0
? ? ? ? if rr < 0:
? ? ? ? ? ? rr = 0
? ? ? ? dst[i, j] = (bb, gg, rr)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(14, 6), dpi=100) ?# 设置绘图区域的大小和像素
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(dst)
plt.show()
运行结果:
来源:https://blog.csdn.net/qq_44989881/article/details/117163026
标签:OpenCV,图片亮度
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP文件中的安全问题
2011-04-14 11:15:00
python 爬取小说并下载的示例
2023-08-24 19:31:49
![](https://img.aspxhome.com/file/2023/2/61962_0s.gif)
asp如何编写一个加法器?
2009-11-08 18:58:00
Python requests timeout的设置
2022-01-27 01:20:40
PHP session有效期session.gc_maxlifetime
2023-11-14 17:01:45
网站重构到底是什么
2008-11-03 11:30:00
女装类视觉设计分享
2009-10-30 18:36:00
![](https://img.aspxhome.com/file/UploadPic/200910/30/1256637795938-73s.jpg)
python 类的继承 实例方法.静态方法.类方法的代码解析
2021-12-19 14:19:34
TensorFlow中关于tf.app.flags命令行参数解析模块
2021-10-17 03:40:40
![](https://img.aspxhome.com/file/2023/6/101146_0s.png)
PyTorch策略梯度算法详情
2022-12-20 14:35:12
![](https://img.aspxhome.com/file/2023/7/86617_0s.png)
asp制作验证码的方法
2008-05-08 12:50:00
Python代码调试的几种方法总结
2022-06-14 18:21:27
![](https://img.aspxhome.com/file/2023/5/71475_0s.jpg)
PHP实现的微信公众号扫码模拟登录功能示例
2023-11-22 18:13:08
python批量修改交换机密码的示例
2023-06-29 07:52:42
pygame实现方块动画实例讲解
2022-11-01 13:37:46
![](https://img.aspxhome.com/file/2023/8/90508_0s.png)
Python使用APScheduler实现定时任务过程解析
2023-01-23 19:20:35
![](https://img.aspxhome.com/file/2023/0/82640_0s.png)
Script 元素 type 属性的妙用
2011-03-07 16:13:00
python非单一.py文件用Pyinstaller打包发布成exe
2021-12-19 00:40:05
![](https://img.aspxhome.com/file/2023/6/85116_0s.jpg)
支持中文的Len, Left, Right函数
2008-03-18 13:50:00
Python实现变声器功能(萝莉音御姐音)
2023-04-12 23:04:16
![](https://img.aspxhome.com/file/2023/0/96980_0s.jpg)