gpt4 book ai didi

c++ - 使用 C++ 在 OpenCV 中矩阵中的多维数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:33 32 4
gpt4 key购买 nike

我想在 OpenCV (C++) 中声明、填充、访问与命名空间 cv 兼容的多维矩阵。我没有找到关于它们的快速易学示例。你能帮帮我吗?

最佳答案

这是来自 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 .

关于c++ - 使用 C++ 在 OpenCV 中矩阵中的多维数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8809517/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com