4

I have a RGB image of the following shape ((3L, 5L, 5L). It means 5 by 5 pixels image having 3 layers (R,G,andB).I want to cluster it using DBSCAN algorithm as follows. But I got an error message that ValueError: Found array with dim 3. Expected <= 2. Can not I use for my 3d image?

import numpy as np
from sklearn.cluster import DBSCAN
from collections import Counter

data = np.random.rand(3,5,5)
print np.shape(data)
print data

db = DBSCAN(eps=0.12, min_samples=3).fit(data)
print db
DBSCAN(algorithm='auto', eps=0.12, leaf_size=30, metric='euclidean',
    min_samples=1, p=None, random_state=None)
labels = db.labels_

print Counter(labels)
3
  • 1
    If you look at the documentation: X : array or sparse (CSR) matrix of shape (n_samples, n_features), or array of shape (n_samples, n_samples) Commented Aug 17, 2015 at 6:08
  • How do you define the distance between two pixels? Commented Aug 17, 2015 at 7:05
  • @yangjie I have no idea. Do you mean distance between two pixels over the channel (voxel)? Commented Aug 17, 2015 at 12:54

1 Answer 1

2

To cluster you need to say what the distance between two points is. DBSCAN is not a graph clustering algorithm, it works with features. You need to represent each pixel as features, so that the distances are appropriate.

The features could just be RGB, in which case similar colors are clustered together. Or the features could also include x, y coordinates, which would mean spacial distances are also considered.

If you want to consider spatial distances, I'd suggest you take a look at scikit-image's segmentation module, which contains a couple of popular image segmentation methods.

Sign up to request clarification or add additional context in comments.

2 Comments

So, yes RGB are the features, and x,y coordinates are the spatial distances. I have to cluster the data, i.e., RGB channel values with respect to the x,y coordinates. This means, the nearer the RGB values, the nearer the x,y coordinates, i.e., they would fall into one same cluster. In this case, does not DBSCAN work?
It can work but you need to put all your data in to a 2d array where each row is one pixel and the first three columns are the RGB values and the next two columns are the x and y values. (and you probably need to multiply either of them by some value to get them to the same order of magnitude).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.