我想声明,填充,访问与命名空间cv兼容的OpenCV(C++)中的多维矩阵。我发现他们没有快速而容易学习的例子。你能帮我解决吗?OpenCV中使用C++的Matrix中的多维数据
5
A
回答
8
以下是NAryMatIterator文档的简短示例;它显示了如何创建,填充和在OpenCV中处理一个多维矩阵:
void computeNormalizedColorHist(const Mat& image, Mat& hist, int N, double minProb)
{
const int histSize[] = {N, N, N};
// make sure that the histogram has a proper size and type
hist.create(3, histSize, CV_32F);
// and clear it
hist = Scalar(0);
// the loop below assumes that the image
// is a 8-bit 3-channel. check it.
CV_Assert(image.type() == CV_8UC3);
MatConstIterator_<Vec3b> it = image.begin<Vec3b>(),
it_end = image.end<Vec3b>();
for(; it != it_end; ++it)
{
const Vec3b& pix = *it;
hist.at<float>(pix[0]*N/256, pix[1]*N/256, pix[2]*N/256) += 1.f;
}
minProb *= image.rows*image.cols;
Mat plane;
NAryMatIterator it(&hist, &plane, 1);
double s = 0;
// iterate through the matrix. on each iteration
// it.planes[*] (of type Mat) will be set to the current plane.
for(int p = 0; p < it.nplanes; p++, ++it)
{
threshold(it.planes[0], it.planes[0], minProb, 0, THRESH_TOZERO);
s += sum(it.planes[0])[0];
}
s = 1./s;
it = NAryMatIterator(&hist, &plane, 1);
for(int p = 0; p < it.nplanes; p++, ++it)
it.planes[0] *= s;
}
此外,检查出所述cv::compareHist
功能的NAryMatIterator
here另一使用示例。
1
要创建多维矩阵是大小100×100×,采用浮筒,一个信道,并与所有元素初始化为10你写这样的:
int size[3] = { 100, 100, 3 };
cv::Mat M(3, size, CV_32FC1, cv::Scalar(10));
要遍历和输出中的元素矩阵可以这样做:
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
for (int k = 0; k < 3; k++)
std::cout << M.at<cv::Vec3f>(i,j)[k] << ", ";
然而,谨防使用多维矩阵的烦恼作为记录在这里:How do i get the size of a multi-dimensional cv::Mat? (Mat, or MatND)
+0
正是我在找的东西,简单又方便!谢谢。 – 2015-11-17 16:25:00
相关问题
- 1. Opencv:二维条码(Data Matrix)检测
- 2. C#中的多维数据结构#
- 3. 使用单维数组中的数据创建多维数组
- 4. C++中的多维数组
- 5. C++中的多维数组?
- 6. C++中的多维数组
- 7. C中的多维数组
- 8. 在C中使用多维大数组
- 9. c使用多维数组
- 10. 在C#中有多个数据类型的多维数组?
- 11. C#中多维数组中的问题
- 12. 将多维数组加载到C++中的数据文件中
- 13. opencv多维kmeans
- 14. 多维数组索引中的多维数组C++
- 15. 如何获取C#中多维数组的单维的引用?
- 16. 使用与多维数据
- 17. 在three.js中开始使用进行中的多维数据集
- 18. C++多维数据处理
- 19. 访问多维数组中的数据
- 20. 取消多维数组中的数据
- 21. 使用jQuery访问多维JSON数组中的数据
- 22. 如何使用把手渲染多维数组中的数据?
- 23. 如何使用jquery访问多维数组中的数据?
- 24. C++ - 多维数组中的指针
- 25. C++中的多维动态数组类
- 26. 目标C中的多维数组
- 27. C++中的多维数组挂起
- 28. 多维数组和C++中的指针?
- 29. c中的多维集合#
- 30. C++中的多维向量
因此,一个3维数组应是这样的吗? int sz [] = {3,3,3}; Mat accumarray(3,sz,CV_8U,Scalar :: all(0)); accumarray.at(0,1,2)= 20; –
garak
2012-01-11 00:42:39
是的。这应该工作得很好。 – mevatron 2012-01-11 15:04:12
好吧,上面的方法似乎不适用于4维矩阵。你知道原因吗? – garak 2012-01-16 14:52:28