Reputation: 1006
I am new to Python OpenCV image processing. I want to remove the border/outline shadow of images as shown below. I checked 'how to remove shadow from scanned images' which does not work for me. Is this even possible?
Upvotes: 4
Views: 4213
Reputation: 2181
Your problem of border/outline shadows reminded me of the vignette filter. You can have a look at this question if you want to know more about it. So essentially our task to remove the effect of the vignette filter and then increase brightness.
#####VIGNETTE
import cv2
import numpy as np
img = cv2.imread('Paris.jpg')
height, width = img.shape[:2]
original = img.copy()
# generating vignette mask using Gaussian kernels
kernel_x = cv2.getGaussianKernel(width, 150)
kernel_y = cv2.getGaussianKernel(height, 150)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)
# applying the mask to each channel in the input image
for i in range(3):
img[:, :, i] = img[:, :, i] * mask
cv2.imshow('Original', original)
cv2.imshow('Vignette', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
To counter the effect change img[:, :, i] = img[:, :, i] * mask
to img[:, :, i] = img[:, :, i] / mask
Now we need to increase the brightness of the image. For this, we will convert the image to HSV and increase the values of saturation and value matrices. To know about it in more detail you can refer to this article.
#THE FULL CODE
import cv2
import numpy as np
img = cv2.imread('shadow.jpg')
original = cv2.imread('bright.jpg')
height, width = img.shape[:2]
# generating vignette mask using Gaussian kernels
kernel_x = cv2.getGaussianKernel(width, 150)
kernel_y = cv2.getGaussianKernel(height, 150)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)
test = img.copy()
for i in range(3):
test[:, :, i] = test[:, :, i] / mask
hsv = cv2.cvtColor(test, cv2.COLOR_BGR2HSV)
hsv = np.array(hsv, dtype = np.float64)
hsv[:,:,1] = hsv[:,:,1]*1.3 ## scale pixel values up or down for channel 1(Lightness)
hsv[:,:,1][hsv[:,:,1]>255] = 255
hsv[:,:,2] = hsv[:,:,2]*1.3 ## scale pixel values up or down for channel 1(Lightness)
hsv[:,:,2][hsv[:,:,2]>255] = 255
hsv = np.array(hsv, dtype = np.uint8)
test = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('Original_bright', original)
cv2.imshow('Original_dark', img)
cv2.imshow('Result', test)
cv2.waitKey(0)
cv2.destroyAllWindows()
The result compared with the original bright image.
How the result would have looked like without the inverse vignette filter.
Upvotes: 5