{"action":"created","issue":{"url":"https://api.github.com/repos/Itseez/opencv/issues/6355","repository_url":"https://api.github.com/repos/Itseez/opencv","labels_url":"https://api.github.com/repos/Itseez/opencv/issues/6355/labels{/name}","comments_url":"https://api.github.com/repos/Itseez/opencv/issues/6355/comments","events_url":"https://api.github.com/repos/Itseez/opencv/issues/6355/events","html_url":"https://github.com/Itseez/opencv/issues/6355","id":145200349,"number":6355,"title":"cvflann::hierarchicalClustering doesn't compile with Hamming Distance","user":{"login":"mixmixmix","id":9990867,"avatar_url":"https://avatars.githubusercontent.com/u/9990867?v=3","gravatar_id":"","url":"https://api.github.com/users/mixmixmix","html_url":"https://github.com/mixmixmix","followers_url":"https://api.github.com/users/mixmixmix/followers","following_url":"https://api.github.com/users/mixmixmix/following{/other_user}","gists_url":"https://api.github.com/users/mixmixmix/gists{/gist_id}","starred_url":"https://api.github.com/users/mixmixmix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mixmixmix/subscriptions","organizations_url":"https://api.github.com/users/mixmixmix/orgs","repos_url":"https://api.github.com/users/mixmixmix/repos","events_url":"https://api.github.com/users/mixmixmix/events{/privacy}","received_events_url":"https://api.github.com/users/mixmixmix/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Itseez/opencv/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-01T14:48:13Z","updated_at":"2016-04-06T11:15:54Z","closed_at":null,"body":"Hello:\r\n\r\nFunction call: `cv::flann::hierarchicalClustering<cv::flann::Hamming<uchar>>` causes compilation error: \r\n```\r\ninclude/opencv2/flann/dist.h:450:31: error: reinterpret_cast from 'cvflann::ZeroIterator<unsigned char>' to 'const pop_t *' (aka 'const unsigned long long *') is not allowed\r\n            const pop_t* b2 = reinterpret_cast<const pop_t*> (b);\r\n                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n```\r\nWorks fine with L2 distance.\r\n\r\nPossibly the same problem as in [this very old issue 1346 from OpenCV 2.3.1](http://code.opencv.org/issues/1346)\r\n\r\n\r\n## Please state the information for your system\r\n- OpenCV version: 3.0\r\n\r\n### Expected behaviour\r\nhierarchical clustering of binary feature descriptors (stored as chars).\r\n\r\n### Actual behaviour\r\ncompilation error\r\n\r\n### Additional description\r\n\r\n### Code example to reproduce the issue:\r\n\r\n\r\n```\r\n    cvflann::KMeansIndexParams params;\t\r\n\tcv::Mat words = cv::Mat::zeros(dictionarySize, features.cols, CV_32F);\r\n\tint count = cv::flann::hierarchicalClustering<cv::flann::Hamming<uchar>>(features,words,params);\t\r\n//works fine with L2 distance\r\n//    int count = cv::flann::hierarchicalClustering<cv::flann::L2<float>>(features,words,params);\t\r\n\r\n```\r\n\r\nError in compiling:\r\n\r\n```\r\n[ 11%] Building CXX object CMakeFiles/r-bow.dir/bow.cpp.o\r\nIn file included from /home/mix/rachael/source/bow.cpp:1:\r\nIn file included from /home/mix/rachael/source/common.h:11:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann.hpp:48:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/flann_base.hpp:43:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/all_indices.h:36:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/kdtree_index.h:41:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/dynamic_bitset.h:49:\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/dist.h:450:31: error: reinterpret_cast from 'cvflann::ZeroIterator<unsigned char>' to 'const pop_t *' (aka 'const unsigned long long *') is not allowed\r\n            const pop_t* b2 = reinterpret_cast<const pop_t*> (b);\r\n                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/kmeans_index.h:681:25: note: in instantiation of function template specialization 'cvflann::Hamming<unsigned char>::operator()<unsigned char *, cvflann::ZeroIterator<unsigned char> >' requested here\r\n            variance += distance_(vec, ZeroIterator<ElementType>(), veclen_);\r\n                        ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/kmeans_index.h:444:9: note: in instantiation of member function 'cvflann::KMeansIndex<cvflann::Hamming<unsigned char> >::computeNodeStatistics' requested here\r\n        computeNodeStatistics(root_, indices_, (int)size_);\r\n        ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/flann_base.hpp:283:12: note: in instantiation of member function 'cvflann::KMeansIndex<cvflann::Hamming<unsigned char> >::buildIndex' requested here\r\n    kmeans.buildIndex();\r\n           ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann.hpp:532:23: note: in instantiation of function template specialization 'cvflann::hierarchicalClustering<cvflann::Hamming<unsigned char> >' requested here\r\n    return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);\r\n                      ^\r\n/home/mix/rachael/source/bow.cpp:24:25: note: in instantiation of function template specialization 'cv::flann::hierarchicalClustering<cvflann::Hamming<unsigned char> >' requested here\r\n        int count = cv::flann::hierarchicalClustering<cv::flann::Hamming<uchar>>(features,words,params);        \r\n                               ^\r\nIn file included from /home/mix/rachael/source/bow.cpp:1:\r\nIn file included from /home/mix/rachael/source/common.h:11:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann.hpp:48:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/flann_base.hpp:43:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/all_indices.h:36:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/kdtree_index.h:41:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/dynamic_bitset.h:49:\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/dist.h:450:31: error: reinterpret_cast from 'cvflann::ZeroIterator<unsigned char>' to 'const pop_t *' (aka 'const unsigned long long *') is not allowed\r\n            const pop_t* b2 = reinterpret_cast<const pop_t*> (b);\r\n                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/kmeans_index.h:687:21: note: in instantiation of function template specialization 'cvflann::Hamming<unsigned char>::operator()<int *, cvflann::ZeroIterator<unsigned char> >' requested here\r\n        variance -= distance_(mean, ZeroIterator<ElementType>(), veclen_);\r\n                    ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/kmeans_index.h:444:9: note: in instantiation of member function 'cvflann::KMeansIndex<cvflann::Hamming<unsigned char> >::computeNodeStatistics' requested here\r\n        computeNodeStatistics(root_, indices_, (int)size_);\r\n        ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/flann_base.hpp:283:12: note: in instantiation of member function 'cvflann::KMeansIndex<cvflann::Hamming<unsigned char> >::buildIndex' requested here\r\n    kmeans.buildIndex();\r\n           ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann.hpp:532:23: note: in instantiation of function template specialization 'cvflann::hierarchicalClustering<cvflann::Hamming<unsigned char> >' requested here\r\n    return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);\r\n                      ^\r\n/home/mix/rachael/source/bow.cpp:24:25: note: in instantiation of function template specialization 'cv::flann::hierarchicalClustering<cvflann::Hamming<unsigned char> >' requested here\r\n        int count = cv::flann::hierarchicalClustering<cv::flann::Hamming<uchar>>(features,words,params);        \r\n                               ^\r\nIn file included from /home/mix/rachael/source/bow.cpp:1:\r\nIn file included from /home/mix/rachael/source/common.h:11:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann.hpp:48:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/flann_base.hpp:43:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/all_indices.h:38:\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/kmeans_index.h:854:38: error: no matching function for call to object of type 'cvflann::Hamming<unsigned char>'\r\n                    DistanceType d = distance_(dataset_[indices[i]], ZeroIterator<ElementType>(), veclen_);\r\n                                     ^~~~~~~~~\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/kmeans_index.h:445:9: note: in instantiation of member function 'cvflann::KMeansIndex<cvflann::Hamming<unsigned char> >::computeClustering' requested here\r\n        computeClustering(root_, indices_, (int)size_, branching_,0);\r\n        ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/flann_base.hpp:283:12: note: in instantiation of member function 'cvflann::KMeansIndex<cvflann::Hamming<unsigned char> >::buildIndex' requested here\r\n    kmeans.buildIndex();\r\n           ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann.hpp:532:23: note: in instantiation of function template specialization 'cvflann::hierarchicalClustering<cvflann::Hamming<unsigned char> >' requested here\r\n    return ::cvflann::hierarchicalClustering<Distance>(m_features, m_centers, params, d);\r\n                      ^\r\n/home/mix/rachael/source/bow.cpp:24:25: note: in instantiation of function template specialization 'cv::flann::hierarchicalClustering<cvflann::Hamming<unsigned char> >' requested here\r\n        int count = cv::flann::hierarchicalClustering<cv::flann::Hamming<uchar>>(features,words,params);        \r\n                               ^\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/dist.h:425:16: note: candidate template ignored: substitution failure [with Iterator1 = unsigned char *, Iterator2 = cvflann::ZeroIterator<unsigned char>]\r\n    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = -1) const\r\n               ^\r\nIn file included from /home/mix/rachael/source/bow.cpp:1:\r\nIn file included from /home/mix/rachael/source/common.h:11:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann.hpp:48:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/flann_base.hpp:43:\r\nIn file included from /opt/opencv-3.0.0-dbg/include/opencv2/flann/all_indices.h:38:\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/kmeans_index.h:864:25: error: no matching function for call to object of type 'cvflann::Hamming<unsigned char>'\r\n            variance -= distance_(centers[c], ZeroIterator<ElementType>(), veclen_);\r\n                        ^~~~~~~~~\r\n/opt/opencv-3.0.0-dbg/include/opencv2/flann/dist.h:425:16: note: candidate template ignored: substitution failure [with Iterator1 = int *, Iterator2 = cvflann::ZeroIterator<unsigned char>]\r\n    ResultType operator()(Iterator1 a, Iterator2 b, size_t size, ResultType /*worst_dist*/ = -1) const\r\n               ^\r\n4 errors generated.\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/Itseez/opencv/issues/comments/206317119","html_url":"https://github.com/Itseez/opencv/issues/6355#issuecomment-206317119","issue_url":"https://api.github.com/repos/Itseez/opencv/issues/6355","id":206317119,"user":{"login":"mixmixmix","id":9990867,"avatar_url":"https://avatars.githubusercontent.com/u/9990867?v=3","gravatar_id":"","url":"https://api.github.com/users/mixmixmix","html_url":"https://github.com/mixmixmix","followers_url":"https://api.github.com/users/mixmixmix/followers","following_url":"https://api.github.com/users/mixmixmix/following{/other_user}","gists_url":"https://api.github.com/users/mixmixmix/gists{/gist_id}","starred_url":"https://api.github.com/users/mixmixmix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mixmixmix/subscriptions","organizations_url":"https://api.github.com/users/mixmixmix/orgs","repos_url":"https://api.github.com/users/mixmixmix/repos","events_url":"https://api.github.com/users/mixmixmix/events{/privacy}","received_events_url":"https://api.github.com/users/mixmixmix/received_events","type":"User","site_admin":false},"created_at":"2016-04-06T11:15:54Z","updated_at":"2016-04-06T11:15:54Z","body":"It works when I use the deprecated function instead (with float centres):\r\n` [WARNING] cv::flann::hierarchicalClustering<ELEM_TYPE,DIST_TYPE> is deprecated, use cv::flann::hierarchicalClustering<Distance> instead `\r\nI'm testing it now with the  BoW on Caltech101 to see if it computes what it promise to.\r\n\r\nAs it seems to be the only way to use BoW approach with binary features, I'm happy to write a tutorial and correct the bug in Hamming distance. However, it worries me that only deprecated function works properly. Can someone clarify why it is being deprecated?\r\n"}}