OpenCV的一些操作,如生成随机矩阵,高斯矩阵,矩阵相乘之类的
/*
功能:说明矩阵的一些操作方法
*/
#include "cv.h"//该头文件包含了#include "cxcore.h"
#include "highgui.h"
#include <stdio.h>
void PrintMat(CvMat *A); // 显示矩阵
void GenRandn(CvMat *arr, int seed); // 生成正态分布的随机矩阵
void GenRand(CvMat *arr, int seed); // 生成[0,1]均匀分布的随机矩阵
static int cmp_func( const void* _a, const void* _b, void* userdata ); // 比较函数
void Test_Multiply(); // 测试矩阵乘法
void Test_cvGetRawData(); // 将缓存数据填入CvMat数组中
void Test_DCT(); // 计算DCT变换
void Test_Rand(); // 生成随机数
void Test_SeqSort(); // 二维序列排序
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "cvaux.lib" )
#pragma comment( lib, "highgui.lib" )
#pragma comment( lib, "cv.lib" )
int main()
{
Test_Multiply(); // pass
Test_cvGetRawData(); // pass
Test_DCT(); //pass
Test_Rand(); // pass
Test_SeqSort(); // pass
return 0;
}
// Testing: Sort 2d points in top-to-bottom left-to-right order.
//给二维序列排序
void Test_SeqSort()
{
//创建内存块,为0表示当前默认大小为64k
CvMemStorage* storage = cvCreateMemStorage(0);
//创建一动态序列
CvSeq* seq = cvCreateSeq( CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), storage );
int i;
printf("\n=== Test sequence sorting ===");
for( i = 0; i < 10; i++ )
{
CvPoint pt;
pt.x = rand() % 1000; // 1000 以内的随机数
pt.y = rand() % 1000;
//添加元素到序列尾部
cvSeqPush( seq, &pt );
}
printf("\nOriginal point set:\n");
for( i = 0; i < seq->total; i++ )
{
// cvGetSeqElem---返回索引所指定的元素指针
CvPoint* pt = (CvPoint*)cvGetSeqElem( seq, i );
printf( "(%d,%d)\n", pt->x, pt->y );
}
//使用特定的比较函数对序列中的元素进行排序
cvSeqSort( seq, cmp_func, 0 /* userdata is not used here */ );
/* print out the sorted sequence */
printf("\nAfter sorting:\n");
for( i = 0; i < seq->total; i++ )
{
CvPoint* pt = (CvPoint*)cvGetSeqElem( seq, i );
printf( "(%d,%d)\n", pt->x, pt->y );
}
cvClearSeq( seq ); // Sequence clearing should be done before storage clearing
cvReleaseMemStorage( &storage );
}
//排序函数
static int cmp_func( const void* _a, const void* _b, void* userdata )
{
CvPoint* a = (CvPoint*)_a;
CvPoint* b = (CvPoint*)_b;
int y_diff = a->y - b->y; //有多少行
int x_diff = a->x - b->x; //有多少列
return y_diff ? y_diff : x_diff;
}
// 生成随机矩阵
void Test_Rand()
{
CvMat* a = cvCreateMat( 10, 6, CV_32F ); //生成10x6矩阵
int i;
printf("\n=== Test generating random matrix ===");
for(i=0;i<5;i++)
{
GenRandn(a, i); //调用
PrintMat(a);
}
cvReleaseMat(&a);
}
// 显示矩阵
void PrintMat(CvMat* A)
{
int i,j;
//printf("\nMatrix = :");
for(i=0;i<A->rows;i++) //行
{
printf("\n");
switch( CV_MAT_DEPTH(A->type) )
{
case CV_32F:
case CV_64F:
for(j=0;j<A->cols;j++) //列
//获取2维数组的元素
printf("%9.3f ", (float) cvGetReal2D( A, i, j ));
break;
case CV_8U:
case CV_16U:
for(j=0;j<A->cols;j++)
printf("%6d",(int)cvGetReal2D( A, i, j ));
break;
default:
break;
}
}
printf("\n");
}
//生成[0,1]区间均匀分布的随机矩阵
void GenRand(CvMat* arr, int seed)
{
// let‘s noisy_screen be the floating-point 2d array that is to be "crapped"
CvRandState rng;
// initialize random generator
rng.state = cvRNG(0xffffffff);
cvRandInit( &rng,
0, 1, // use dummy parameters now and adjust them further
seed, // use input seed here
CV_RAND_UNI // specify uniform type
);
//用随机数填充矩阵
cvRandArr( &rng.state, arr, CV_RAND_UNI, cvRealScalar(0), cvRealScalar(1) );
// RNG state does not need to be deallocated
}
//生成标准正态分布的随机矩阵
void GenRandn(CvMat* arr, int seed)
{
// let‘s noisy_screen be the floating-point 2d array that is to be "crapped"
CvRandState rng;
// modify RNG to make it produce normally distributed values
rng.state = cvRNG(0xffffffff);
cvRandInit( &rng,
0, 1, // use dummy parameters now and adjust them further
seed, // use input seed here
CV_RAND_NORMAL // specify uniform type
);
// fill random numbers to arr, with mean zero and variance one
//注意标志CV_RAND_NORMAL是表示正态分布或高斯分布
cvRandArr( &rng.state, arr, CV_RAND_NORMAL,
cvRealScalar(0), // average intensity
cvRealScalar(1) // deviation of the intensity
);
// RNG state does not need to be deallocated
}
// Test matrix multiply
void Test_Multiply() //main()函数第一个被调用
{
double a[] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12 };
double b[] = { 1, 5, 9,
2, 6, 10,
3, 7, 11,
4, 8, 12 };
double c[9];
CvMat Ma, Mb, Mc;
printf("\n=== Test multiply ===");
cvInitMatHeader( &Ma, 3, 4, CV_64FC1, a, CV_AUTOSTEP );
cvInitMatHeader( &Mb, 4, 3, CV_64FC1, b, CV_AUTOSTEP );
cvInitMatHeader( &Mc, 3, 3, CV_64FC1, c, CV_AUTOSTEP );
cvMatMulAdd( &Ma, &Mb, 0, &Mc );
PrintMat(&Ma);//调用
PrintMat(&Mb);
PrintMat(&Mc);
return;
}
// Get raw data from data buffer and pass them to a matrix
void Test_cvGetRawData()
{
float* data;
int step;
float a[] = { 1, 2, 3, 4,
-5, 6, 7, 8,
9, -10, -11, 12 };
CvMat array;
CvSize size;
int x, y;
printf("\n=== Test get raw data ===");
//cvInitMatHeader 初始化矩阵头
//CvMat* cvInitMatHeader( CvMat* mat, int rows, int cols, int type,void* data=NULL, int step=CV_AUTOSTEP );
cvInitMatHeader( &array, 3, 4, CV_32FC1, a, CV_AUTOSTEP );
cvGetRawData( &array, (uchar**)&data, &step, &size );
step /= sizeof(data[0]);
printf("\nCvMat = ");
PrintMat(&array); //调用
printf("\nData = ");
for( y = 0; y < size.height; y++, data += step )
{
printf("\n");
for( x = 0; x < size.width; x++ )
{
//fabs---Calculates the absolute value of the floating-point argument
//求绝对值
data[x] = (float)fabs(data[x]);
printf("%8.2f",data[x]);
}
}
printf("\n");
return;
}
// test 1-d and 2-d dct transform
void Test_DCT()
{
float data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
CvMat a;
a = cvMat(2,4,CV_32FC1,data);//2×4数组
printf("\n=== Test DCT ===");
printf("\nOriginal matrix = ");
PrintMat(&a); //调用
//cvDCT 执行一维或者二维浮点数组的离散馀弦变换或者离散反馀弦变换
cvDCT(&a, &a, CV_DXT_FORWARD);
printf("\n2-D DCT = "); PrintMat(&a);//1D 或者 2D 馀弦变换
cvDCT(&a, &a, CV_DXT_INVERSE);
printf("\n2-D IDCT = "); PrintMat(&a);//1D or 2D 反馀弦变换
}
http://www.verydemo.com/demo_c291_i6212.html
http://blog.csdn.net/cc1949/article/details/22476251 矩阵相乘multi
http://www.cnblogs.com/DreamUp/archive/2010/07/27/1786225.html 矩阵相乘的一些运算acm题 pku