Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect centroid location #9

Open
hkchengrex opened this issue Feb 20, 2020 · 7 comments
Open

Incorrect centroid location #9

hkchengrex opened this issue Feb 20, 2020 · 7 comments

Comments

@hkchengrex
Copy link

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 😄

@hkchengrex
Copy link
Author

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

@Algy
Copy link
Owner

Algy commented Feb 24, 2020

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
Copy link
Author

Stay safe! I am in Hong Kong so 👀

@ori30ffs
Copy link

ori30ffs commented Dec 3, 2020

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
Copy link

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
Copy link

ekosman commented Jun 28, 2021

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

@dennisushi
Copy link

dennisushi commented Oct 14, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants