Im trying to brighten and dim an image using OpenCV with two approaches.
Approach 1: Used OpenCV's add
and subtract
functions to brigthen and dim the image.
Approach 2: Manually added/subtracted pixel values.
Both produced different results, with the OpenCV approach producing better results than the manual as you can see in the output image below. Why is this happening?.
Code:
img = cv2.imread('New_Zealand_Coast.jpg',cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Matrix for storing pixel alter values
mat = np.ones(img.shape, dtype='uint8') * 70
# Approach 1
img_brighter = cv2.add(img, mat)
img_dimmer = cv2.subtract(img, mat)
# Approach 2
img_brighter_manual = img + mat
img_dimmer_manual = img - mat
# Plot Approach 1
plt.figure(figsize=(20,6))
plt.subplot(131)
plt.imshow(img)
plt.title('Original 1')
plt.subplot(132)
plt.imshow(img_brighter)
plt.title('Brighter')
plt.subplot(133)
plt.imshow(img_dimmer)
plt.title('Dimmer')
plt.suptitle('Approach 1 : With CV Add/Subtract Function')
# Plot Approach 2
plt.figure(figsize=(20,6))
plt.subplot(131)
plt.imshow(img)
plt.title('Original 1')
plt.subplot(132)
plt.imshow(img_brighter_manual)
plt.title('Brighter')
plt.subplot(133)
plt.imshow(img_dimmer_manual)
plt.title('Dimmer')
plt.suptitle('Approach 2 : With manual Add/Subtract')
Original Image Array:
Modified array from Approach 1:
Modified array from Approach 2:
As you can see, there is some difference in the elements of arrays produced by the two approaches.
Here is the original image used in the code, in case you want to test with it.