Algy / fast-slic

20x Real-time superpixel SLIC Implementation with CPU
MIT License
263 stars 34 forks source link

Incorrect centroid location #9

Open hkchengrex opened 4 years ago

hkchengrex commented 4 years ago

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: x Y: y

But it gives me this for X: 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:

hkchengrex commented 4 years ago

I like the output of 0.4 more though, guess I will compute the centroid again in numpy for now.

Algy commented 4 years ago

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😭)

hkchengrex commented 4 years ago

Stay safe! I am in Hong Kong so 👀

ori30ffs commented 3 years ago

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

MassimoDM commented 3 years ago

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.

ekosman commented 3 years ago

Any progress on the problem? I encounter this problem too.

dennisushi commented 3 years ago

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