Ask Your Question
0

How to change brightness of an image? (Increase or Decrease)

asked 2018-06-07 08:11:57 -0600

Santhosh1 gravatar image

I want to basically reduce brightness of an image

Method 1 source

mImage = cv2.imread('image.jpg')

hsvImg = cv2.cvtColor(mImage,cv2.COLOR_BGR2HSV)

value = 90

vValue = hsvImg[...,2]
hsvImg[...,2]=np.where((255-vValue)<value,255,vValue+value)

plt.subplot(111), plt.imshow(cv2.cvtColor(hsvImg,cv2.COLOR_HSV2RGB))
plt.title('test'), plt.xticks([]), plt.yticks([])
plt.show()

which gives a very weird bright effect

Method 2 Source answer in comments of the correct answer

image= cv2.add(image,np.array([50.0]))

Suppose to increase the brightness of the image by 50% have no idea how.

Method 3 source Using gamma

invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
    for i in np.arange(0, 256)]).astype("uint8")

# apply gamma correction using the lookup table
cv2.LUT(image, table)

Method 4 source

Changing the contrast and brightness of an image, in the documentation is in C++ couldn't replecate or should be same as Method 3 I have no idea

According to Gamma vs brightness - any difference?

They two distinct terms.

Method 2 and Method 3 seems to work even when reducing the light intensity

Which method will not wash out the colours and only reduce the intensity of light is what I would like to know?

edit retag flag offensive close merge delete

Comments

what if you just multiply it with some factor, like

img * 0.8

???

berak gravatar imageberak ( 2018-06-07 08:23:43 -0600 )edit

@berak I am working on a wide range of real world images hard to control the exposer so 0.8 seems to have no effect very similar to 1.0

Santhosh1 gravatar imageSanthosh1 ( 2018-06-07 08:57:51 -0600 )edit

yea, sorry. the 1st question should have been: why are you doing this ? what is the context ?

berak gravatar imageberak ( 2018-06-07 09:06:28 -0600 )edit
1

@berak Since the colour were getting washed out from the images on decreasing the brightness, I tried to enhance the colour in that particular image. The result were good, but I need to find a way to enhance colour of an image to make it more vibrant. I will put up a different question for this

Santhosh1 gravatar imageSanthosh1 ( 2018-06-08 01:50:12 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2018-06-08 04:48:04 -0600

Santhosh1 gravatar image

Increasing the value is tricky and can be done in any of the above mentioned methods depending on the application it is being used in.

To decrease the brightness is still much easy, just changing the V channel by a factor less than 1.0

Code

mImage = cv2.imread('image1.jpg')

hsvImg = cv2.cvtColor(mImage,cv2.COLOR_BGR2HSV)

# decreasing the V channel by a factor from the original
hsvImg[...,2] = hsvImg[...,2]*0.6

plt.subplot(111), plt.imshow(cv2.cvtColor(hsvImg,cv2.COLOR_HSV2RGB))
plt.title('brightened image'), plt.xticks([]), plt.yticks([])
plt.show()
edit flag offensive delete link more
0

answered 2018-06-07 09:10:15 -0600

kbarni gravatar image

updated 2018-06-07 09:11:13 -0600

Gamma and brightness are not the same operations. Brightness is a linear operation, gamma is nonlinear; gamma affects only the grays.

However I think there are several different operations to change the brightness of an image:

  1. Add or substract a constant. Take care of the overflow (values above 255 must be truncated to 255; negative values to 0). This is basically your method 2.

    A variant would be to work in the HSV color space and change the V channel, then go back to RGB (your method 1).

    The drawback of this method is that it clips the image to 0 or 255.

  2. Multiply the image with a constant (between 0 and 2). This is similar to berak's suggestion, but the formula is a bit different for lightening.

    The algorithm is: if the value<1 (darken), then image_out=image_in*value

    If value>1 (lighten) then v2=2-value and image_out=image_in*v2+(1-v2)*255

    This method won't clip the image. Most programs use this method.

For all these operations you can use direct operations or color lookup (creating LUTs). I didn't test which solution is faster.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-06-07 08:11:57 -0600

Seen: 27,062 times

Last updated: Jun 08 '18