Mat对象操作

1 、Mat 介绍

Mat对象的图像数据结构、自动分配内存、不存在内存泄漏的问题,是面向对象的数据结构。分了两个部分,头部与数据部分

2 、Mat 常用的构造函数与方法

Mat B(A) 只复制头部
Mat F = A.clone(); 全部复制
Mat G; A.copyTo(G) 全部复制

#构造函数
Mat()
Mat(int rows,int cols,int type)
Mat(Size size, int type)
Mat(int rows, int cols, int type , cost Scalar &s)
Mat(Size size int type , cost Scalar &s)
Mat(int ndims, const int * sizes, int type)
Mat(int ndims, const int * sizes, int type,cost Scalar &s))
#方法
void copyTo(Mat mat)
void convertTo(Mat dst, int type)
Mat clone()
int channels()
int depth()
bool empty();
uchar* ptr(i=0)

3 、 创建数组M

    Mat M;
    M.create(4,3,CV_8UC2);
    M = Scalar(17,127);
    cout << "M = " << endl << " " << M << endl << endl;
image.png

4、 创建数组C

at C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
image.png

5、 整体代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
#include <opencv2/core/core.hpp> 
#include <opencv2/imgcodecs.hpp> 
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char** argv) {

    Mat src, dst;
    src = imread("D:\\imageTest.jpg");
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input image", CV_WINDOW_AUTOSIZE);
    imshow("input image", src);

    Mat M;
    M.create(4,3,CV_8UC2);
    M = Scalar(17,127);
    cout << "M = " << endl << " " << M << endl << endl;

    uchar* pp = M.ptr<uchar>(0);
    printf("%d\n", *pp);

    Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    cout << "C = " << endl << " " << C << endl << endl;


    waitKey();
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容