最快的方法?
为了获得更高的处理速度,可以在原始BGR图像上添加正或负整数。但是要使用OpenCV函数来避免溢出。一个好的选择是convertScaleAbs函数。我们使用USC SIPI的参考“狒狒”图像:
![](https://istack.dev59.com/CcJRQ.webp)
import cv2
def fast_brightness(input_image, brightness):
''' input_image: color or grayscale image
brightness: -255 (all black) to +255 (all white)
returns image of same type as input_image but with
brightness adjusted'''
img = input_image.copy()
cv2.convertScaleAbs(img, img, 1, brightness)
return img
img = cv2.imread('mandrill.tiff',cv2.IMREAD_COLOR)
cv2.imwrite('output.jpg', fast_brightness(img, 100))
针对亮度值为100的情况,以下是相关it技术内容的翻译:
![](https://istack.dev59.com/9KaHy.webp)
像Photoshop一样
如果需要更像Photoshop、The Gimp或其他图像处理程序的亮度函数,可以使用类似@md-hanif-ali-sohag 或此答案中的函数:
def photoshop_brightness(input_img, brightness = 0):
''' input_image: color or grayscale image
brightness: -127 (all black) to +127 (all white)
returns image of same type as input_image but with
brightness adjusted
'''
img = input_img.copy()
if brightness != 0:
if brightness > 0:
shadow = brightness
highlight = 255
else:
shadow = 0
highlight = 255 + brightness
alpha_b = (highlight - shadow)/255
gamma_b = shadow
cv2.convertScaleAbs(input_img, img, alpha_b, gamma_b)
return img
时间
我对每个函数运行1,000次进行了计时。令人惊讶的是,两者的时间几乎相同。
elapsed fast_brightness [sec]: 0.8595983982086182
elapsed photoshop_brightness [sec]: 0.8565976619720459
v > lim
?那不是返回一个布尔值吗? - palapapa