现在的位置: 首页 > 综合 > 正文

cvmSet cvmGet

2013年10月06日 ⁄ 综合 ⁄ 共 3220字 ⁄ 字号 评论关闭
//运行参数:girl.jpg 
#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"ml.lib")
#pragma comment(lib,"cvaux.lib")
#pragma comment(lib,"cvcam.lib") 

#include "cv.h"
#include "highgui.h"
#include <stdio.h>

inline void cvDoubleMatPrint( const CvMat* mat );
int main( int argc, char** argv )
{
 IplImage* pImg; //声明IplImage指针

 //载入图像,强制转化为Gray
 if( argc == 2 && (pImg = cvLoadImage( argv[1], CV_LOAD_IMAGE_UNCHANGED)) != 0 )
 {
// 在点 (100,100) 和 (200,200) 之间绘制一矩形,边线用红色、宽度为 1
cvRectangle(pImg, cvPoint(100,100), cvPoint(200,200), cvScalar(0,0,255), 1);
// 圆心为(100,100)、半径为20. 圆周绿色、宽度为1
cvCircle(pImg, cvPoint(100,100), 20, cvScalar(0,255,0), 1);
// 在 (100,100) 和 (200,200) 之间、线宽为 1 的绿色线段
cvLine(pImg, cvPoint(100,100), cvPoint(200,200), cvScalar(0,255,0), 1);
//printf("sizeof(double)=%d\n",sizeof(double)); //=8
//CvMat* cvCreateMat(int rows, int cols, int type);
/*
 type: 矩阵元素类型. 
 按CV_<bit_depth>(S|U|F)C<number_of_channels> 方式指定.  例如: CV_8UC1 、CV_32SC2. 
 示例: 
 */
  //CvMat* M1 = cvCreateMat(4,4,CV_32FC1);
  CvMat* M1 = cvCreateMat(4,4,CV_64FC1);
  cvSetIdentity(M1);//初始化矩阵为单位矩阵
  int i,j;
   //cvmSet(M1,1,1,2.0); // Set M(i,j)
	int n       = M1->cols;
	float *data = M1->data.fl; 
	//data[i*n+j] = 3.0;
  printf("\n=====赋值为3.0==============\n");
//void  cvmSet( CvMat* mat, int row, int col, double value )
   for(i=0;i<4;i++)
		 for(j=0;j<4;j++)
			 //cvmSet(M1,i,j,3.0);
			 data[i*n+j] = 3.0;
		 
   //cvmGet(M,i,j); // Get M(i,j) 
   //CV_INLINE  double  cvmGet( const CvMat* mat, int row, int col )
   for( i=0;i<4;i++)
   {
		 for( j=0;j<4;j++)
			 printf("%f ",cvmGet(M1,i,j));
		 printf("\n"); 
   }
  printf("\n===End:赋值为3.0==============\n");

  CvMat* M2;
  M2=cvCloneMat(M1);//复制矩阵

  printf("=============================\n");
  {
	    printf("=======方式一、逐点赋值式====\n");
	    CvMat* mat = cvCreateMat( 2, 2, CV_64FC1 );
		cvZero( mat );
		cvmSet( mat, 0, 0, 1 );
		cvmSet( mat, 0, 1, 2 );
		cvmSet( mat, 1, 0, 3 );
		cvmSet( mat, 1, 1, 4 );

		cvDoubleMatPrint(mat);
		cvReleaseMat( &mat );
        printf("\n=====End:方式一、逐点赋值式====\n");
  }
  //初始化矩阵
  {
	double a[] = { 1,  2,  3,  4,
				   5,  6,  7,  8,
				   9, 10, 11, 12 };
	CvMat Ma=cvMat(3, 4, CV_64FC1, a);
	printf("===使用数组初始化====================\n"); 
	   for( i=0;i<3;i++)
	   {
			 for( j=0;j<4;j++)
				 printf("%lf ",cvmGet(&Ma,i,j));
			 printf("\n"); 
	   }
	   printf("===修改数组值=================\n"); 
		a[1*4+1] = 2.0; // Ma(i,j)=2.0;
	   for( i=0;i<3;i++)
	   {
			 for( j=0;j<4;j++)
				 printf("%f ",cvmGet(&Ma,i,j));
			 printf("\n"); 
	   }
	 printf("=====End:使用数组初始化============\n"); 

	//等价于: 
	{
		CvMat Ma;
		cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a);
	}
  }
  //printf("%x\n",M1);
  cvReleaseMat(&M1);//释放矩阵内存:CVAPI(void)  cvReleaseMat( CvMat** mat );
  //printf("%x\n",M1);//成功释放后M1被置为了NULL

  cvReleaseMat(&M2);

  cvNamedWindow( "Image", 1 ); // 创建窗口
  cvShowImage( "Image", pImg ); // 显示图像
  cvWaitKey(0);     // 等待按键


  cvDestroyWindow( "Image" );  // 销毁窗口
  cvReleaseImage( &pImg );  // 释放图像

  return 0;
 }

 return -1;
}

inline void cvDoubleMatPrint( const CvMat* mat )
{
    int i, j;
    for( i = 0; i < mat->rows; i++ )
    {
        for( j = 0; j < mat->cols; j++ )
        {
            printf( "%lf ",cvmGet( mat, i, j ) );
        }
        printf( "\n" );
    }
}

=====赋值为3.0==============
32.000008 32.000008 32.000008 32.000008
32.000008 32.000008 32.000008 32.000008
0.000000 0.000000 1.000000 0.000000
0.000000 0.000000 0.000000 1.000000

===End:赋值为3.0==============
=============================
=======方式一、逐点赋值式====
1.000000 2.000000
3.000000 4.000000

=====End:方式一、逐点赋值式====
===使用数组初始化====================
1.000000 2.000000 3.000000 4.000000
5.000000 6.000000 7.000000 8.000000
9.000000 10.000000 11.000000 12.000000
===修改数组值=================
1.000000 2.000000 3.000000 4.000000
5.000000 2.000000 7.000000 8.000000
9.000000 10.000000 11.000000 12.000000
=====End:使用数组初始化============

抱歉!评论已关闭.