c++ - 打印关键点描述符矩阵 opencv 的值

标签 c++ opencv matrix unsigned-char feature-descriptor

我在打印通过使用任何 opencv 描述符提取器的“计算”方法获得的描述符矩阵的值时遇到了一些问题。我想将一个特征的描述符一个一个地打印到一个文件中,但是当我使用“at”访问描述符矩阵的某个元素时,我总是收到该元素的不同值。以下是我在使用“at”时用来测试描述符矩阵输出值的“for”循环:

for(int i=0; i<nF; i++){

    if(lpx != keypoints[i].pt.x && lpy != keypoints[i].pt.y){
        usedFeatures++;
        cerr << descriptors.row(i) << endl << endl; // printing row of descriptor matrix
        fileS << keypoints[i].pt.y << " " << keypoints[i].pt.x << " ";
        fileS << keypoints[i].size << " " << keypoints[i].angle << endl;

        if(i == nF - 2){
            //printing subvector of descriptor matrix made of the element at row i and col 0
            cerr << "get row i, col 0 " << descriptors.row(i).col(0) << endl;

            //same as before just inverting the order of access
            cerr << "get col 0, row i: " << descriptors.col(0).row(i) << endl;


            //printing the value of the element with 'at'
            cerr << (int)descriptors.at<uchar>(i, 0);

            //creating a new int and giving it the value of element (i, 0) of descriptor matrix. Should be the same
            //value shown on the subvector before
            int test = descriptors.at<uchar>(i, 0);

            //printing value of element
            cerr << "i, 0: " << test << endl;
        }

第二个“if”是一个测试“if”,我在访问描述符的元素时查看打印的值。现在,由

cerr << descriptors.row(i) << endl << endl;

在 nF - 2 迭代中,我得到以下结果:

[20, 11, 0, 18, 51, 3, 0, 3, 133, 50, 0, 0, 0, 0, 0, 11, 133, 18, 0, 0, 0, 0, 0, 3,
119, 2, 0, 0, 0, 0, 0, 2, 19, 5, 0, 4, 55, 27, 1, 1, 133, 25, 0, 1, 4, 1, 0, 22, 133,
18, 0, 0, 0, 0, 0, 14, 131, 13, 1, 0, 0, 0, 0, 1, 12, 1, 0, 1, 56, 133, 25, 13, 133,
14, 0, 0, 3, 8, 20, 80, 133, 38, 0, 0, 0, 0, 0, 51, 106, 11, 1, 0, 0, 0, 0, 23, 0, 0,
0, 0, 19, 126, 70, 11, 23, 0, 0, 0, 0, 9, 83, 133, 53, 1, 0, 0, 0, 0, 2, 133, 26, 
3, 2, 0, 0, 0, 0, 28]

正如预期的那样,前两个打印在第二个“if”中:

cerr << "get row i, col 0 " << descriptors.row(i).col(0) << endl;

cerr << "get col 0, row i: " << descriptors.col(0).row(i) << endl;

给我 [20]

但是另外两个打印

cerr << (int)descriptors.at<uchar>(i, 0);

int test = descriptors.at<uchar>(i, 0);
cerr << "i, 0: " << test << endl;

给我 0 而不是 20。我之前显示的 nF-2 行的完整结果,当使用“at”访问元素并打印它们时:

 0 0 160 65 0 0 48 65 0 0 0 0 0 0 144 65 0 0 76 66
 0 0 64 64 0 0 0 0 0 0 64 64 0 0 5 67 0 0 72 66
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 48 65 0 0 5 67 0 0 144 65 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 64 0 0 238 66
 0 0 0 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 64

这和我想象的完全不一样。我已经尝试了很多东西:使用 float、double、unsigned int 而不是仅使用 int 进行转换,以及分配给这些类型的变量;在打印之前转换矩阵,复制矩阵然后转换,创建具有不同类型的描述符矩阵......但它们都不起作用。我怀疑它与描述符矩阵的类型有关,尽管我几乎可以肯定它具有 uchar 类型(我用 elemSize 检查过)

提前致谢,抱歉我的英语和问题的大小。

最佳答案

找到了答案。这确实是一个类型问题。返回的描述符矩阵的类型并不是我想的uchar,实际上是float。获取值(value)

(float)descriptors.at<float>(i, 0);

给了我正确的值(value)。有趣的是我可以发誓我之前尝试过 float ,但没有用。我一定只对 int、double 和 unsigned int 尝试过。

关于c++ - 打印关键点描述符矩阵 opencv 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10418124/

相关文章:

c++ - strdup 指针版本需要一个临时指针

c++ - 如何将 write() 系统调用设置为 "flush"?

c++ - 使用状态创建 C++11 std::function<>

matlab - 如何从现有矩阵中提取新矩阵

file - 如何在 Mathematica 中将 .dat 文件作为矩阵导入?

c++ - std::async int 函数可以在任务完成之前退出吗?

java - 在 python 和 java 中使用 OpenCV 的不同图像

opencv - 如何使用 catkin 将 OpenCV 3.4.5 dnn 模块与自定义 cv_bridge 链接?错误:未定义的引用 cv::dnn:experimental_dnn_v2::Net::Net()

python - 如何在Python中将边缘检测结果和原始图片拼凑在一起

matlab - 我如何使用 cellfun 或 arrayfun 来代替这个循环?