[CV] Glare Removal ์๊ณ ๋ฆฌ์ฆ ์ ๋ฆฌ
๐ก ๋ณธ ๋ฌธ์๋ 'Glare Removal ์๊ณ ๋ฆฌ์ฆ ์ ๋ฆฌ'์ ๋ํด ์ ๋ฆฌํด๋์ ๊ธ์ ๋๋ค.
๊ณผ๋ํ๊ณ ์กฐ์ ๋์ง ์๋ ๋ฐ๊ธฐ๋ก ์ธํด ๋ฐ์ํ๋ Glareํ ํ๋ฉด์ ์์์์ ์ ๊ฑฐํ๊ณ ์ถ์ ๊ฒฝ์ฐ๊ฐ ์ข ์ข ์์ต๋๋ค. ์ด๋ฅผ ์ ๊ฑฐํ๋ ๋ฐฉ๋ฒ์ ๊ฐ๋ ๊ณผ ์ฝ๋ ์์ค์ผ๋ก ์์ธํ ์ ๋ฆฌํ์์ผ๋ ์ฐธ๊ณ ํ์๊ธฐ ๋ฐ๋๋๋ค.
1. Glare Removal
Glare?
Glare๋ ๊ณผ๋ํ๊ณ ์กฐ์ ๋์ง ์๋ ๋ฐ๊ธฐ๋ก ์ธํด ๋ฐ์ํ๋ ์๊ฐ์ ๊ฐ๊ฐ์ ๋๋ค. ๊ทธ๊ฒ์ ์ฃผ๊ด์ ์ด๊ณ ๋๋ถ์ฌ์ ๋ํ ๋ฏผ๊ฐ์ฑ์ ๋งค์ฐ ๋ค์ํ ์ ์์ต๋๋ค. ๋ ธ์ธ๋ค์ ๋ณดํต ๋์ ๋ ธํ๋ ํน์ฑ ๋๋ฌธ์ ๋๋ถ์ฌ์ ๋ ๋ฏผ๊ฐํฉ๋๋ค.
Glare Detection
1) Threshold based
์ผ๋ฐ์ ์ผ๋ก Glare๊ฐ ์กด์ฌํ ๋ ํฝ์ ๊ฐ์ด 180๋ณด๋ค ๋๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๋ ์ ์ญ ์ด์งํ๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๊ฒ ์๋ณ ํ ์ ์์ต๋๋ค.
def create_mask(image):
gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY )
blurred = cv2.GaussianBlur( gray, (9,9), 0 )
_,thresh_img = cv2.threshold( blurred, 180, 255, cv2.THRESH_BINARY)
thresh_img = cv2.erode( thresh_img, None, iterations=2 )
thresh_img = cv2.dilate( thresh_img, None, iterations=4 )
# perform a connected component analysis on the thresholded image,
# then initialize a mask to store only the "large" components
# labels = measure.label( thresh_img, neighbors=8, background=0 )
labels = measure.label( thresh_img, connectivity=2, background=0 )
mask = np.zeros( thresh_img.shape, dtype="uint8" )
# loop over the unique components
for label in np.unique( labels ):
# if this is the background label, ignore it
if label == 0:
continue
# otherwise, construct the label mask and count the
# number of pixels
labelMask = np.zeros( thresh_img.shape, dtype="uint8" )
labelMask[labels == label] = 255
numPixels = cv2.countNonZero( labelMask )
# if the number of pixels in the component is sufficiently
# large, then add it to our mask of "large blobs"
if numPixels > 300:
mask = cv2.add( mask, labelMask )
return mask
- ์ด๋ฏธ์ง๋ฅผ ๊ทธ๋ ์ด์ค์ผ์ผ๋ก ๋ณํ
- ๊ฐ์ฐ์์ ํ๋ ฌ(9x9)์ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ ๋ธ๋ฌ๋งํ์ฌ ๋ ธ์ด์ฆ ๊ฐ์
- ๊ธ๋ก๋ฒ ์๊ณ๊ฐ ๋ฐฉ๋ฒ์์ ์๊ณ๊ฐ์ 180์ผ๋ก ์ค์ -> ํฝ์ ๊ฐ์ด 180 ์ด์์ด๋ฉด, ํฐ์
- small blobs of noise ์ ๊ฑฐํ๊ณ ์ ๋ชจํด๋ก์ง ํฝ์ฐฝ, ์นจ์ ์ฐ์ฐ(erosion, dilations) ์ํ
- ๋ชจํด๋ก์ง ํฝ์ฐฝ/์นจ์ ์ฐ์ฐ ๊ด๋ จ ๋ด์ฉ ์ฐธ๊ณ : https://deep-learning-study.tistory.com/226
- Connected Component Labeling: sikit-image ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ด image.measure.labels
- ๋ง์คํฌ(Mask) ์์ฑ: numPixel์ด ๋ฏธ๋ฆฌ ์ ์๋ ์๊ณ๊ฐ(์ด ๊ฒฝ์ฐ, 300 ํฝ์ )์ ์ด๊ณผํ๋ฉด blob์ "์ถฉ๋ถํ ํฌ๋ค"๊ณ ๊ฐ์ฃผํ์ฌ ๋ง์คํฌ๋ก ์ง์
Threshold based Result
Glare Removal Algorithms: Inpainting method
Image Inpainting์ ์ด๋ฏธ์ง์์ ๋๋ฝ๋ ์์ญ์ ์ฌ๊ตฌ์ฑํ๋ ์์ ์ ๋๋ค. ์ด๋ ์ปดํจํฐ ๋น์ ์์ ์ค์ํ ๋ฌธ์ ์ด๋ฉฐ ๊ฐ์ฒด ์ ๊ฑฐ, ์ด๋ฏธ์ง ๋ณต์, ์กฐ์, ์ฌํ์ , ํฉ์ฑ ๋ฐ ์ด๋ฏธ์ง ๊ธฐ๋ฐ ๋ ๋๋ง๊ณผ ๊ฐ์ ๋ง์ ์ด๋ฏธ์ง ๋ฐ ๊ทธ๋ํฝ ์ ํ๋ฆฌ์ผ์ด์ ์์ ํ์์ ์ธ ๊ธฐ๋ฅ์ ๋๋ค.
1) OpenCV: Naiver-Stokes method, Fast Marching method
OpenCV์์๋ Naiver-Stokes method(NS), Fast Marching method(Telea) ๋ฐฉ์์ ์ง์ํฉ๋๋ค. ์๋์์ ํด๋น Method์ ๋ํด ์์ธํ ์ค๋ช ํด๋๊ธด ํ์์ผ๋, CPU ์ฐ์ฐ์ด๋ผ ๊ทธ๋ฐ์ง ๋งค์ฐ ๋๋ฆฌ๋ฉฐ ๊ทธ๋ฆฌ ์ถ์ฒํ์ง๋ ์์ต๋๋ค.
Naiver-Stokes method
Image intensities of the region can be updated using the partial differential equation, and the smoothness of the image can be calculated by the image Laplacian (The Laplacian is a 2-D isotropic measure of the 2nd spatial derivative of an image. The Laplacian of an image highlights regions of rapid intensity change and is therefore often used for edge detection (see zero-crossing edge detectors). The Laplacian is often applied to an image that has first been smoothed with something approximating a Gaussian smoothing filter in order to reduce its sensitivity to noise, and hence the two variants will be described together here. The operator normally takes a single gray-level image as input and produces another gray-level image as output)
Laplacian and partial differential equations can be used to preserve the edges and continue to propagate colour information in smooth regions. This is one of the methods to do image inpainting.
- Ref(Paper): Navier-Stokes, Fluid Dynamics, and Image and Video Inpainting - https://www.math.ucla.edu/~bertozzi/papers/cvpr01.pdf
Fast Marching method
a weighted average over a known image neighbourhood of the pixel is used for image smoothness to inpaint. The known neighbourhood pixels and gradients are used to estimate the colour of the pixel to be inpainted.
- Ref(Paper): An Image Inpainting Technique Based on the Fast Marching Method - https://www.semanticscholar.org/paper/An-Image-Inpainting-Technique-Based-on-the-Fast-Telea/67d0cb47d14150daff08980efbea9f1267d3a4e5
We can use any of the above algorithms to inpaint.
How to use in OpenCV python
dst = cv2.inpaint(src, inpaintMask, inpaintRadius, flags)
- src → The input glared image
- dst → Output image
- inpaintMask →A binary mask indicating pixels to be inpainted.
- inpaintRadius →Neighborhood around a pixel to inpaint.
- flags → INPAINT_NS,(Navier-Stokes based method) or INPAINT_TELEA (Fast marching based method)
์์ ๋ฐ ํ์ฉ
result1 = cv2.inpaint(img, mask, 101, cv2.INPAINT_TELEA)
When we select the inpaintRadius, if the regions to be inpainted are thin, smaller values produce better results (less blurry).
OpenCV Result: Naiver-Stokes method, Fast Marching method
2) Inpainting SOTA: e.g. co-mod-gan
SOTA ๋ ผ๋ฌธ ๋ฐ ์ฝ๋๋ ๋์ฒด๋ก paperwithcode์์ ์ฐพ์๋ณด๋ ค๊ณ ํฉ๋๋ค. Image Inpainting ๋ถ์ผ์ outdoor ํ๊ฒฝ์์์ benchmark์๋ Place2๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ ๊ฒ ๊ฐ๊ณ ์ด ์ค ์ ๋ช ํ๋ฐ ์ฝ๋๊ฐ ์ ๋์๊ฐ๋ ๊ฒ์ ๊ธฐ์ค์ผ๋ก ํ ์คํธ ๋ ผ๋ฌธ(co-mod-gan)์ ์ ํ์์ต๋๋ค.
- Source: High-Resolution Image Inpainting with Iterative Confidence Feedback and Guided Upsampling
- Image source: High-Resolution Image Inpainting with Iterative Confidence Feedback and Guided Upsampling
์๋นํ ๋น ๋ฅธ ์๋๋ก ์ฒ๋ฆฌํ ์ ์์ผ๋, ๋์ฒด๋ก Inpainting ๋ถ์ผ๋ 512 x 512 ์ฌ์ด์ฆ๋ฅผ ์ต๋๋ก ์ง์ํ๊ธฐ์ ์ด๋ฏธ์ง๋ฅผ ํฌ๋กญํ์ฌ ์ธํ์ผ๋ก ๋ฃ๋ ๊ฒ์ ์ถ์ฒํฉ๋๋ค.
- Ref: [paperwithcode] image inpainting - https://paperswithcode.com/task/image-inpainting
Inpainting SOTA Result: e.g. co-mod-gan
- [Github] DrawingProcess/co-mod-gan-pytorch: https://github.com/DrawingProcess/co-mod-gan-pytorch
์ฐธ๊ณ
- [Blog] Glare removal With Inpainting[OpenCV Python]: https://rcvaram.medium.com/glare-removal-with-inpainting-opencv-python-95355aa2aa52
- [Github] awesome-reflection-removal: https://github.com/ChenyangLEI/awesome-reflection-removal
- [paperwithcode] image inpainting: https://paperswithcode.com/task/image-inpainting
- [Github] DrawingProcess/co-mod-gan-pytorch: https://github.com/DrawingProcess/co-mod-gan-pytorch