代碼測試: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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM