I am trying to remove raindrops from an image using OpenCV in Python. The image is a cartoon-style outdoor scene with trees, clouds, a pond, and a green landscape. Raindrops are drawn across the scene in white streaks. After applying an inpainting algorithm, the quality of the image decreases, and the pond in the center of the image appears to split into two parts.
I attempted to remove raindrops in my code, but I’m struggling to maintain the quality. I’m seeking help to improve the code and would appreciate assistance with refining it.
import cv2 as cv
import numpy as np
# Load the image
image_path = "C:/Users/kavit/Downloads/RAIN.png" # Update this path to your image file
image = cv.imread(image_path)
# Check if the image is loaded correctly
if image is None:
raise Exception("Error: Could not load the image.")
# Convert the image to grayscale
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# Apply a Gaussian blur to reduce noise and minor raindrop streaks
blurred = cv.GaussianBlur(gray, (5, 5), 0)
# Use adaptive thresholding to detect raindrops with more sensitivity
mask = cv.adaptiveThreshold(blurred, 255, cv.ADAPTIVE_THRESH_MEAN_C,
cv.THRESH_BINARY_INV, 11, 2)
# Combine with Canny edge detection to capture more details of raindrops
edges = cv.Canny(blurred, threshold1=20, threshold2=60)
combined_mask = cv.bitwise_or(mask, edges)
# Apply stronger morphological operations to clean the mask more thoroughly
kernel = np.ones((5, 5), np.uint8) # Increase kernel size
mask_cleaned = cv.dilate(combined_mask, kernel, iterations=5) # More dilation
mask_cleaned = cv.erode(mask_cleaned, kernel, iterations=4) # More erosion
# Use inpainting with a larger radius to cover remaining rain streaks
inpainted_image_ns = cv.inpaint(image, mask_cleaned, 10, cv.INPAINT_NS)
inpainted_image_telea = cv.inpaint(image, mask_cleaned, 15, cv.INPAINT_TELEA)
# Blend the two inpainted results for more comprehensive raindrop removal
inpainted_image = cv.addWeighted(inpainted_image_ns, 0.5, inpainted_image_telea, 0.5, 0)
# Apply selective sharpening to restore some of the lost quality
sharpen_kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
sharpened_image = cv.filter2D(inpainted_image, -1, sharpen_kernel)
# Display the results
cv.imshow('Original Image with Raindrops', image)
cv.imshow('Refined Raindrop Mask', mask_cleaned)
cv.imshow('Final Image without Raindrops', sharpened_image)
# Wait until a key is pressed to close the windows
cv.waitKey(0)
cv.destroyAllWindows()
# Save the result to your desired path
cv.imwrite("C:/Users/kavit/Downloads/raindrop_removed_final_improved_v2.png", sharpened_image)
1