android opencv cvgetsubrect,《学习opencv》笔记——矩阵和图像操作——cvGertDims,cvGetDimSize,cvGetRow,cvGetRow,cvGetSi...

矩阵和图像的操作

(1)cvGetDims函数

其结构

int cvGetDims(//返回矩阵的维度和每维上的大小

const CvArr* arr,//目标矩阵

int * sizes = NULL//每个维上的大小,是个数组

);

实例代码

#include

#include

#include

#include

using namespace std;

int main()

{

double a[5][5] =

{

{1,0,0,0,6},

{0,2,0,7,0},

{0,0,3,0,0},

{0,9,0,4,0},

{8,0,0,0,5}

};

CvMat va=cvMat(5,5, CV_64FC1,a);

cout<

for(int i=0;i<5;i++)

{

for(int j=0;j<5;j++)

printf("%f\t",cvmGet(&va,i,j));

cout << endl;

}

int sizes[100] = {0};

int v = cvGetDims(&va,sizes);

cout << "矩阵的维数:";

cout << v << endl;

cout << "每个维度上的大小:"<

for(int i = 0 ; i<100&&sizes[i]!=0;i++)

cout << sizes[i] << endl;

getchar();

return 0;

}

输出结果

96dbfc1f5558139494660519100adc5c.png

(2)cvGetDimSize函数

其结构

int cvGetDimSize(//返回有index指定的维度的大小

const CvArr* arr,//目标矩阵

int index//维度索引

);

实例代码

#include

#include

#include

#include

using namespace std;

int main()

{

double a[5][4] =

{

{1,0,0,0},

{0,2,0,7},

{0,0,3,0},

{0,9,0,4},

{8,0,0,0}

};

CvMat va=cvMat(5,4, CV_64FC1,a);

cout<

for(int i=0;i<5;i++)

{

for(int j=0;j<4;j++)

printf("%f\t",cvmGet(&va,i,j));

cout << endl;

}

int v = cvGetDimSize(&va,1);

cout << "矩阵的指定维数上的大小为:";

cout << v << endl;

getchar();

return 0;

}

输出结果

e2f11d733ca1341c7083c62631d9d354.png

(3)cvGetRow函数

其结构

CvMat* cvGetRow(//取出矩阵中的一行

const CvArr* arr,//目标矩阵

CvMat* submat,//矩阵指针

int row//所要取的行的索引数

);

实例代码

#include

#include

#include

#include

using namespace std;

int main()

{

double a[5][5] =

{

{1,0,0,0,0},

{0,2,0,0,0},

{0,0,3,0,0},

{0,0,0,4,0},

{0,0,0,0,5}

};

CvMat va=cvMat(5,5, CV_64FC1,a);

cout<

for(int i=0;i<5;i++)

{

for(int j=0;j<5;j++)

printf("%f\t",cvmGet(&va,i,j));

cout << endl;

}

CvMat vb =cvMat(5,5, CV_64FC1);

CvMat vc =cvMat(5,5, CV_64FC1);

vc = *(cvGetRow(&va,&vb,3));

cout << "所要取的行为:"<< endl;

for(int i=0;i

{

for(int j=0;j

printf("%f\t",cvmGet(&vc,i,j));

cout << endl;

}

getchar();

return 0;

}

输出结果

9e007632e057c2f0bfdb22b9d0cc5175.png

(4)cvGetRows函数

其结构

CvMat* cvGetRows(//取出矩阵中的一些行

const CvArr* arr,//目标矩阵

CvMat* submat,//矩阵指针

int start_row,//开始行,包含

int end_row//结束行,不包含

);

实例代码

#include

#include

#include

#include

using namespace std;

int main()

{

double a[5][5] =

{

{1,0,0,0,0},

{0,2,0,0,0},

{0,0,3,0,0},

{0,0,0,4,0},

{0,0,0,0,5}

};

CvMat va=cvMat(5,5, CV_64FC1,a);

cout<

for(int i=0;i<5;i++)

{

for(int j=0;j<5;j++)

printf("%f\t",cvmGet(&va,i,j));

cout << endl;

}

CvMat vb =cvMat(5,5, CV_64FC1);

CvMat vc =cvMat(5,5, CV_64FC1);

vc = *(cvGetRows(&va,&vb,0,3));

cout << "所要取的行为:"<< endl;

for(int i=0;i

{

for(int j=0;j

printf("%f\t",cvmGet(&vc,i,j));

cout << endl;

}

getchar();

return 0;

}

输出结果

9b9db0786e440e75d1e0629b7d848fea.png

(5)cvGetSize函数

其结构

CvSize cvGetSize(//获得图像尺寸

const CvArr* arr

);

实例代码

#include

#include

#include

#include

using namespace std;

int main()

{

IplImage *src1;

src1=cvLoadImage("5.jpg");

CvSize cs = cvGetSize(src1);

cout << "图像的尺寸为:(单位:像素)"<

cout <

cout <

getchar();

return 0;

}

输出结果

737c2bc64adbec2bc53da0a0e7299ab0.png

(6)cvGetSubRect函数

其结构

CvSize cvGetSubRect(//返回选择区域的尺寸

const CvArr* arr,//目标矩阵

CvArr* submat,//矩阵指针

CvRect rect//选择区域

);

ps:1.《学习openCV》中该函数返回值为CvSize,不过我所用的2.49中返回值是CvMat,请注意。

2. 切勿在使用submat前初始化变量,不然会造成内存溢出。

实例代码

#include

#include

#include

#include

using namespace std;

int main()

{

IplImage *res;

int subX = 0;

int subY = 0;

CvMat matSrc;

res = cvLoadImage("1.jpg", CV_LOAD_IMAGE_COLOR);

subX = res->width / 2;

subY = res->height / 2;

CvMat *pMat = cvGetSubRect(res, &matSrc, cvRect(0, 0, res->width - subX, res->height - subY));

IplImage *pSubImg = cvCreateImageHeader(cvSize(440, 660), 8, 1);

cvGetImage(pMat, pSubImg);

cvShowImage("所截图像",pSubImg);

cvShowImage("原图像", res);

cvWaitKey(0);

return 0;

}

输出结果

75b09608d0f3db6d85f569a3f2d0c0f7.png

to be continued

原文:http://blog.csdn.net/zhurui_idea/article/details/28603255

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值