'Image Colorhash with alpha channel

I create a background extractor with opencv to get the colorhash without the background white

img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

def mask_image(gray_image):
    mask = cv2.threshold(gray_image, 250, 255, cv2.THRESH_BINARY)[1]
    mask = 255 - mask
    kernel = np.ones((3,3), np.uint8)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
    mask = cv2.GaussianBlur(mask, (0,0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT)

    return (2*(mask.astype(np.float32))-255.0).clip(0,255).astype(np.uint8)
    

def transparent_bounding_box(image, mask):
    transp = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
    transp[:, :, 3] = mask

    return transp

image_mask = mask_image(gray)
transparent_image = transparent_bounding_box(img, image_mask)

# save resulting masked image
file_name = 'no_bg_white.png'
cv2.imwrite(file_name, transparent_image)

enter image description here enter image description here

I am using the colorhash from imagehash lib https://pypi.org/project/ImageHash/ but unfortunately the hash of the images is the same

hash = imagehash.colorhash(image)
hash_2 = imagehash.colorhash(image_2)
print(hash,',', hash_2)

output: 07e00000000 , 07e00000000 

Do someone know how can I get the colorhash of a image without the transparency value?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source