您的位置:首页 > 运维架构

OpenCV学习之CvMat的用法详解及实例(三)

2015-01-30 20:12 483 查看
本文转载自 LOVE笨笨猪  http://blog.sina.com.cn/s/blog_74a459380101obhm.html

10.修改矩阵的形状——cvReshape的操作
经实验表明矩阵操作的进行的顺序是:首先满足通道,然后满足列,最后是满足行。

注意:这和Matlab是不同的,Matlab是行、列、通道的顺序。

我们在此举例如下:

对于一通道:

// 1 channel

CvMat *mat, mathdr;

double data[] = { 11, 12, 13, 14,

                   21, 22, 23, 24,

                   31, 32, 33, 34 };

CvMat* orig = &cvMat( 3, 4, CV_64FC1, data );
//11 12 13 14
//21 22 23 24
//31 32 33 34
mat = cvReshape( orig, &mathdr, 1, 1 ); // new_ch, new_rows
cvDoubleMatPrint( mat ); // above
// 11 12 13 14 21 22 23 24 31 32 33 34
mat = cvReshape( mat, &mathdr, 1, 3 ); // new_ch, new_rows
cvDoubleMatPrint( mat ); // above
//11 12 13 14
//21 22 23 24
//31 32 33 34
mat = cvReshape( orig, &mathdr, 1, 12 ); // new_ch, new_rows
cvDoubleMatPrint( mat ); // above
// 11
// 12
// 13
// 14
// 21
// 22
// 23
// 24
// 31
// 32

// 33

// 34

mat = cvReshape( mat, &mathdr, 1, 3 ); // new_ch, new_rows

cvDoubleMatPrint( mat ); // above

//11 12 13 14

//21 22 23 24

//31 32 33 34

mat = cvReshape( orig, &mathdr, 1, 2 ); // new_ch, new_rows

cvDoubleMatPrint( mat ); // above

//11 12 13 14 21 22

//23 24 31 32 33 34

mat = cvReshape( mat, &mathdr, 1, 3 ); // new_ch, new_rows

cvDoubleMatPrint( mat ); // above

//11 12 13 14

//21 22 23 24

//31 32 33 34

mat = cvReshape( orig, &mathdr, 1, 6 ); // new_ch, new_rows

cvDoubleMatPrint( mat ); // above

// 11 12

// 13 14

// 21 22

// 23 24

// 31 32

// 33 34

mat = cvReshape( mat, &mathdr, 1, 3 ); // new_ch, new_rows

cvDoubleMatPrint( mat ); // above

//11 12 13 14

//21 22 23 24

//31 32 33 34

// Use cvTranspose and cvReshape( mat, &mathdr, 1, 2 ) to get

// 11 23

// 12 24

// 13 31

// 14 32

// 21 33

// 22 34

// Use cvTranspose again when to recover

对于三通道

// 3 channels

CvMat mathdr, *mat;

double data[] = { 111, 112, 113, 121, 122, 123,211, 212, 213, 221, 222, 223 };

CvMat* orig = &cvMat( 2, 2, CV_64FC3, data );

//(111,112,113) (121,122,123)

//(211,212,213) (221,222,223)

mat = cvReshape( orig, &mathdr, 3, 1 ); // new_ch, new_rows

cv3DoubleMatPrint( mat ); // above
// (111,112,113) (121,122,123) (211,212,213) (221,222,223)
// concatinate in column first order
mat = cvReshape( orig, &mathdr, 1, 1 );// new_ch, new_rows
cvDoubleMatPrint( mat ); // above
// 111 112 113 121 122 123 211 212 213 221 222 223
// concatinate in channel first, column second, row third
mat = cvReshape( orig, &mathdr, 1, 3); // new_ch, new_rows
cvDoubleMatPrint( mat ); // above
//111 112 113 121
//122 123 211 212
//213 221 222 223
// channel first, column second, row third
mat = cvReshape( orig, &mathdr, 1, 4 ); // new_ch, new_rows
cvDoubleMatPrint( mat ); // above
//111 112 113
//121 122 123
//211 212 213
//221 222 223
// channel first, column second, row third
// memorize this transform because this is useful to
// add (or do something) color channels
CvMat* mat2 = cvCreateMat( mat->cols, mat->rows, mat->type );
cvTranspose( mat, mat2 );

cvDoubleMatPrint( mat2 ); // above

//111 121 211 221

//112 122 212 222

//113 123 213 223

cvReleaseMat( &mat2 ); 

11.计算色彩距离
我们要计算img1,img2的每个像素的距离,用dist表示,定义如下

IplImage *img1 = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 3 );

IplImage *img2 = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 3 );

CvMat *dist = cvCreateMat( h, w, CV_64FC1 );

比较笨的思路是:cvSplit->cvSub->cvMul->cvAdd

代码如下:

IplImage *img1B = cvCreateImage( cvGetSize(img1), img1->depth, 1 );

IplImage *img1G = cvCreateImage( cvGetSize(img1), img1->depth, 1 );

IplImage *img1R = cvCreateImage( cvGetSize(img1), img1->depth, 1 );

IplImage *img2B = cvCreateImage( cvGetSize(img1), img1->depth, 1 );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv