python调节图像明暗亮度
彩色和灰度图像明暗调节解决方案
函数
cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst
参数说明
src1 – first input array.
alpha – weight of the first array elements.
src2 – second input array of the same size and channel number as src1.
beta – weight of the second array elements.
dst – output array that has the same size and number of channels as the input arrays.
gamma – scalar added to each sum.
dtype – optional depth of the output array; when both input arrays have the same depth, dtype can be set to -1, which will be equivalent to src1.depth().
import cv2
import numpy as np
import matplotlib.pyplot as plt
def imgBrightness(img1, c, b):
rows, cols, channels = img1.shape
blank = np.zeros([rows, cols, channels], img1.dtype)
rst = cv2.addWeighted(img1, c, blank, 1-c, b)
return rst
img = cv2.imread('Maldives.jpeg')
dst = imgBrightness(img, 0.5, 0)
dst2 = imgBrightness(img, 1.5, 0)
cv2.namedWindow("origin",0);
cv2.resizeWindow("origin", 640, 480)
cv2.imshow('origin',img)
cv2.namedWindow("enhanced",0);
cv2.resizeWindow("enhanced", 640, 480)
cv2.imshow('enhanced',dst)
cv2.namedWindow("enhanced2",0);
cv2.resizeWindow("enhanced2", 640, 480)
cv2.imshow('enhanced2',dst2)
函数说明
adjust_gamma
skimage.exposure.
adjust_gamma
(image, gamma=1, gain=1)
Performs Gamma Correction on the input image.
Also known as Power Law Transform. This function transforms the input image pixelwise according to the equation O = I**gamma
after scaling each pixel to the range 0 to 1.
Parameters
image ndarray
Input image.
gamma float, optional
Non negative real number. Default value is 1.
gain float, optional
The constant multiplier. Default value is 1.
Returns
out ndarray
Gamma corrected output image.
Notes
For gamma greater than 1, the histogram will shift towards left and the output image will be darker than the input image.
For gamma less than 1, the histogram will shift towards right and the output image will be brighter than the input image.
from skimage import exposure
img = np.load("npy")
gam1= exposure.adjust_gamma(img, 1.5) #调暗
gam2= exposure.adjust_gamma(img, 0.5) #调亮
plt.figure()
plt.imshow(img,plt.cm.gray)
plt.figure()
plt.imshow(gam1,plt.cm.gray)
plt.figure()
plt.imshow(gam2,plt.cm.gray)
文章合集请关注公众号:未名方略
更多推荐
所有评论(0)