CvMat与IplImage读取

2014年10月2日 由 Creater 留言 »

1.单通道读取

	double a[9]={1,2,3,4,5,6,7,8,9};
	double* data=0;

	CvMat *pm=cvCreateMat(3,3,CV_64FC1);

	cvInitMatHeader(pm,3,3,CV_64FC1,a);


	cout<<"行:"<<pm->height<<"   列:"<<pm->width<<"  step: "<<pm->step<<endl;
	for(int i=0;i<pm->height;i++)
	{
		data=(double*)(pm->data.ptr +i*pm->step);
		for(int j=0;j<pm->width;j++)
		{
			printf("%3.1f ", *(data + j));
		}
		printf("\n");
	}

2.3通道读取

	double a[27]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27};
	double* data=0;

	CvMat *pm=cvCreateMat(3,3,CV_64FC3);

	cvInitMatHeader(pm,3,3,CV_64FC3,a);
	

	cout<<"行:"<<pm->height<<"   列:"<<pm->width<<"  step: "<<pm->step<<endl;
	for(int i=0;i<pm->height;i++)
	{
		data=(double*)(pm->data.ptr +i*pm->step);
		for(int j=0;j<pm->width;j++)
		{
			printf("%3.1f %3.1f %3.1f ", *(data + j*3+0), *(data + j*3+1), *(data + j*3+2));
		}
		printf("\n");
	}

3.单通道读取

	IplImage *dst = cvCreateImage(cvGetSize(pm),IPL_DEPTH_64F,1);
	cvConvert(pm, dst);
	cout<<"行:"<<dst->height<<"   列:"<<dst->width<<"  step: "<<dst->widthStep<<endl;
	double *imgData  = 0;
	for(int i=0;i<dst->height;i++)
	{
		imgData=(double*)(dst->imageData +i*dst->widthStep);
		for(int j=0;j<dst->width;j++)
		{
			printf("%3.1f ", *(imgData +j));
		}
		printf("\n");
	}

4.多通道读取

	IplImage *dst = cvCreateImage(cvGetSize(pm),IPL_DEPTH_64F,3);
	cvConvert(pm, dst);
	cout<<"行:"<<dst->height<<"   列:"<<dst->width<<"  step: "<<dst->widthStep<<endl;
	double *imgData  = 0;
	for(int i=0;i<dst->height;i++)
	{
		imgData=(double*)(dst->imageData +i*dst->widthStep);
		for(int j=0;j<dst->width;j++)
		{
			printf("%3.1f %3.1f %3.1f ", *(imgData +j*dst->nChannels +0),*(imgData +j*dst->nChannels +1),*(imgData +j*dst->nChannels +2));
		}
		printf("\n");
	}
广告位

发表评论

你必须 登陆 方可发表评论.