OpenCV-利用cvPtr2D存取矩阵元素和用cvGetReal2D取矩阵元素

来源:互联网 发布:易语言ce修改器源码 编辑:程序博客网 时间:2024/06/05 16:16

1:代码如下:

#include "stdafx.h"#include "highgui.h"#include "cv.h"#include "iostream"using namespace std;void PrintMat2(CvMat*target, char * name)//第一个参数为cvMat矩阵指针,第二个参数为输出的矩阵的名字{    printf("%s:\n", name);    for (int i=0; i<target->rows; i++)    {        for (int j=0; j<target->cols;j++)        {            printf("%f\t", cvmGet(target, i,j));        }        printf("\n");    }}int main(){    float vals[]={0.866,-0.500,0.500,0.866};    CvMat rotmat;    //cvInitMatHeader(CvMat矩阵指针,行数,列数,数据类型,一维矩阵指针)    cvInitMatHeader(&rotmat,2,2,CV_32FC1,vals);    PrintMat2(&rotmat,"romat");    //cvPtr2D返回的类型默认为uchar*型,需要做相应的强制转化。cvPtr2D既能读矩阵元素又能取矩阵元素。    //uchar* cvPtr2D(const CvArr* arr,int idx0,int idx1,int* type=NULL),最后一个参数可以不用管,因为其默认为NULL。    float* p=(float*)cvPtr2D(&rotmat,1,1);    cout<<"*p的值为:"<<*p<<endl;    //p已经指向了矩阵中的该元素,既可以用p设置该元素的数,又可以用p取该元素的数    *p=3;    p--;    *p=4;    PrintMat2(&rotmat,"romat");    //double cvGetReal2D(const CvArr* arr,int idx0,int idx1)    double s=cvGetReal2D(&rotmat,1,1);    cout<<"s的值为:"<<s<<endl;    return 0;}
运行结果:



原创粉丝点击