Home > Blockchain >  How to affect a certain region's brightness by a certain scalar in opencv with python?
How to affect a certain region's brightness by a certain scalar in opencv with python?

Time:11-17

I have this image attached to this description. I want to select a region anywhere on the image and increase its brightness without affecting the other parts of the image. How to handle this situation in python OpenCV.

The brightness of a region in a black rectangle should be affected by a certain scalar.

Any idea. Input Image Output Image

CodePudding user response:

Here is one way to do that in Python/OpenCV.

Crop the region. Increase its brightness. Then put the modified region back into the image.

Input:

enter image description here

import cv2
import numpy as np

# load image
img = cv2.imread('orange_flower.jpg')

# specify region
x,y,w,h = 480,183,163,115

# crop region
crop = img[y:y h, x:x w]

# increase brightness of crop
gain = 1.5
crop = (gain * crop.astype(np.float64)).clip(0,255).astype(np.uint8)

# put crop back into input
result = img.copy()
result[y:y h, x:x w] = crop

# save output
cv2.imwrite('orange_flower_result.jpg', result)

# Display various images to see the steps
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

enter image description here

  • Related