hustvl / Matte-Anything

[Image and Vision Computing (Vol.147 Jul. '24)] Interactive Natural Image Matting with Segment Anything Models
MIT License
465 stars 33 forks source link

Removing background issue #18

Closed LukaGiorgadze closed 8 months ago

LukaGiorgadze commented 8 months ago

I've been using your library for image matting and encountered an issue. After applying the alpha matte to an RGB image, parts of the background, especially around complex areas like hair, remain visible. I tried refining the alpha matte using guided filtering, but the background is still not completely removed. I'm new in Python and image processing, so I might be doing something wrong, please guide me on how to remove or/and apply a new background on the processed photo.

DiffMatte Result: result

Extracting foreground: final

Code:

import cv2
import numpy as np
from PIL import Image

original_image = cv2.imread("demo/retriever_rgb.png")
alpha_matte = cv2.imread("demo/result.png", cv2.IMREAD_GRAYSCALE)

if alpha_matte.dtype != np.uint8:
    alpha_matte = cv2.convertScaleAbs(alpha_matte, alpha=255.0)

radius = 60 
eps = 1e-3  
refined_alpha = cv2.ximgproc.guidedFilter(original_image, alpha_matte, radius, eps)

result = cv2.merge((original_image, refined_alpha))

cv2.imwrite("demo/final.png", result)
cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()