Python,OpenCV:增加图像亮度而不会溢出UINT8数组 [英] Python, OpenCV: Increasing image brightness without overflowing UINT8 array

查看:1515
本文介绍了Python,OpenCV:增加图像亮度而不会溢出UINT8数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试增加灰度图像的亮度。 cv2.imread()返回一个numpy数组。我正在为数组的每个元素添加整数值。从理论上讲,这会增加每一个。之后,我可以将上限阈值设置为255并获得更高亮度的图像。



以下是代码:

  gray = cv2.imread(路径+文件,0)

打印类型(灰色)

打印灰色[0]

new =灰色+价值

打印新[0]

res = np.hstack((灰色,新))

cv2.imshow('image',res)
cv2.waitKey(0)
cv2.destroyAllWindows()

然而,内部OpenCV例程显然是这样的:

  new_array = old_array%255 

每个高于255的像素强度值将成为除以255的余数。



结果,我变暗了而不是完全变白。



这是输出:

 < type'numpy.ndarray'> 
[115 114 121 ...,170 169 167]
[215 214 221 ...,14 13 11]

以下是图片:





如何关闭此余数机制?有没有更好的方法来提高OpenCV的亮度?

解决方案

一个想法就是在添加之前检查一下value 通过检查 255 与当前像素值之间的差异并检查它是否在值。如果是,我们不会添加,我们会直接在 255 设置,否则我们会这样做加成。现在,这个决策可以通过掩模创建来缓解,并且将是 -

  mask =(255  -  gray)< value 

然后,将此掩码/布尔数组提供给


I am trying to increase brightness of a grayscale image. cv2.imread() returns a numpy array. I am adding integer value to every element of the array. Theoretically, this would increase each of them. After that I would be able to put upper threshold of 255 and get the image with the higher brightness.

Here is the code:

grey = cv2.imread(path+file,0)

print type(grey)

print grey[0]

new = grey + value

print new[0]

res = np.hstack((grey, new))

cv2.imshow('image', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

However, internal OpenCV routine apparently does something like that:

new_array = old_array % 255

Every pixel intensity value higher than 255 becomes a remainder of dividing by 255.

As a result, I am getting dark instead of completely white.

Here is the output:

<type 'numpy.ndarray'>
[115 114 121 ..., 170 169 167]
[215 214 221 ...,  14  13  11]

And here is the image:

How can I switch off this remainder mechanism? Is there any better way to increase brightness in OpenCV?

解决方案

One idea would be to check before adding value whether the addition would result in an overflow by checking the difference between 255 and the current pixel value and checking if it's within value. If it does, we won't add value, we would directly set those at 255, otherwise we would do the addition. Now, this decision making could be eased up with a mask creation and would be -

mask = (255 - grey) < value

Then, feed this mask/boolean array to np.where to let it choose between 255 and grey+value based on the mask.

Thus, finally we would have the implementation as -

grey_new = np.where((255 - grey) < value,255,grey+value)

Sample run

Let's use a small representative example to demonstrate the steps.

In [340]: grey
Out[340]: 
array([[125, 212, 104, 180, 244],
       [105,  26, 132, 145, 157],
       [126, 230, 225, 204,  91],
       [226, 181,  43, 122, 125]], dtype=uint8)

In [341]: value = 100

In [342]: grey + 100 # Bad results (e.g. look at (0,1))
Out[342]: 
array([[225,  56, 204,  24,  88],
       [205, 126, 232, 245,   1],
       [226,  74,  69,  48, 191],
       [ 70,  25, 143, 222, 225]], dtype=uint8)

In [343]: np.where((255 - grey) < 100,255,grey+value) # Expected results
Out[343]: 
array([[225, 255, 204, 255, 255],
       [205, 126, 232, 245, 255],
       [226, 255, 255, 255, 191],
       [255, 255, 143, 222, 225]], dtype=uint8)

Testing on sample image

Using the sample image posted in the question to give us arr and using value as 50, we would have -

这篇关于Python,OpenCV:增加图像亮度而不会溢出UINT8数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆