直接使用Mat的构造函数,把指针的位置赋给下面中的data,由于char的范围是-128~127,应该用uchar类型。要构造一个数据指针指向char数组的Mat。函数原型如下:
Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);
Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);
完整代码
// matTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/nonfree/features2d.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/legacy/legacy.hpp"
#include<math.h>
#include <string>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
uchar c[100][100];
for(int i=0; i<100; i++)
for(int j=0; j<100; j++)
c[i][j] = (i<j)?0:255;
imshow("x", Mat(100, 100, CV_8UC1, (void *)c));
waitKey();
return 0;
}