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()

运行结果:

OpenCV实现图片亮度增强或减弱

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()

运行结果:

OpenCV实现图片亮度增强或减弱

图片亮度减弱

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()

运行结果:

OpenCV实现图片亮度增强或减弱

来源:https://blog.csdn.net/qq_44989881/article/details/117163026

标签:OpenCV,图片亮度
0
投稿

猜你喜欢

  • ASP文件中的安全问题

    2011-04-14 11:15:00
  • python 爬取小说并下载的示例

    2023-08-24 19:31:49
  • 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
  • python 类的继承 实例方法.静态方法.类方法的代码解析

    2021-12-19 14:19:34
  • TensorFlow中关于tf.app.flags命令行参数解析模块

    2021-10-17 03:40:40
  • PyTorch策略梯度算法详情

    2022-12-20 14:35:12
  • asp制作验证码的方法

    2008-05-08 12:50:00
  • Python代码调试的几种方法总结

    2022-06-14 18:21:27
  • PHP实现的微信公众号扫码模拟登录功能示例

    2023-11-22 18:13:08
  • python批量修改交换机密码的示例

    2023-06-29 07:52:42
  • pygame实现方块动画实例讲解

    2022-11-01 13:37:46
  • Python使用APScheduler实现定时任务过程解析

    2023-01-23 19:20:35
  • Script 元素 type 属性的妙用

    2011-03-07 16:13:00
  • python非单一.py文件用Pyinstaller打包发布成exe

    2021-12-19 00:40:05
  • 支持中文的Len, Left, Right函数

    2008-03-18 13:50:00
  • Python实现变声器功能(萝莉音御姐音)

    2023-04-12 23:04:16
  • asp之家 网络编程 m.aspxhome.com