hierarchical clustering
I am trying to cluster features using the flann hierarchicalClustering function. The features' dimension is 48 and I have around 100 features. I don't understand why the clustering crashes ? (seg fault). Below is a sample code of what I am doing :
//samples
cv::Mat1f descriptorsSamples = cv::Mat1f::zeros(nSamples, 48);
//here I fill descriptorsSamples with float values (48 dim descriptors)
unsigned int branching = 32; // 32
unsigned int niterations = 10; // 100
int nClusters = 0;
cvflann::KMeansIndexParams kmean_params;
unsigned int initiNCenters = 10;
cv::Mat1f centers(initiNCenters, 3);
nClusters = cv::flann::hierarchicalClustering< cv::flann::L2<float> >((const cv::Mat &) descriptorsSamples,
(cv::Mat &) centers,
(const::cvflann::KMeansIndexParams &) kmean_params);
IMPORTANT : I am using OpenCV 2.4.8 (this version is imposed)
don't. it's stone age, and no more maintained.
your code does not segfault with recent 4.1.0, so:
thanks for your answer. I will try to update my OpenCV version. Meanwhile below is how I fill my samples :
I think it could be the types (my descriptor vector is of type double and the sample required for the hierarchical clustering are floats). Can I use doubles with the hierarchical clustering ?
are your
descriptorsSamples
valid throughout all of the program ? the flann code tries to run away with an internal float * pointer, breaking any Mat refcounting.however, the way you copy it to a float Mat looks correct, and iirc, no, you have to stick with float using flann.
I think I know where the issue is: the sample dimension is too big. I have reduced the dimension from 48 to 5 and it runs. Now, since my descriptors are dimension 48, I wonder if there is indeed a limitation in the dimensions of the samples ? I couldn't find this information anywhere.
no, there are no internal size restrictions. look at your own code again.
what if you do not use
Mat1f
butMat(w,h,CV_32F)
?I just tried using Mat(w,h, CV_32F) and this did not fix the issue... I keep on searching. Thanks again.
finally got it working (cv::Mat1f centers(initiNCenters, 3) : I replaced 3 with the descriptor's dimension. It was 3 because I was testing to cluster 3d data...). However, when I iterate the process in order to cluster new descriptors, it would be stuck after some iterations... I guess I need to read further on the hierarchical clustering in OpenCV... I have found this : https://docs.opencv.org/3.4.6/dc/de5/... I have search for a more detailed documentation on OpenCV's HC but it seems that I can't manage to find one.
The clustering, when it works, ends up returning only one cluster. My intuition is that this might be due to the choice of distance, among other parameters. I have seen that there is an overloaded HC function that allows to select the distance. How can I change the default distance type ? What shall I provide in the Distance d = Distance() field ?