程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Example of contrast and brightness adjustment in Python digital image processing

編輯:Python

Catalog

skimage Bag exposure modular

1、gamma adjustment

2、log Logarithmic adjustment

3、 Judge whether the image contrast is low

4、 Adjust the intensity

skimage Bag exposure modular

Adjustment of image brightness and contrast , It's on the skimage Bag exposure Inside the module

1、gamma adjustment

For the pixels of the original image , Perform power operation , Get the new pixel value . Formula g Namely gamma value .

If gamma>1, The new image is darker than the original image

If gamma<1, The new image is brighter than the original image

The function format is :skimage.exposure.adjust_gamma(image, gamma=1)

gamma The parameter defaults to 1, The original image does not change .

from skimage import data, exposure, img_as_floatimport matplotlib.pyplot as pltimage = img_as_float(data.moon())gam1= exposure.adjust_gamma(image, 2) # Dimming gam2= exposure.adjust_gamma(image, 0.5) # Dimming 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 Logarithmic adjustment

This is just like gamma contrary

principle :I=log(I)

from skimage import data, exposure, img_as_floatimport matplotlib.pyplot as pltimage = img_as_float(data.moon())gam1= exposure.adjust_log(image) # Logarithmic adjustment 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、 Judge whether the image contrast is low

function :is_low_contrast(img)

Return to one bool Type value

from skimage import data, exposureimage =data.moon()result=exposure.is_low_contrast(image)print(result)

Output is False

4、 Adjust the intensity

function :

skimage.exposure.rescale_intensity(image, in_range='image', out_range='dtype')

in_range Indicates the intensity range of the input picture , The default is 'image', Represents the maximum size of the image / The minimum pixel value is used as the range

out_range Indicates the intensity range of the output picture , The default is 'dype', Represents the maximum of the type of image used / The minimum value is used as the range

By default , Enter the name of the picture [min,max] The range is stretched to [dtype.min, dtype.max], If

dtype=uint8, that dtype.min=0, dtype.max=255

import numpy as npfrom skimage import exposureimage = np.array([51, 102, 153], dtype=np.uint8)mat=exposure.rescale_intensity(image)print(mat)

Output is [  0 127 255]

That is, the minimum pixel value is determined by 51 Turn into 0, The maximum value is determined by 153 Turn into 255, The whole is stretched , But the data type has not changed , still uint8

We talked about , Can pass img_as_float() Function will unit8 Type conversion to float type , There are actually simpler ways , It's times 1.0

import numpy as npimage = np.array([51, 102, 153], dtype=np.uint8)print(image*1.0)

by [51,102,153] Turned into [  51.  102.  153.]

and float The range of types is [0,1], So right. float Conduct rescale_intensity After the adjustment , Range becomes [0,1], instead of [0,255]

import numpy as npfrom skimage import exposureimage = np.array([51, 102, 153], dtype=np.uint8)tmp=image*1.0mat=exposure.rescale_intensity(tmp)print(mat)

The result is [ 0. &nbsp; 0.5  1. ]

If the original pixel value does not want to be stretched , Just wait for the scale to shrink , Just use in_range Parameters , Such as :

import numpy as npfrom skimage import exposureimage = np.array([51, 102, 153], dtype=np.uint8)tmp=image*1.0mat=exposure.rescale_intensity(tmp,in_range=(0,255))print(mat)

Output is :[ 0.2  0.4  0.6], That is, the original pixel value is divided by 255

If parameters in_range Of [main,max] The range is larger than the range of the original pixel value [min,max] Big or small , Then cut it , Such as :

mat=exposure.rescale_intensity(tmp,in_range=(0,102))print(mat)

Output [ 0.5  1.   1. ], That is, the original pixel value is divided by 102, beyond 1 Turn into 1

If there are negative numbers in an array , Now I want to adjust to a positive number , Just use out_range Parameters . Such as :

import numpy as npfrom skimage import exposureimage = np.array([-10, 0, 10], dtype=np.int8)mat=exposure.rescale_intensity(image, out_range=(0, 127))print(mat)

Output [  0  63 127]

That's all python Details of an example of contrast and brightness adjustment for digital image processing , More about python For information on digital image contrast brightness adjustment, please pay attention to other relevant articles on software development network !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved