OpenCV實(shí)現(xiàn)圖片亮度增強(qiáng)或減弱
本文實(shí)例為大家分享了OpenCV實(shí)現(xiàn)圖片亮度增強(qiáng)或減弱的具體代碼,供大家參考,具體內(nèi)容如下
對(duì)每個(gè)像素點(diǎn)的三通道值進(jìn)行同步放大,同時(shí)保持通道值在0-255之間
將圖像中的像素限制在最小值和最大值之間,超過此區(qū)間的值賦值為最小值或最大值
圖片亮度增強(qiáng)
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) ?# 設(shè)置繪圖區(qū)域的大小和像素
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(dst)
plt.show()運(yùn)行結(jié)果:

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) ?# 設(shè)置繪圖區(qū)域的大小和像素
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(dst)
plt.show()運(yùn)行結(jié)果:

圖片亮度減弱
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) ?# 設(shè)置繪圖區(qū)域的大小和像素
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(dst)
plt.show()運(yùn)行結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程
Pexpect是一個(gè)純Python模塊,可以用來和ssh、ftp、passwd、telnet等命令行命令進(jìn)行交互使用,在Linux系統(tǒng)下尤其好用,下面我們就來具體來看一下Python安裝使用命令行交互模塊pexpect的基礎(chǔ)教程:2016-05-05
pytest自動(dòng)化測(cè)試數(shù)據(jù)驅(qū)動(dòng)yaml/excel/csv/json
這篇文章主要為大家介紹了pytest自動(dòng)化測(cè)試數(shù)據(jù)驅(qū)動(dòng)yaml/excel/csv/json的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python實(shí)現(xiàn)向好友發(fā)送微信消息
利用python可以實(shí)現(xiàn)微信消息發(fā)送功能,怎么實(shí)現(xiàn)呢?你肯定會(huì)想著很復(fù)雜,但是python的好處就是很多人已經(jīng)把接口打包做好了,只需要調(diào)用即可,今天通過本文給大家分享使用?Python?實(shí)現(xiàn)微信消息發(fā)送的思路代碼,一起看看吧2022-06-06
Python中面向?qū)ο竽銘?yīng)該知道的一下知識(shí)
這篇文章主要介紹了Python中面向?qū)ο竽銘?yīng)該知道的一下知識(shí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python 實(shí)現(xiàn)二叉查找樹的示例代碼
這篇文章主要介紹了Python 實(shí)現(xiàn)二叉查找樹的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
python?tkinter實(shí)現(xiàn)彈窗的輸入輸出
這篇文章主要為大家詳細(xì)介紹了python?tkinter實(shí)現(xiàn)彈窗的輸入輸出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
windows10 pycharm下安裝pyltp庫和加載模型實(shí)現(xiàn)語義角色標(biāo)注的示例代碼
這篇文章主要介紹了windows10 pycharm下安裝pyltp庫和加載模型實(shí)現(xiàn)語義角色標(biāo)注,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
利用Python?NumPy庫及Matplotlib庫繪制數(shù)學(xué)函數(shù)圖像
最近開始學(xué)習(xí)數(shù)學(xué)了,有一些題目的函數(shù)圖像非常有特點(diǎn),下面這篇文章主要給大家介紹了關(guān)于利用Python?NumPy庫及Matplotlib庫繪制數(shù)學(xué)函數(shù)圖像的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

