Ask Your Question

Eric Greveson's profile - activity

2013-04-30 08:00:44 -0500 received badge  Teacher (source)
2013-04-30 05:08:14 -0500 commented answer compareHist problem, type != CV_32F

You're right that both matrices need to be of type CV_32F - however, the method that the compareHist function (currently) uses to check this is flawed. The test H1.type() == CV_32F will return false if the matrix is stored as e.g. a 2D, n-channel matrix. If n was 3, then H1.type() would be CV_32FC3, and CV_32FC3 != CV_32F. Hence my pull request to change to use H1.depth() - this will return CV_32F even if it is a multi-channel matrix. This bug would only be seen if you're using a histogram stored as a multi-channel cv::Mat - which is only the case for histograms of greater than 2 dimensions. I think your suggestion might indeed be the answer to the original post, though - having a matrix of the wrong type is a more likely scenario - so I'll upvote this.

2013-04-30 00:15:46 -0500 received badge  Necromancer (source)
2013-04-29 13:03:46 -0500 received badge  Editor (source)
2013-04-29 12:21:43 -0500 received badge  Supporter (source)
2013-04-29 12:21:04 -0500 answered a question compareHist problem, type != CV_32F

I've come across the same problem using the cv2.compareHist function in the Python API (which basically calls the same code as the C++ function once data structures have been wrapped / converted). I tried a minor modification - using a 2D array rather than a 3D array - and it worked. I think there is a bug in compareHist in histogram.cpp - as the error message says, it asserts:

H1.type() == H2.type() && H1.type() == CV_32F

but I think that H1.type() == CV_32F returns false if it's a 3D histogram, as H1.type() is no longer CV_32F - it might be something like CV_32FC3 or CV_32FC4, which is stored as a different number (see CV_MAKE_TYPE macro). I believe it could be fixed by using CV_MAT_DEPTH(H1.type()) == CV_32F, although I haven't checked the code. I'll see if I can find a suitable place to file a bug.

EDIT: I've sent a pull request to change H1.type() to H1.depth(). If this gets accepted and merged, the issue should be fixed in future OpenCV versions.