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(imagegamma=1gain=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)

文章合集请关注公众号:未名方略

 

 

  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞行codes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值