fast-slic
fast-slic copied to clipboard
Incorrect centroid location
Thank you for this great piece of work!
I spotted an issue in the latest version (0.4.0), that the centroid information returned by slic.slic_model.clusters is incorrect.
Minimal working example:
import numpy as np
# Much faster than the standard class
from fast_slic.avx2 import SlicAvx2
from PIL import Image
import cv2
with Image.open('fish.jpg') as f:
image = np.array(f)
slic = SlicAvx2(num_components=512, compactness=10)
assignment = slic.iterate(image) # Cluster Map
x_centroid_map = np.zeros_like(assignment, dtype=np.uint16)
y_centroid_map = np.zeros_like(assignment, dtype=np.uint16)
for c in slic.slic_model.clusters:
y, x = c['yx']
x_centroid_map[assignment==c['number']] = x
y_centroid_map[assignment==c['number']] = y
out = Image.fromarray((x_centroid_map.astype('float')/3000*255).astype('uint8'))
out.save('x.jpg')
out = Image.fromarray((y_centroid_map.astype('float')/3000*255).astype('uint8'))
out.save('y.jpg')
I would expect the (x, y) centroid map to look like this:
X:
Y:

But it gives me this for X:
and Y is fine.
Note that this works in version 0.3.5 (which I used to generate the "expected output")!
Hope this helps :smile:
I like the output of 0.4 more though, guess I will compute the centroid again in numpy for now.
Thanks for the report. I'll have a look on the version you mentioned. (it may take time though, because of the ongoing issue in my country😭)
Stay safe! I am in Hong Kong so 👀
Hello @Algy As I understand the problem with centroid is still available. Can you advise me a fast solution to calculate it by myself? Currently I'm using regionprops from skimage, but it's too slow
Hello @Algy ,
Thanks for this library, it is really fast and does the job very well.
I think that I met an issue that could be connected to the centroid one. If I show the image obtained by coloring every pixel of original image with the color of the assigned cluster I get the colors all shuffled up. I think that the indices of the clusters do not match the indices in the assignment matrix.
PS I checked the color space and it is not a problem of color conversion.
Any progress on the problem? I encounter this problem too.
You can bypass the problem by reassigning the index at very little cost, would still be faster if fixed in the C++ code probably.
for entry in slic.slic_model.clusters:
y, x = entry['yx']
idx = assignment[int(y),int(x)]
x_centroid_map[assignment==idx] = x
y_centroid_map[assignment==idx] = y