lundi 20 mars 2023

How to remove reflections from the water surface?

I tried to convert to HSV space and define a range of values for the color of the reflection. Then I create the binary mask with the pixels within the range (Fig. 2), but this has a problem, since it segments also a part of the trees. How could I segment only the reflection of the water surface?. enter image description hereFig. 1. Original image with reflection

enter image description here

Fig. 2. Mask

Finally, I remove the reflections using the inpaint function, but this also has a problem as it does not restore correctly (Fig. 3). enter image description hereFig. 3. Without reflection

Here is the complete code:

import cv2
from PIL import Image, ImageEnhance

# Read image using OpenCV
img = cv2.imread("0336-CH.jpg")

# Convert image from RGB to HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Define range of values for the reflection color
umbral_bajo = (25, 0, 100) 
umbral_alto = (40, 50, 255) 

# Create binary mask with pixels within range
mask = cv2.inRange(img_hsv, umbral_bajo, umbral_alto)

# Removing reflections using the inpaint function
img_noreflect = cv2.inpaint(img, mask, 20, cv2.INPAINT_TELEA)

# Converting the OpenCV image to PIL
img_pil = Image.fromarray(img_noreflect)

# Create an object to control contrast with PIL
contrast = ImageEnhance.Contrast(img_pil)

# Applying a contrast factor of 1.5 to the image with PIL
img_pil_contrast = contrast.enhance(1.5)

# Create an object to control saturation with PIL
color = ImageEnhance.Color(img_pil_contrast)

# Applying a saturation factor of 1.2 to the image with PIL
img_pil_saturation = color.enhance(1.2)

# Converting the PIL image to OpenCV
img_opencv = np.array(img_pil_saturation)




Aucun commentaire:

Enregistrer un commentaire