opencv的矩阵操作

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;
}
运行结果如下图



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值