如何使用C ++ Mat类ptr对象的这个基本C包装打印Mat元素 [英] How do I print a Mat element using this basic C wrapper for the C++ Mat Class ptr object

查看:175
本文介绍了如何使用C ++ Mat类ptr对象的这个基本C包装打印Mat元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一些OpenCV C ++代码编写一个C包装,我有两个函数用于创建一个Mat对象,并将数据分别设置为cv_create_Mat_type和cv_Mat_ptr_index。
函数是他们需要的,我不能改变它们,因为有一部分项目我正在与某人工作。

I'm writing a C wrapper for some OpenCV C++ code and I have 2 functions I use to create a Mat object and set data to it cv_create_Mat_type and cv_Mat_ptr_index respectively. The functions are as they need to be, I can't change them because there part of a project I'm working on with someone.

我的问题是如何打印cv_Mat_ptr_index填满的矩阵,因为它在for循环中填充。在cv_Mat_ptr_index上的返回是一个uchar *我尝试解除引用它,但我需要知道如何获取在cv_Mat_ptr_index,uchar *的返回的数据,因为我敢肯定,如果我错了,纠正我,包含当前从下面代码中的数据数组分配到矩阵mat中的元素。

My question is how do I print the matrix that cv_Mat_ptr_index is filling up, as it fills it up inside the for loop. The return on cv_Mat_ptr_index is a uchar* I tried dereferencing it but I need to know how to get at the data inside the return of cv_Mat_ptr_index, the uchar*, because I'm sure, and correct me if I'm wrong, that it contains the element currently allocated into the matrix mat from the data array in the below code .

预先感谢任何接受者。

    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/highgui/highgui_c.h"
    #include <iostream>

    using namespace cv;
    using namespace std;



    uchar* cv_Mat_ptr_index(Mat* self, int i) {
        return self->ptr(i);
    }

    Mat* cv_create_Mat_typed(int rows, int cols, int type) {
        return new Mat(rows, cols, type);
    }

    int main(  )
    {

    float data[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} };
    Mat* mat = cv_create_Mat_typed(4, 2, CV_64F);
    for(int i = 0; i < 4; i++)
        for(int j = 0; j < 2; j++)
             cv_Mat_ptr_index(mat, i)[j] = data[i][j];


    }

编辑:

我试图基本打印一个矩阵使用cv_Mat_ptr_index,希望它设置数据到矩阵mat但如果我不能这样做,我希望你可以告诉我如何使用cv_Mat_ptr_index打印矩阵mat的内容

I'm trying to basically print a matrix using cv_Mat_ptr_index, hopefully while its setting the data to the matrix "mat" but If I cant do that I was hoping you can show me how to use cv_Mat_ptr_index to print the contents of the matrix "mat"

i / e like cout< mat; 通常会做,但我不能使用 cout<<因为它是一个指针。

i/e like cout << mat; would normally do but I can't use cout << mat; because it is a pointer.

我也希望你能告诉我如何打印一个元素的mat它已经可以访问ptr对象...我不能以任何方式改变cv_Mat_ptr_index虽然...

I was also hoping you can show me how to print an element of "mat" with it because it already has access to the ptr object...I can't change cv_Mat_ptr_index in any way though...

推荐答案

code> Mat :: ptr(i)给出一个指向矩阵中 i th元素的指针,向量。

Mat::ptr(i) gives a pointer to the ith element in the matrix, seen as a long 1 dimensional vector.

Mat :: at< double>(i,0)到矩阵的 i 行的第0个元素,看作2D矩阵正常,所以你可以取其地址。

Mat::at<double>(i,0) gives a reference to the 0th element of the ith row of the matrix, seen as a 2D matrix as normal, so you can take its address.

double* cv_Mat_ptr_index(Mat* self, int i) {
        return &self->at<double>(i,0);
}

不要忘记取消引用 cv_Mat_ptr_index(...)

Don't forget to dereference the pointer you get back from cv_Mat_ptr_index(...) when you assign to it, eg

   *cv_Mat_ptr_index(mat, i)[j] = data[i][j];
// ^-- note dereference

这篇关于如何使用C ++ Mat类ptr对象的这个基本C包装打印Mat元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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