1、 创建一个大小为100*100的3通道2维矩阵,并将其所有数据置0,然后在上面画一个圆形并显示这幅图像
<pre name="code" class="cpp">#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "stdio.h"
int main()
{
//创建一个大小为100*100的3通道2维矩阵,并将其数据置0
CvMat *mat=cvCreateMat(100,100,CV_32FC3);
cvSetZero(mat);
//在创建好的矩阵上,画一个绿色的圆形图像
cvCircle(mat,cvPoint(30,30),6,cvScalar(0,200,0),1,8,0);
cvNamedWindow("org",1);
cvShowImage("org",mat);
cvWaitKey(0);
cvReleaseMat(&mat);
cvDestroyWindow("org");
//下面是两种不同的方式显示矩阵中的元素,主要是为了测试矩阵是否创建成功
// for(int i=0;i<mat->rows;i++)
// {
// /* const float *ptr=(float *)(mat.data.ptr+i*mat.step);
// for(int j=0;j<mat.cols*(mat->step/8);j++)
// printf("%.3f ",*ptr++);
// printf("\n");
// */
// for(int j=0;j<mat->cols;j++)
// {
// for(int k=0;k<mat->step/8;k++)
// printf("%.f ",((float* )(mat->data.ptr+i*mat->step))[3*j+k]);
// }
// }
return 0;
}
/*运行结果如下图*/
2、创建一个拥有三通道的二维字节类型矩阵,大小为100*100,并将所有值赋为0,通过函数cvPtr2D将指针指向中间的通道(“绿色”)。以(20,5)与(40,20)为顶点画一个绿色的长方形。
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main()
{
IplImage * mat = cvCreateImage( cvSize(100,100), IPL_DEPTH_8U, 3);//创建一个IplImage对象
cvZero(mat);//将像素值全部置为0
int top=20,left=5,bottom=40,right=20;
//两条竖线
for(;top<=bottom;top++)
{
*(cvPtr2D(mat,top,left)+2)=255;
*(cvPtr2D(mat,top,right)+2)=255;
}
top=20;
//两条横线
for(;left<=right;left++)
{
*(cvPtr2D(mat,top,left)+1)=255;
*(cvPtr2D(mat,bottom,left)+1)=255;
}
cvNamedWindow("org",1);
cvShowImage("org",mat);
cvWaitKey(0);
cvReleaseImage(&mat);
cvDestroyWindow("org");
return 0;
}
运行结果如下图