OpenCV實(shí)現(xiàn)圖片亮度增強(qiáng)或減弱
本文實(shí)例為大家分享了OpenCV實(shí)現(xiàn)圖片亮度增強(qiáng)或減弱的具體代碼,供大家參考,具體內(nèi)容如下
對(duì)每個(gè)像素點(diǎn)的三通道值進(jìn)行同步放大,同時(shí)保持通道值在0-255之間
將圖像中的像素限制在最小值和最大值之間,超過(guò)此區(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實(shí)現(xiàn)圖像識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖像識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Python的ORM框架中SQLAlchemy庫(kù)的查詢操作的教程
這篇文章主要介紹了Python的ORM框架中SQLAlchemy庫(kù)的查詢操作的教程,SQLAlchemy用來(lái)操作數(shù)據(jù)庫(kù)十分方便,需要的朋友可以參考下2015-04-04
Python基于callable函數(shù)檢測(cè)對(duì)象是否可被調(diào)用
這篇文章主要介紹了Python基于callable函數(shù)檢測(cè)對(duì)象是否可被調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Python編程實(shí)現(xiàn)的圖片識(shí)別功能示例
這篇文章主要介紹了Python編程實(shí)現(xiàn)的圖片識(shí)別功能,涉及Python PIL模塊的安裝與使用技巧,需要的朋友可以參考下2017-08-08
Python字符串處理實(shí)現(xiàn)單詞反轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Python字符串處理實(shí)現(xiàn)單詞反轉(zhuǎn)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

