如何使用OpenCV访问C ++中的3D直方图值? [英] How to access 3D Histogram values in C++ using OpenCV?

查看:141
本文介绍了如何使用OpenCV访问C ++中的3D直方图值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问RGB图像的3D直方图。但是直方图矩阵返回等于-1的行和列的数量。 我想遍历直方图并检查3D矩阵中的各个值。但是,当我检查矩阵中的行数和列数时,我得到-1如下所示。

I am trying to access an 3D histogram of a RGB image. But the histogram matrix returns the number of rows and columns equal to -1. I want to iterate through the histogram and check the individual values in the 3D matrix. But, when I check the number of rows and columns in the matrix, I get -1 as shown below.

CODE

int main( int argc, const char** argv ) {
    Mat image = imread("fl.png");
    int histSize[3] = {8, 8, 8};
    float range[2] = {0, 256};
    const float * ranges[3] = {range, range, range};
    int channels[3] = {0, 1, 2};
    Mat hist;
    calcHist(&image, 1, channels, Mat(), hist, 3, histSize, ranges);
    cout << "Hist.rows = "<< hist.rows << endl;
    cout << "Hist.cols = "<< hist.cols << endl;
    return 0;
}

OUTPUT

Hist.rows = -1
Hist.cols = -1

我犯了什么错误?如何访问各个矩阵值。

What mistake am I making? How can I access the individual matrix values.

推荐答案

Mat


//! the number of rows and columns or (-1, -1) when the array has more than 2 dimensions


但您有3个维度。

您可以使用 hist.at< T> ;(i,j,k)

或者您可以使用文档此处

    // Build with gcc main.cpp  -lopencv_highgui -lopencv_core -lopencv_imgproc
    #include <iostream>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui.hpp>
    #include <opencv2/imgproc.hpp>

    using std::cout;
    using std::endl;
    using namespace cv; # Please, don't include whole namespaces!

    int main( int argc, const char** argv ) {
        Mat image = imread("good.jpg");
        int histSize[3] = {8, 8, 8};
        float range[2] = {0, 256};
        const float * ranges[3] = {range, range, range};
        int channels[3] = {0, 1, 2};
        Mat hist;
        calcHist(&image, 1, channels, Mat(), hist, 3, histSize, ranges);
        cout << "Hist.dims = " << hist.dims << endl;
        cout << "Value: " << hist.at<double>(0,0, 0) << endl;
        cout << "Hist.rows = "<< hist.rows << endl;
        cout << "Hist.cols = "<< hist.cols << endl;
        return 0;
    }



迭代所有值:


$ b b

Iterate through every value:

        for (MatConstIterator_<double> it = hist.begin<double>(); it != hist.end<double>(); it++) {
            cout << "Value: " << *it << "\n";
        }
        cout << std::flush;



使用索引遍历每个值:



Iterate through every value using indices:

        for (int i=0; i<histSize[0]; i++) {
            for (int j=0; j<histSize[1]; j++) {
                for (int k=0; k<histSize[2]; k++) {
                    cout << "Value(" << i << ", " << j << ", " << k <<"): " << hist.at<double>(i, j, k) << "\n";
                }
            }
        }
        cout << std::flush;

这篇关于如何使用OpenCV访问C ++中的3D直方图值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆