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

标签 c++ opencv

我想在 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/

相关文章:

c++ - ROS环境搭建错误(catkin)

image-processing - opencv dft仅适用于2的幂的图像尺寸

c++ - 为什么 C++ 允许使用这种方法修改私有(private)成员?

C++:如何初始化非整数类型的静态成员变量?

c++ - 无法获取地址 (wcsstr) - undefined variable ?

android - Opencv 在 android 中拆分和更改该 channel 中的值

qt - 将 Qt QByteArray 转换为 OpenCV cv::InputArray

c++ - VC解决方案跨多个项目访问同一个对象

c++ - opencv/c++ 我如何只检测之前检测到的方形区域内的关键点

python - openCV2无法正确显示图像(添加了灰色区域)