Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在OpenCV(C+;+;)中使用calcHist时出错_C++_Opencv_Histogram_Mat - Fatal编程技术网

C++ 在OpenCV(C+;+;)中使用calcHist时出错

C++ 在OpenCV(C+;+;)中使用calcHist时出错,c++,opencv,histogram,mat,C++,Opencv,Histogram,Mat,关于OpenCV中的calcHist,这里有很多问题,但我找不到问题的答案,并且已经阅读了好几次文档,因此希望有人能通过以下代码发现我的问题: //the setup is that I've got a 1x993 cv::Mat called bestLabels that contains cluster //labels for 993 features each belonging to 1 of 40 different clusters. I'm just trying //t

关于OpenCV中的calcHist,这里有很多问题,但我找不到问题的答案,并且已经阅读了好几次文档,因此希望有人能通过以下代码发现我的问题:

//the setup is that I've got a 1x993 cv::Mat called bestLabels that contains cluster 
//labels for 993 features each belonging to 1 of 40 different clusters. I'm just trying 
//to histogram these into hist. 

cv::Mat hist;
int nbins = 40; 
int hsize[] = { nbins }; 
float range[] = { 0, 39 };
const float *ranges[] = { range };
int chnls[] = { 0 };   
cv::calcHist(&bestLabels, 1, chnls, cv::Mat(), hist, 1, hsize, ranges);
这会编译,但当我运行它时,会出现一个错误:

OpenCV Error: Unsupported format or combination of formats () in cv::calcHist
一开始很难让它编译,但现在我真的不确定我遗漏了什么。救命啊

或者,我尝试遍历bestLabels的元素,只是增加存储直方图的数组中的值,但是使用bestLabels.at(0,I)也不起作用。必须有一种更简单的方法从cv::Mat对象中提取单个元素


谢谢您的帮助。

最佳标签的类型是什么

我可以用CV_32S重现您的错误,但它可以用CV_8U或CV_32F正常工作

也许最简单的方法是将其转换为uchar:

bestLabels.convertTo( bestLabels, CV_8U ); // CV_32F for float, might be overkill here

此外,“手动”直方图计算也不是那么难:

Mat bestLabels(1,933,CV_32S); // assuming 'int' here again

Mat hist(1,40,CV_8U,Scalar(0));

for ( int i=0; i<bestLabels.cols; i++ ) 
    hist[ bestLabels.at<int>(0,i) ] ++;
matbestlabels(1933年,CV_32S);//再次假设“int”
Mat hist(1,40,CV_8U,标量(0));

for(int i=0;ibestLabels是cv::Mat,来自kmeans()函数的输出。您建议的是我之前对hist所做的(手动计算)但是它给我带来了麻烦,原因和我想的一样,因为我正在使用。所以openCV因为无法处理自己的数据类型而抛出了一个错误?我没有出于任何特殊原因选择CV_32S,它只是kmeans()的输出类型。为了澄清这一点,我在调用bestLabels.type()时我得到了4,我认为这意味着CV_32S。是的,我也这么认为。不幸的是,calcHist不喜欢integer Mat输入(错误是这样说的)。请参阅编辑。