opencv——創建矩陣並顯示


//單通道
#include <opencv2\opencv.hpp>
#include <iostream>
#include <conio.h>
int main()
{
	float data[18]={3,4,5,6,7,8,
		        9,0,1,2,1,3,
		        4,5,6,6,7,8};    /*創建數據*/
	CvMat mat ;     /*創建矩陣*/
	cvInitMatHeader(&mat ,3,6,CV_32FC1,data);   /*初始化矩陣*/
	//顯示矩陣
          for(int y=0;y<mat.rows;y++)
	{
		for( int x=0;x<mat.cols;x++)
	{double val = cvGetReal2D(&mat,y,x);
		printf("%f  ",val);}
		printf("\n");
	}
	
		_getch();
		return 0;
}

運行結果:

 

/* Initializes CvMat header */
CVAPI(CvMat*) cvInitMatHeader( CvMat* mat, int rows, int cols,
                              int type, void* data CV_DEFAULT(NULL),
                              int step CV_DEFAULT(CV_AUTOSTEP) );

//雙通道

#include <opencv2\opencv.hpp>
#include <iostream>
#include <conio.h>
int main()
{
	float data[18]={3,4,5,6,7,8,
		            9,0,1,2,1,3,
		            4,5,6,6,7,8};
	CvMat mat ;
	cvInitMatHeader(&mat ,3,3,CV_32FC2,data);
	for(int y=0;y<mat.rows;y++)
	{
		for( int x=0;x<mat.cols;x++)
	{CvScalar value = cvGet2D(&mat,y,x);
		printf("(%f %f)  ",value.val[0],value.val[1]);}
		printf("\n");
	}
	free(&mat);
		_getch();
		return 0;
}
運行結果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章