zoukankan      html  css  js  c++  java
  • 【OpenCV学习】子矩阵操作

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

    #include "highgui.h"
    #include "cv.h"
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    void main()
    {
    int i=0;
    int j=0;
    CvRect mat_rect=cvRect(1,1,3,3);//指定取得数组子集的范围
    CvMat *mat=cvCreateMat(6,6,CV_64FC1);//原数组
    CvMat *submat=cvCreateMat(3,3,CV_64FC1);//按mat_rect指定取得的数组子集
    CvMat *mat_rows=cvCreateMat(2,6,CV_64FC1);//按一定跨度内的行取得的数组子集
    /*对这个6*6的矩阵初始化*/
    for (i=0;i<6;i++)
    {
    for (j=0;j<6;j++)
    {
    CV_MAT_ELEM(*mat,double,i,j)=i*6+j;
    }
    }
    /*打印源矩阵*/
    for (i=0;i<6;i++)
    {
    for (j=0;j<6;j++)
    {
    cout<<setw(3)<<CV_MAT_ELEM(*mat,double,i,j);
    }
    cout<<endl;
    }
    cout<<endl;
    /*取子数组,大小根据mat_rect来决定,然后打印*/
    cvGetSubRect(mat,submat,mat_rect);
    for (i=0;i<3;i++)
    {
    for (j=0;j<3;j++)
    {
    cout<<setw(3)<<CV_MAT_ELEM(*submat,double,i,j);
    }
    cout<<endl;
    }
    cout<<endl;
    /*取第一行(从0开始)取到第(3-1=)2行,并打印*/
    cvGetRows(mat,mat_rows,1,3,1);
    for (i=0;i<2;i++)
    {
    for (j=0;j<6;j++)
    {
    cout<<setw(3)<<CV_MAT_ELEM(*mat_rows,double,i,j);
    }
    cout<<endl;
    }
    cvReleaseMat(&mat);
    cvReleaseMat(&submat);
    cvReleaseMat(&mat_rows);
    }

    在OpenCV中一般取元素都用定义好的宏:
    取Mat结构的元素的宏为CV_MAT_ELEM,它取2D的矩阵最方便。
    取IplImage元素的宏为CV_IMAGE_ELEM,如取一个图像(x,y)点的灰度值:
    uchar *ptr=CV_IAMGE_ELEM(image,uchar,i,j*3);
    ptr[0],ptr[1],ptr[2]就是对应的B/G/R三个分量了。


                   作者:gnuhpc
                   出处:http://www.cnblogs.com/gnuhpc/
                   除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


    分享到:

  • 相关阅读:
    树剖
    codeforces round 589
    codeforces round 590
    code craft 20
    Ozon Tech Challenge 2020 (Div.1 + Div.2)
    codeforces round 625
    Crime HDU
    codeforces 594
    codeforces 596
    python操作mysql方法和常见问题
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2571315.html
Copyright © 2011-2022 走看看