opencv03-Mat矩阵API的使用

opencv03-Mat矩阵API的使用

构造方法(具体介绍看API文档)

int main() {
    Mat m1 = Mat(200, 100, CV_8UC1);
    imshow("o1", m1);
    Mat m2 = Mat(Size(100, 200), CV_8UC1);
    imshow("o2", m2);

    Mat m3 = Mat(200, 100, CV_8UC3, Scalar(255, 0, 0));
    imshow("o3", m3);

    const int sizes[] = {200, 3};
    Mat m4 = Mat(2, sizes, CV_8UC3);
    imshow("m4", m4);

    waitKey(0);
    return 0;
}

成员方法

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

/**
 * Mat操作
 */
int main() {
    string filename = "D:/workspace/cpp_workspace/my-cv/data/img/lena.jpg";
    Mat img = imread(filename, ImreadModes::IMREAD_COLOR);

    Mat b = Mat(img); //部分复制Mat,只复制对象的头和指针,不复制数据


    /**
     * 1. int channels() 返回矩阵的channel
     */
    cout << img.channels() << endl; // 3

    Mat img2;
    cvtColor(img, img2, COLOR_BGR2GRAY); // 彩色转换为灰白
    imshow("img2", img2);
    cout << img2.channels() << endl; // 1

    /**
     *2. int depth() 返回一个矩阵元素的深度
     * img.cols:矩阵的列数
     * img.dims:矩阵的维数 >=2
     * img.rows:矩阵的行数
     * img.size:矩阵的行数*列数
     * img.row(0); //
     * bool empty(); 判断矩阵是否为空
     */
    cout << img.depth() << endl; // 0
    cout << img2.depth() << endl;// 0
    cout << img.cols << endl; // 512
    cout << img2.cols << endl; // 512

    cout << img.dims << endl; // 2 -维数
    cout << img.size << endl; // 512 x 512

    cout << img.rows << endl; // 512
    cout << img2.rows << endl; // 512
    cout << img.row(0).size << endl; // 1 x 512
    cout << img.col(0).size << endl; // 512 x 1

    // Returns true if the array has no elements.
    cout << img.empty() << endl;


    /**
     * 3. void copyTo(Mat mat) 将矩阵复制到另一个矩阵, 参数 mat为目标矩阵,如果在操作前没有适当的大小或类型,则为重新分配。
     * 该方法将矩阵数据复制到另一个矩阵。在复制数据之前,该方法调用
     *          m.create(this->size(), this->type());
     * 以便在需要时重新分配目标矩阵。While m.copyTo(m); works flawlessly, 函数不处理源矩阵和目标矩阵之间部分重叠的情况。
     * 指定操作掩码时,如果上面显示的 Mat::create 调用重新分配矩阵,在复制数据之前,新分配的矩阵将初始化为全零。
     *
     * 完全复制Mat,包括对象的头,指针和数据
     */
    Mat copyTo;
    img.copyTo(copyTo);
    imshow("copyTo", copyTo);

    /**
     * 4. void convertTo(Mat dst, int type)
     *
     */
    Mat convertTo;
    img.convertTo(convertTo, CV_8UC4);
    imshow("convertTo", convertTo);

    /**
     * 5. Mat clone(): 创建一个完整的mat的copy, 完全复制Mat,包括对象的头,指针和数据
     */
    Mat clone = img.clone();
    imshow("clone", clone);

    cout << ".............................." << endl;
    /**
     * 6. uchar* ptr(i=0)
     */
    Mat M;
    M.create(4, 3, CV_8UC2);
    M = Scalar(127, 127);
    cout << "M = " << endl << " " << M << endl << endl;
    uchar *firstRow = M.ptr<uchar>(1);
    printf("%d\n", *firstRow);

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


    cout << ".............................." << endl;
    cv::Mat image = cv::Mat::ones(5, 10, CV_8UC1); //宽5,长10
    uchar *data00 = image.ptr<uchar>(0); // data00是指向image第一行第一个元素的指针。
    uchar *data10 = image.ptr<uchar>(1); // data10是指向image第二行第一个元素的指针。
    uchar data01 = image.ptr<uchar>(0)[1]; // data01是指向image第一行第二个元素的指针。
    printf("%d\n", *data00); // 1
    printf("%d\n", data01); // 1
    cout << image << endl; //


    waitKey(0);
    return 0;
}

成员方法 create

/**
 *
 * 1. void create(int rows, int cols, int type);
 * 2. void create(Size size, int type);
 * 3. void create(int ndims, const int* sizes, int type);
 * 4. void create(const std::vector<int>& sizes, int type);
 */
int main() {

    //1. void create(int rows, int cols, int type);
    Mat m;
    m.create(2, 3, CV_8UC1);

    //3. void create(int ndims, const int* sizes, int type);
    Mat m3;
    const int sizes[] = {3, 4};
    m3.create(2, sizes, CV_8UC3);
    cout << m3 << endl;


    // 4. void create(const std::vector<int>& sizes, int type);
    Mat m4;
    std::vector<int> sizes2 = vector<int>();
    sizes2.push_back(3);
    sizes2.push_back(4);

    m4.create(sizes2, CV_8UC3);
    cout << m4 << endl;
    waitKey(0);
    return 0;
}

版权声明:本文为博主作者:奋斗的小乌龟原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/guo20082200/article/details/132028235

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2024年1月11日
下一篇 2024年1月11日

相关推荐