OpenCV2.x 学习笔记02--遍历像素点操作-二维图像数据转存一维数组算法实现

版权声明: https://blog.csdn.net/qq_40025335/article/details/82799144

TIme:20180905

ENV:Vs2013、OPencv2.4.9

Function:二维图像数据转存一维数组算法实现

Athor:New(XATU)

------------------------------

1、Mat简介

2、IplImage简介

3、功能函数参考i程序代码

-------------------------------------------

1、Mat是OpenCV最基本的数据结构,Mat即矩阵(Matrix)的缩写,Mat数据结构主要包含2部分:Header和Pointer。Header中主要包含矩阵的大小,存储方式,存储地址等信息;Pointer中存储指向像素值的指针。我们在读取图片的时候就是将图片定义为Mat类型,其重载的构造函数一大堆,

    1. classCV_EXPORTSMat
    2. {
    3. public:
    4. //! default constructor
    5. Mat();
    6. //! constructs 2D matrix of the specified size and type
    7. // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
    8. Mat(int _rows, int _cols, int _type);
    9. Mat(Size _size, int _type);
    10. //! constucts 2D matrix and fills it with the specified value _s.
    11. Mat(int _rows, int _cols, int _type, const Scalar& _s);
    12. Mat(Size _size, int _type, const Scalar& _s);
    13. //! constructs n-dimensional matrix
    14. Mat(int _ndims, constint* _sizes, int _type);
    15. Mat(int _ndims, constint* _sizes, int _type, const Scalar& _s);
    16. //! copy constructor
    17. Mat(const Mat& m);
    18. //! constructor for matrix headers pointing to user-allocated data
    19. Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);
    20. Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);
    21. Mat(int _ndims, constint* _sizes, int _type, void* _data, constsize_t* _steps=0);
    22. //! creates a matrix header for a part of the bigger matrix
    23. Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
    24. Mat(const Mat& m, const Rect& roi);
    25. Mat(const Mat& m, const Range* ranges);
    26. //! converts old-style CvMat to the new matrix; the data is not copied by default
    27. Mat(const CvMat* m, bool copyData=false);
    28. //! converts old-style CvMatND to the new matrix; the data is not copied by default
    29. Mat(const CvMatND* m, bool copyData=false);
    30. //! converts old-style IplImage to the new matrix; the data is not copied by default
    31. Mat(const IplImage* img, bool copyData=false);
    32. ......
    33. }

2IplImage*是C语言操作OpenCV1.x的数据结构,在当时C操纵opencv的时候,地位等同于Mat,OpenCV为其提供了一个接口,很方便的直接将IplImage转化为Mat,即使用构造函数 

Mat(const IplImage* img, bool copyData=false);

3、功能函数参考i程序代码

//Info:Mainsfs01.cpp
//变量为初始化
//Get_UCF_Image_Format(),二维图像数据转存一维数组算法实现-。
//程序规范化
//Env:VS2013、Opencv2.4.9
//Function:
//Time:20180831

#include "stdafx.h"
#include <iostream>  
#include <fstream> 
#include <opencv2/opencv.hpp>  
#include "ImageTools.h"

using namespace std;
using namespace cv;

#define    SSize 200

void Get_UCF_Image_Format(Mat s, uchar* d)
{
    int i, j;
    int width;// widthStep;
    int height;

    //width = s->width;/* 图像宽像素数 */  
    //height = s->height;
    width = s.cols;//列长
    height = s.rows;//行
    //用指针访问像素,速度最快
    uchar *p;
    for (int i = 0; i < height; i++)
    {
        p = s.ptr<uchar>(i);//获取每行首地址
        for (int j = 0; j < width; j++)
        {
            d[i*height + j] = p[j];
        }
    }
}

有疑问欢迎留言,不定期查看

其他参考:https://blog.csdn.net/xiahouzuoxin/article/details/38298165

猜你喜欢

转载自blog.csdn.net/qq_40025335/article/details/82799144