zoukankan      html  css  js  c++  java
  • OpenCV学习笔记6_GetInCvMat_访问CvMat数据块

    访问CvMat数据块

    矩阵的维度与通道

    GetInCvMat.c  

    注:缺点:这个访问的效率是比较低,两个for()循环的原因,但是它很好的说明了维度和通道的含义

    #include "stdafx.h"
    
    #include "cv.h"
    #include "highgui.h"
    #include "cxcore.h"
    
    int main()
    {
        float data[18] = {
            1 ,2 ,3 ,4 ,5 ,6 ,
            7 ,8 ,9 ,10,11,12,
            13,14,15,16,17,18,
        };
        CvMat mat;
        cvInitMatHeader(&mat,3,6,CV_32FC1,data);    //单通道排列输出
        //cvInitMatHeader(&mat,3,3,CV_32FC2,data);    //双通道排列输出
        //cvInitMatHeader(&mat,3,2,CV_32FC3,data);    //三通道排列输出
        
        /*
        int size[3] = {2, 2, 2};
        CvMatND mat_nd;
        cvInitMatNDHeader(&mat_nd,3,size,CV_32FC1,data);    //高维的单通道,比如3维1通道
        cvInitMatNDHeader(&mat_nd,3,size,CV_32FC2,data);*/    //高维的单通道,比如3维2通道
    
    
    
        for (int y = 0; y < mat.rows; y++)
        {
            for (int x = 0; x < mat.cols; x++)
            {
                float value = cvGetReal2D(&mat, y, x);
                printf("%f ",value);//单通道
    
                /*CvScalar value = cvGet2D(&mat, y, x);
                printf("(%f  %f) ",value.val[0],value.val[1]);*/  //双通道
                /*CvScalar value = cvGet2D(&mat, y, x);
                printf("(%f  %f  %f)   ",value.val[0],value.val[1],value.val[2]);*/  //三通道
                /*CvScalar value = cvGet3D(&mat, z, y, x);
                printf("(%f  %f  %f)   ",value.val[0],value.val[1],value.val[2]);*/  //三维
                /*CvScalar value = cvGetRealND(&mat, z, y, x);
                printf("(%f  %f  %f)   ",value.val[0],value.val[1],value.val[2]);*/  //多维(三维)
            }
            printf("\n ");
        }
        return 0;
    }

    GetInCvMat.c  改进版,使用指针的偏移来访问矩阵元素,效率更高,采用。

    #include "stdafx.h"
    
    #include "cv.h"
    #include "highgui.h"
    #include "cxcore.h"
    
    int main()
    {
        float data[18] = {
            1 ,2 ,3 ,4 ,5 ,6 ,
            7 ,8 ,9 ,10,11,12,
            13,14,15,16,17,18,
        };
        CvMat mat;
        cvInitMatHeader(&mat,3,6,CV_32FC1,data);    //单通道排列输出
        //cvInitMatHeader(&mat,3,3,CV_32FC2,data);    //双通道排列输出
        //cvInitMatHeader(&mat,3,2,CV_32FC3,data);    //三通道排列输出
    
        int y = 2, x = 3;
        //int nChannel = 2;双通道排列输出
        //int nChannel = 3;三通道排列输出
    
        for (int y = 0; y < mat.rows; y++)
        {
            float* p_float = (float*)(mat.data.ptr+y*mat.step); 
    
            for (int x = 0; x < mat.cols; x++)
            {    
                float value = *(p_float+x);
                /*float value[2];
                value[0] = *(p_float+x*nChannel)
                value[1] = *(p_float+x*nChannel + 1)*/    //双通道排列输出
                /*float value[3];
                value[0] = *(p_float+x*nChannel)
                value[1] = *(p_float+x*nChannel + 1)
                value[2] = *(p_float+x*nChannel + 2)*/    //三通道排列输出
    
                printf("( %f) ",value);//单通道
            }
                /*printf("(%f  %f) ",value.val[0],value.val[1]);*/  //双通道
                /*printf("(%f  %f  %f)   ",value.val[0],value.val[1],value.val[2]);*/  //三通道
            printf("\n ");
        }
        return 0;
    }
  • 相关阅读:
    Find the Smallest K Elements in an Array
    Count of Smaller Number
    Number of Inversion Couple
    Delete False Elements
    Sort Array
    Tree Diameter
    Segment Tree Implementation
    Java Programming Mock Tests
    zz Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
    Algorithm about SubArrays & SubStrings
  • 原文地址:https://www.cnblogs.com/gaoquanning/p/3067848.html
Copyright © 2011-2022 走看看