【发布时间】:2016-05-19 12:11:20
【问题描述】:
这是我正在运行的代码,以供参考我将在代码底部询问的内容:
#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( )
{ uchar a;
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];
printf("%i\n", cv_Mat_ptr_index(mat, i)[j]);}}
}
在 for 循环的正上方是我用来将“data”的内容添加到矩阵“mat”中的包装函数。它被称为 cv_Mat_ptr_index。在这一行cv_Mat_ptr_index(mat, i)[j] = data[i][j]; 它将“data”中的所有数据设置为“mat”。我试图让这条线printf("%i\n", cv_Mat_ptr_index(mat, i)[j]) 将数据的内容打印为 4x2 矩阵,即这正是:
501 10 255 10 501 255 10 501
但我能得到的最好的结果是在下面。我尝试更改 for 循环结束括号的位置。尝试使用 %i, %s,%u 调用,因为数字错误...我更改了第二个 cv_Mat_ptr_index` line by taking off the[j]` 将该行更改为 uchar* 输出,因此我可以使用 u%。尝试使用 cout。
..我可以继续下去,但我可以使用一些帮助来获取printf("%i\n", cv_Mat_ptr_index(mat, i)[j] 行来打印上面的矩阵..我在一个项目中使用它,我无法更改前 2 个C 包装器以任何方式。我正在尝试停止使用 mat::at,因此学习如何使用此函数打印矩阵会很有帮助。
245
10
255
10
245
255
10
245
【问题讨论】:
-
在该代码中的某处,您将数组的每个元素转换为无符号字符。 unsigned char 只能表示 0 到 255 之间的数字。当您将 501 分配给 unsigned char 时,结果数字是 245。
-
@user3386109 感谢您的加入,你能帮我让它做我想做的事吗..即使我找到了 uchar 它仍然只打印 5 个数字...cv_Mat_ptr_index 是一个 uchar*返回。
-
评论太多了,所以我发布了一个答案。