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 modularAdjustment of image brightness and contrast , It's on the skimage Bag exposure Inside the module
1、gamma adjustmentFor 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)
4、 Adjust the intensityOutput is False
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. 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 !