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

图像处理之 opencv 学习---矩阵的操作的更多相关文章

  1. 图像处理之 opencv 学习---opencv 中的常用算法

    http://blog.csdn.net/lindazhou2005/article/details/1534234 文中有提到鲁棒性 http://blog.csdn.net/chary8088/a ...

  2. Opencv图像与矩阵的操作

    #include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h ...

  3. OpenCV 学习笔记 02 使用opencv处理图像

    1 不同色彩空间的转换 opencv 中有数百种关于不同色彩空间的转换方法,但常用的有三种色彩空间:灰度.BRG.HSV(Hue-Saturation-Value) 灰度 - 灰度色彩空间是通过去除彩 ...

  4. OpenCV学习笔记5

    OpenCV学习笔记5 图像变换 傅里叶变换 这里可以先学习一下卷积分,了解清除卷积的过程和实际意义,在看这一章节的内容. 原理: 傅里叶变换经常被用来分析不同滤波器的频率特性.我们可以使用 2D 离 ...

  5. opencv学习笔记(七)SVM+HOG

    opencv学习笔记(七)SVM+HOG 一.简介 方向梯度直方图(Histogram of Oriented Gradient,HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子 ...

  6. opencv学习笔记(三)基本数据类型

    opencv学习笔记(三)基本数据类型 类:DataType 将C++数据类型转换为对应的opencv数据类型 OpenCV原始数据类型的特征模版.OpenCV的原始数据类型包括unsigned ch ...

  7. opencv学习笔记(一)IplImage, CvMat, Mat 的关系

    opencv学习笔记(一)IplImage, CvMat, Mat 的关系 opencv中常见的与图像操作有关的数据容器有Mat,cvMat和IplImage,这三种类型都可以代表和显示图像,但是,M ...

  8. OpenCV 学习笔记(模板匹配)

    OpenCV 学习笔记(模板匹配) 模板匹配是在一幅图像中寻找一个特定目标的方法之一.这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否"相似",当相似度足够 ...

  9. OpenCV 学习笔记03 凸包convexHull、道格拉斯-普克算法Douglas-Peucker algorithm、approxPloyDP 函数

    凸形状内部的任意两点的连线都应该在形状里面. 1 道格拉斯-普克算法 Douglas-Peucker algorithm 这个算法在其他文章中讲述的非常详细,此处就详细撰述. 下图是引用维基百科的.ε ...

随机推荐

  1. selenium 切换窗口的几种方法

    第一种方法: 使用场景: 打开多个窗口,需要定位到新打开的窗口 使用方法: # 获取打开的多个窗口句柄 windows = driver.window_handles # 切换到当前最新打开的窗口 d ...

  2. Linux之crontab定时任务

    ****crontab简介**** 简而言之呢,crontab就是一个自定义定时器. ****crontab配置文件**** 其一:/var/spool/cron/ 该目录下存放的是每个用户(包括ro ...

  3. Centos6.5搭建git远程仓库

    远程仓库搭建 step1:安装git ```yum -y install git``` step2:创建用户git,用来运行git服务 useradd git passwd git //修改git用户 ...

  4. TOJ 1203: Number Sequence

    1203: Number Sequence Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submi ...

  5. 也来“玩”Metro UI之磁贴(二)

    继昨天的“也来“玩”Metro UI之磁贴(一)”之后,还不过瘾,今天继续“玩”吧——今天把单选的功能加进来,还有磁贴的内容,还加了发光效果(CSS3,IE9+浏览器),当然,还是纯CSS,真的要感谢 ...

  6. BZOJ 4503 两个串 ——FFT

    [题目分析] 定义两个字符之间的距离为 (ai-bi)^2*ai*bi 如果能够匹配,从i到i+m的位置的和一定为0 但这和暴力没有什么区别. 发现把b字符串反过来就可以卷积用FFT了. 听说KMP+ ...

  7. Goldbach

    Description: Goldbach's conjecture is one of the oldest and best-known unsolved problems in number t ...

  8. Log4j2异步情况下怎么防止丢日志的源码分析以及队列等待和拒绝策略分析

    org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor以下所有源码均在此类中首先我们看下log4j2异步队列的初始化 从这里面我们 ...

  9. POJ3132 Sum of Different Primes

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3473   Accepted: 2154 Description A pos ...

  10. python中单引号,双引号,三引号的比较 转载

    本文转载自http://blog.sina.com.cn/s/blog_6be8928401017lwy.html 先说1双引号与3个双引号的区别,双引号所表示的字 符串通常要写成一行 如: s1 = ...