代码测试:unsigned char*图像数据转换成OpenCV中Mat类型

直接使用Mat的构造函数,把指针的位置赋给下面中的data就OK了:

Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);

代码为matTest.cpp

 

// 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;
}

运行结果:

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);

opencv下char数组数据显示_百度知道 https://zhidao.baidu.com/question/304116674485151964.html

char的范围是-128~127,应该用uchar类型。构造一个数据指针指向char数组的Mat。

原文地址:https://www.cnblogs.com/wxl845235800/p/9111355.html