/* 程序名:cvmat.c
功能:说明矩阵的一些操作方法
*/
#include "cv.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(); // 二维序列排序
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()
{
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++ )
{
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 );
}
// The comparison function that returns negative, zero
// or positive value depending on the elements relation
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 );
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++)
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");
}
/*
Generate uniformly distributed random numbers.
Fill random numbers to arr, chosen from
a uniform distribution on the interval (0.0,1.0).
seed is the random generator seed.
Array arr should be pre-allocated before calling this function
*/
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
}
/*
Generate normally distributed random numbers.
Fill random numbers to arr, of a normal distribution
with mean zero and variance one.
seed is the random generator seed.
Array arr should be pre-allocated before calling this function
*/
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
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()
{
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( &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++ )
{
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);
printf("\n=== Test DCT ===");
printf("\nOriginal matrix = "); PrintMat(&a);
cvDCT(&a, &a, CV_DXT_FORWARD);
printf("\n2-D DCT = "); PrintMat(&a);
cvDCT(&a, &a, CV_DXT_INVERSE);
printf("\n2-D IDCT = "); PrintMat(&a);
}
没有合适的资源?快使用搜索试试~ 我知道了~
opencv实例源代码
共380个文件
pdb:46个
dsp:30个
dsw:30个
5星 · 超过95%的资源 需积分: 9 10 下载量 148 浏览量
2013-05-29
21:57:04
上传
评论
收藏 43.95MB ZIP 举报
温馨提示
opencv实例源代码,包含十四个基本的实例,是opencv初学者的必备资料,程序在opencv1.0下即可运行,全部可调试
资源推荐
资源详情
资源评论
收起资源包目录
opencv实例源代码 (380个子文件)
1.avi 22.62MB
bike.avi 436KB
video.avi 423KB
cvmat.bsc 2.16MB
corner.bsc 2.02MB
image.bsc 2.02MB
cvmat.c 6KB
drawing.c 5KB
filldemo.c 5KB
motiondetect.c 5KB
dft.c 4KB
morphology.c 3KB
filter.c 2KB
pyramid_segmentation.c 2KB
laplace.c 2KB
cvmatop.c 2KB
imhist.c 2KB
histeq.c 2KB
cvcam.c 1KB
invImage.c 1KB
convert.c 1KB
hello.c 491B
kmean.cpp 2KB
errhandle.cpp 2KB
houghlines.cpp 2KB
hue.cpp 2KB
imadjust.cpp 2KB
readseq.cpp 2KB
pointset.cpp 2KB
corner.cpp 2KB
edge.cpp 1KB
rotate.cpp 1KB
image.cpp 1KB
contour.cpp 1011B
logpolar.cpp 787B
storage.cpp 332B
hello.dsp 4KB
pyramid_segmentation.dsp 4KB
contour.dsp 4KB
motiondetect.dsp 4KB
houghlines.dsp 4KB
morphology.dsp 4KB
errhandle.dsp 4KB
logpolar.dsp 4KB
imadjust.dsp 4KB
pointset.dsp 4KB
filldemo.dsp 4KB
invImage.dsp 4KB
storage.dsp 4KB
readseq.dsp 4KB
drawing.dsp 4KB
cvmatop.dsp 4KB
laplace.dsp 4KB
cvcam.dsp 4KB
corner.dsp 4KB
rotate.dsp 4KB
filter.dsp 4KB
histeq.dsp 4KB
imhist.dsp 4KB
kmean.dsp 4KB
image.dsp 4KB
cvmat.dsp 4KB
edge.dsp 4KB
hue.dsp 4KB
dft.dsp 4KB
convert.dsp 3KB
pyramid_segmentation.dsw 565B
motiondetect.dsw 549B
morphology.dsw 545B
houghlines.dsw 545B
errhandle.dsw 543B
imadjust.dsw 541B
filldemo.dsw 541B
logpolar.dsw 541B
invImage.dsw 541B
pointset.dsw 541B
convert.dsw 539B
cvmatop.dsw 539B
drawing.dsw 539B
storage.dsw 539B
readseq.dsw 539B
contour.dsw 539B
laplace.dsw 539B
filter.dsw 537B
corner.dsw 537B
histeq.dsw 537B
rotate.dsw 537B
imhist.dsw 537B
cvmat.dsw 535B
kmean.dsw 535B
hello.dsw 535B
image.dsw 535B
cvcam.dsw 535B
edge.dsw 533B
dft.dsw 531B
hue.dsw 531B
cvmat.exe 204KB
errhandle.exe 200KB
rotate.exe 200KB
image.exe 200KB
共 380 条
- 1
- 2
- 3
- 4
资源评论
- 爱随心flying2015-07-23例子简单实用,但如果再全面些就更好了
yubing1234567a
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 信息办公个人求职管理系统-jobgljsp.rar
- 信息办公一流网络JSP网络管理系统 v1.0-yljsp10.rar
- chirpstack学习
- 管家婆辉煌、财贸、工贸、服装,食品,千方模拟狗
- 基于python开发的工业环境老鼠检测+源码+文档(毕业设计&课程设计&项目开发)
- USB转以太网的芯片SR9900全套设计资料包括(参考设计原理图PCB+ Linux -Windows驱动程序+量产工具)
- 信息办公XML考试系统-xmlks.rar
- 基于python开发的无人机图像目标检测+实验数据+开发文档+操作流程+源码(毕业设计&课程设计&项目开发)
- 全球智能商品管理与优化系统
- IDM下载器(电脑小工具)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功