python數(shù)字圖像處理之對(duì)比度與亮度調(diào)整示例
skimage包的exposure模塊
圖像亮度與對(duì)比度的調(diào)整,是放在skimage包的exposure模塊里面
1、gamma調(diào)整
對(duì)原圖像的像素,進(jìn)行冪運(yùn)算,得到新的像素值。公式中的g就是gamma值。
如果gamma>1, 新圖像比原圖像暗
如果gamma<1,新圖像比原圖像亮
函數(shù)格式為:skimage.exposure.adjust_gamma(image, gamma=1)
gamma參數(shù)默認(rèn)為1,原像不發(fā)生變化 。
from skimage import data, exposure, img_as_float import matplotlib.pyplot as plt image = img_as_float(data.moon()) gam1= exposure.adjust_gamma(image, 2) #調(diào)暗 gam2= exposure.adjust_gamma(image, 0.5) #調(diào)亮 plt.figure('adjust_gamma',figsize=(8,8)) plt.subplot(131) plt.title('origin image') plt.imshow(image,plt.cm.gray) plt.axis('off') plt.subplot(132) plt.title('gamma=2') plt.imshow(gam1,plt.cm.gray) plt.axis('off') plt.subplot(133) plt.title('gamma=0.5') plt.imshow(gam2,plt.cm.gray) plt.axis('off') plt.show()
2、log對(duì)數(shù)調(diào)整
這個(gè)剛好和gamma相反
原理:I=log(I)
from skimage import data, exposure, img_as_float import matplotlib.pyplot as plt image = img_as_float(data.moon()) gam1= exposure.adjust_log(image) #對(duì)數(shù)調(diào)整 plt.figure('adjust_gamma',figsize=(8,8)) plt.subplot(121) plt.title('origin image') plt.imshow(image,plt.cm.gray) plt.axis('off') plt.subplot(122) plt.title('log') plt.imshow(gam1,plt.cm.gray) plt.axis('off') plt.show()
3、判斷圖像對(duì)比度是否偏低
函數(shù):is_low_contrast(img)
返回一個(gè)bool型值
from skimage import data, exposure image =data.moon() result=exposure.is_low_contrast(image) print(result)
輸出為False
4、調(diào)整強(qiáng)度
函數(shù):
skimage.exposure.rescale_intensity(image, in_range='image', out_range='dtype')
in_range 表示輸入圖片的強(qiáng)度范圍,默認(rèn)為'image', 表示用圖像的最大/最小像素值作為范圍
out_range 表示輸出圖片的強(qiáng)度范圍,默認(rèn)為'dype', 表示用圖像的類型的最大/最小值作為范圍
默認(rèn)情況下,輸入圖片的[min,max]范圍被拉伸到[dtype.min, dtype.max],如果
dtype=uint8, 那么dtype.min=0, dtype.max=255
import numpy as np from skimage import exposure image = np.array([51, 102, 153], dtype=np.uint8) mat=exposure.rescale_intensity(image) print(mat)
輸出為[ 0 127 255]
即像素最小值由51變?yōu)?,最大值由153變?yōu)?55,整體進(jìn)行了拉伸,但是數(shù)據(jù)類型沒有變,還是uint8
前面我們講過,可以通過img_as_float()函數(shù)將unit8類型轉(zhuǎn)換為float型,實(shí)際上還有更簡單的方法,就是乘以1.0
import numpy as np image = np.array([51, 102, 153], dtype=np.uint8) print(image*1.0)
即由[51,102,153]變成了[ 51. 102. 153.]
而float類型的范圍是[0,1],因此對(duì)float進(jìn)行rescale_intensity 調(diào)整后,范圍變?yōu)閇0,1],而不是[0,255]
import numpy as np from skimage import exposure image = np.array([51, 102, 153], dtype=np.uint8) tmp=image*1.0 mat=exposure.rescale_intensity(tmp) print(mat)
結(jié)果為[ 0. 0.5 1. ]
如果原始像素值不想被拉伸,只是等比例縮小,就使用in_range參數(shù),如:
import numpy as np from skimage import exposure image = np.array([51, 102, 153], dtype=np.uint8) tmp=image*1.0 mat=exposure.rescale_intensity(tmp,in_range=(0,255)) print(mat)
輸出為:[ 0.2 0.4 0.6],即原像素值除以255
如果參數(shù)in_range的[main,max]范圍要比原始像素值的范圍[min,max] 大或者小,那就進(jìn)行裁剪,如:
mat=exposure.rescale_intensity(tmp,in_range=(0,102)) print(mat)
輸出[ 0.5 1. 1. ],即原像素值除以102,超出1的變?yōu)?
如果一個(gè)數(shù)組里面有負(fù)數(shù),現(xiàn)在想調(diào)整到正數(shù),就使用out_range參數(shù)。如:
import numpy as np from skimage import exposure image = np.array([-10, 0, 10], dtype=np.int8) mat=exposure.rescale_intensity(image, out_range=(0, 127)) print(mat)
輸出[ 0 63 127]
以上就是python數(shù)字圖像處理之對(duì)比度與亮度調(diào)整示例的詳細(xì)內(nèi)容,更多關(guān)于python數(shù)字圖像對(duì)比度亮度調(diào)整的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實(shí)現(xiàn)刪除列表中某個(gè)元素的3種方法
這篇文章主要介紹了python實(shí)現(xiàn)刪除列表中某個(gè)元素的3種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Python實(shí)現(xiàn)批量識(shí)別銀行卡號(hào)碼以及自動(dòng)寫入Excel表格步驟詳解
這篇文章主要介紹了使用Python實(shí)現(xiàn)高效摸魚,批量識(shí)別銀行卡號(hào)碼并且自動(dòng)寫入Excel表格,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01Python?pywin32實(shí)現(xiàn)word與Excel的處理
這篇文章主要介紹了Python?pywin32實(shí)現(xiàn)word與Excel的處理,pywin32處理Word大多數(shù)用于格式轉(zhuǎn)換,因?yàn)橐话阕x寫操作都可以借助python-docx實(shí)現(xiàn),除非真的有特殊要求,但大部分企業(yè)對(duì)Wrod操作不會(huì)有太多復(fù)雜需求2022-08-08Python基本數(shù)據(jù)結(jié)構(gòu)之字典類型dict用法分析
這篇文章主要介紹了Python基本數(shù)據(jù)結(jié)構(gòu)之字典類型dict用法,結(jié)合實(shí)例形式分析了Python字典類型dict概念、原理、定義及基本使用技巧,需要的朋友可以參考下2019-06-06詳解利用django中間件django.middleware.csrf.CsrfViewMiddleware防止csrf
這篇文章主要介紹了詳解利用django中間件django.middleware.csrf.CsrfViewMiddleware防止csrf攻擊,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10