abhisheka456 / GridShift

9 stars 0 forks source link

Running examples #1

Open pha-nguyen opened 2 years ago

pha-nguyen commented 2 years ago

Hi,

Thanks for your great work!

I can see the implementation of your core GridShift algorithm, and I am diving into the applications that you mentioned in the paper, i.e. object tracking, and segmentation. Could you please provide examples of how to apply your algorithm to those tasks?

Appreciate!

Treeboy2762 commented 2 years ago

Hi, I hope you have already solved the problem.

The below code is a reproducible example.

from GridShiftPP2 import GridShiftPP
import numpy as np

gs = GridShiftPP(0.25, 0.001, 10)

data = np.array([[0,0], [1,1]])
result = gs.fit_predict(data)

print(result)
javiabellan commented 4 days ago

I can not make this color quantization code to work:

import GridShiftPP2  # This is the name given in the setup.py file
import cv2
import requests
import numpy as np
from matplotlib.pyplot import imshow

img_url = "https://t2.gstatic.com/licensed-image?q=tbn:ANd9GcRS7Outpkdd8Ir9TmzQnF5HxJr6nIhJIl2Cgp0mkLtlzF4zSRCx5Wa2bbKkgkUbp741Rso7ZYl90gzJmke9bkE"

response = requests.get(img_url)
img_np_array = np.frombuffer(response.content, np.uint8)
img_bgr = cv2.imdecode(img_np_array, cv2.IMREAD_COLOR)
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

def color_qunatization(img, bandwidth=1.0, threshold=0.001, iterations=10):

    height, width, c = img.shape
    img_feats = img.reshape(-1,3) / 255

    # GridShift
    grid_shift_instance = GridShiftPP2.GridShiftPP(bandwidth=bandwidth, threshold=threshold, iterations=iterations)
    label_map, cluster_centers = grid_shift_instance.fit_predict(img_feats)

    label_map = label_map.reshape(height, width)

    colors = cluster_centers # float64 of shape [N_clusters,3]

    img_q_rgb = colors[label_map] * 255
    img_q_rgb = img_q_rgb.round().astype(np.uint8)

    return label_map, colors, img_q_rgb

label_map, colors, img_q_rgb = color_qunatization(img_rgb)
imshow(img_q_rgb)

All i got is this single avg color:

download-2

Note that Lab color space is recommended for color distances, but i keep the RGB for the simplicity of the code.