OpenCV Mat实例详解 四

        OpenCV Mat实例详解三中详细介绍来了OpenCV Mat类的公有静态成员函数,下面介绍OpenCV Mat类的其他常用成员函数。

OpenCV Mat类常用成员函数

        Mat & adjustROI (int dtop, int dbottom, int dleft, int dright);

        dtop ROI 上边界移动值,如果为正,上边界向上移动,如果值为负,则向下移动。

        dbottom  ROI 下边界移动值,如果为正,下边界向下移动,如果值为负则向上移动。

        dleftm  ROI 左边界移动值,如果为正,左边界向左移动,如果值为负,则向右移动。

        dbottom  ROI 右边界移动值,如果为正,右边界向右移动,如果值为负,则向左移动。

        这是一个比较有用的函数,在机器视觉中作目标检测及尺寸量测时,在对ROI区域左动态调整时,会用到这个函数。

        下面来演示该函数的使用方法,新建一个空的控制台程序,在程序中加入如下代码:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    Mat src = imread("1.bmp");
    if(src.empty())
    { 
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);
    /*
    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    waitKey(0);
    return 0;
}

试运行,结果如下:

我们感兴趣的是载入图片左下角的那颗IC,但取得ROI区域时,却取入了部分别的原件图像。这种情况下就需要用到adjustROI函数来调整ROI区域。下面将程序中注释掉的程序段的注释取消,让adjustROI函数起作用,注释去掉后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    Mat src = imread("1.bmp");
    if(src.empty())
    { 
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);
    
    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    
    waitKey(0);
    return 0;
}

     试运行,结果如下:

最下面 的图片即为调用adjustROI函数调整后的ROI区域,不再包含误选的元件。

void assignTo (Mat &m, int type=-1) const;

        它提供了一种Mat type 转换方法,如果type为-1.则不做type转换,这是矩阵表达式引擎调用的内部使用的方法。

        修改上面的示例代码,来演示该函数的使用,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    Mat src = imread("1.bmp");
    if(src.empty())
    { 
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    /*
    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);
    
    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */

    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    
    waitKey(0);
    return 0;
}

试运行,结果如下:

再修改上面的示例代码,修改后如下:

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

using namespace cv;
using namespace std;

int main()
{
    Mat src = imread("1.bmp");
    if(src.empty())
    { 
        cout << "Load image failed!" << endl;
        return -1;
    }
    //imshow("src", src);
 
    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);
    
    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;

    waitKey(0);
    return 0;
}

试运行,结果如下:

        可以看出已实现src的type转换。虽然可实现type的转换,但是对于已有数据的Mat对象,这种转换可能导致原有数据没法使用及原有图像无法显示。

        at() 

    用以获取Mat对象数据矩阵某以元素的值或者给其赋值。其原型有以下几种形式;

template<typename _Tp >  _Tp &  at (int i0=0)

 template<typename _Tp >  _Tp &  at (int i0=0) const

template<typename _Tp >    _Tp &  at (int row, int col)

template<typename _Tp >   const _Tp &  at (int row, int col) const

template<typename _Tp >    _Tp & at (int i0, int i1, int i2)

template<typename _Tp >    const _Tp & at (int i0, int i1, int i2) const

template<typename _Tp >    _Tp & at (const int *idx)

template<typename _Tp >   const _Tp & at (const int *idx) const

template<typename _Tp >  _Tp &  at (const Vec< int, n > &idx) 

template<typename _Tp >  const _Tp &  at (const Vec< int, n > &idx) const 

template<typename _Tp >   _Tp &   at (Point pt)

template<typename _Tp >  const  _Tp &   at (Point pt) const

上面的at函数原型都有带const与不带const两种形式,二者的区别在于可接受的参数不同。

修改上面代码,来演示其应用,修改后得到代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    { 
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
 
    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);
    
    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    waitKey(0);
    return 0;
}

试运行,结果如下:

再修改以上代码,修改后如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    { 
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
 
    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);
    
    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    waitKey(0);
    return 0;
}

试运行,结果如下:

再修改以上代码,修改后如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    { 
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
 
    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);
    
    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }

    waitKey(0);
    return 0;
}

试运行,结果如下:

再修改上面代码,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    Mat m = Mat(5, 5, CV_8UC1);
    int const length = m.cols * m.rows*m.channels();
    int mdata[25] = {};
    for (size_t i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    
    waitKey(0);
    return 0;
}

试运行,结果如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    Mat m = Mat(5, 5, CV_8UC1);
    int const length = m.cols * m.rows*m.channels();
    Point mdata[25] = {};
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    
    
    waitKey(0);
    return 0;
}

试运行,结果如下:

再修改上面代码,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    Mat m = Mat(5, 5, CV_8UC1);
    int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    Vec<int, 25> mdata;
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    
    
    waitKey(0);
    return 0;
}

 试运行,结果如下:

template<typename _Tp >  MatIterator_< _Tp >  begin ()

template<typename _Tp >  MatIterator_< _Tp >  begin () const

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    Mat m = Mat(5, 5, CV_8UC1);
    typedef Vec<uchar, 1> vec;
    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout << *it1_start;
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    waitKey(0);
    return 0;
}

试运行,结果如下:

再修改上面代码,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    Mat m = Mat(5, 5, CV_8UC1);
    //typedef Vec<uchar, 1> vec;
    MatIterator_<uchar> it_start = m.begin<uchar>();
    MatIterator_<uchar> it_end = m.end<uchar>();

    MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    MatConstIterator_<uchar> it1_end = m.end<uchar>();
    int i = 0;
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<(int) *it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    waitKey(0);
    return 0;
}

试运行,结果如下:

再修改上面代码,修改后的代码如下: 

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    waitKey(0);
    return 0;
}

试运行,结果如下: 

int channels () const

返回Mat对象数据矩阵的通道数,该函数以多次出现再前面的示例中,这里就不再做演示。

int  checkVector (int elemChannels, int depth=-1, bool requireContinuous=true) const

        elemChannels    矩阵应具有的通道数或列数。对于2-D矩阵,当矩阵只有1列时,它应该具有elemChannels通道;当矩阵只有1个通道时,它应该有elemChannels列。对于三维矩阵,它应该只有一个通道。此外,如果平面的数量不是一个,那么每个平面内的行数必须是1;如果每个平面内的行数不是1,则平面数必须是1。

        depth                   矩阵元素位深度,缺省位-1,对任意深度设置位-1是合适的。

        requireContinuous 如果设置位true,矩阵需是连续的。

        返回值  如果不满足需求条件返回-1,否则返回矩阵元素个数,注意元素可能有多个通道。

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    */
    
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    cout << "src channels:  " << src.channels() << endl;
    cout << src.checkVector(src.channels(), -1, true)<<endl;
    
    waitKey(0);
    return 0;
}

试运行,结果如下:

CV_NODISCARD_STD Mat clone () const

返回一个克隆的Mat对象。

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    */
    
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    cout << "src channels:  " << src.channels() << endl;
    //cout << src.checkVector(src.channels(), -1, true)<<endl;

    Mat dst = src.clone();

    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;
    cout << " dst channels:  " << dst.channels() << endl;
    
    waitKey(0);
    return 0;
}

试运行,结果如下:

Mat  col (int x) const

为指定的Mat的对象创建矩阵列头,该方法为指定的矩阵生成一个新的头,这是一个O(1)运算,与矩阵大小无关,x无论输入多大(0~源的cols范围内),新生成的矩阵cols都为1。新矩阵的基础数据与原始矩阵共享。

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    */
    
    /*
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    cout << "src channels:  " << src.channels() << endl;
    //cout << src.checkVector(src.channels(), -1, true)<<endl;

    Mat dst = src.clone();

    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;
    cout << " dst channels:  " << dst.channels() << endl;
    */

    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;

    Mat dst = src.col(5);
    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;

    
    waitKey(0);
    return 0;
}

试运行,结果如下:

再修改上面的示例代码,插入一行修改dst cols的代码,修改后的代码如下: 

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    */
    
    /*
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    cout << "src channels:  " << src.channels() << endl;
    //cout << src.checkVector(src.channels(), -1, true)<<endl;

    Mat dst = src.clone();

    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;
    cout << " dst channels:  " << dst.channels() << endl;
    */

    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;

    Mat dst = src.col(5); 

    dst.cols = 100;
    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;

    
    waitKey(0);
    return 0;
}

试运行,结果如下: 

可以看出,当生成新Mat矩阵头后可以通过修改cols值,来调整新生成的Mat对象的矩阵数据范围。

Mat  colRange (int startcol, int endcol) const

Mat colRange (const Range &r) const

这两个函数与上一个函数的作用差不多,只不过是这两个一开始就指定cols范围。

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    */
    
    /*
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    cout << "src channels:  " << src.channels() << endl;
    //cout << src.checkVector(src.channels(), -1, true)<<endl;

    Mat dst = src.clone();

    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;
    cout << " dst channels:  " << dst.channels() << endl;
    */

    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;

    //Mat dst = src.col(5); 

    Mat dst = src.colRange(5,150);
    Mat dst1 = src.colRange(Range(50,220));
    //dst.cols = 100;
    imshow("dst", dst);
    imshow("dst1", dst1);
    moveWindow("dst1", 0, 200);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;


    cout << " dst1 cols: " << dst1.cols << endl;
    cout << " dst1 rows: " << dst1.rows << endl;
    cout << " dst1 type: " << dst1.type() << endl;

    waitKey(0);
    return 0;
}

试运行,结果如下:  

用上面三个函数生成的Mat对象能否调用adjustROI函数来调整ROI区域呢?答案是肯定的。

void convertTo (OutputArray m, int rtype, double alpha=1, double beta=0) const

m 输出目标Mat 对象,如果其类型及大小不合适,将会被重新分配

alpha 像素BGR值比例缩放因子,可改变图像的明暗,不能缩改变图像大小,缺省为1

beta 添加到缩放因子的可选增量。

该方法将源像素值转换为目标数据类型。 saturate_cast<> 应用在末尾以避免可能的溢出,如下:

 修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

void  copySize (const Mat &m)

内部使用函数;正确地重新分配 _size、_step 数组

m 源Mat对象

 修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    */
    
    /*
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    cout << "src channels:  " << src.channels() << endl;
    //cout << src.checkVector(src.channels(), -1, true)<<endl;

    Mat dst = src.clone();

    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;
    cout << " dst channels:  " << dst.channels() << endl;
    */

    /**
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;

    //Mat dst = src.col(5); 

    Mat dst = src.colRange(5,150);
    Mat dst1 = src.colRange(Range(50,220));
    //dst.cols = 100;
    imshow("dst", dst);
    imshow("dst1", dst1);
    moveWindow("dst1", 0, 200);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;

    cout << " dst1 cols: " << dst1.cols << endl;
    cout << " dst1 rows: " << dst1.rows << endl;
    cout << " dst1 type: " << dst1.type() << endl;
    */
    
    /**
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    src.convertTo(src, src.type(), 1.2, 0);
    imshow("src new", src);
    */
    
    Mat src = Mat(5, 5, CV_8UC1);
    cout << src.size() << endl;

    Mat dst = Mat(8, 8, CV_8UC3);
    
    src.copySize(dst);
    cout << src.size() << endl;
    
   
    waitKey(0);
    return 0;
}

 试运行,结果如下:

void copyTo (OutputArray m) const

void  copyTo (OutputArray m, InputArray mask) const

拷贝到另一个Mat对象

m 目标Mat对象

mask 与 *this 大小相同的操作掩码。它的非零元素表示哪些矩阵元素需要复制。掩码必须为 CV_8U 类型,并且可以有 1 个或多个通道。

 修改上面的示例代码,来演示该函数的用法,修改后的代码如下:

// OpenCVMatTest4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

int main()
{
    /*
    Mat src = imread("1.bmp");
    if(src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    */
    
    /*
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    cout << "src channels:  " << src.channels() << endl;
    //cout << src.checkVector(src.channels(), -1, true)<<endl;

    Mat dst = src.clone();

    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;
    cout << " dst channels:  " << dst.channels() << endl;
    */

    /**
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;

    //Mat dst = src.col(5); 

    Mat dst = src.colRange(5,150);
    Mat dst1 = src.colRange(Range(50,220));
    //dst.cols = 100;
    imshow("dst", dst);
    imshow("dst1", dst1);
    moveWindow("dst1", 0, 200);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;

    cout << " dst1 cols: " << dst1.cols << endl;
    cout << " dst1 rows: " << dst1.rows << endl;
    cout << " dst1 type: " << dst1.type() << endl;
    */
    
    /**
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    src.convertTo(src, src.type(), 1.2, 0);
    imshow("src new", src);
    */
    
    /*
    Mat src = Mat(5, 5, CV_8UC1);
    cout << src.size() << endl;

    Mat dst = Mat(8, 8, CV_8UC3);
    
    src.copySize(dst);
    cout << src.size() << endl;
    */
   
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    Mat dst;
    src.copyTo(dst);
    imshow("dst", dst);

    Mat mask(src.size(), CV_8UC1, cv::Scalar(255));
    mask(Rect(0, 0, src.cols/2, src.rows / 2)) = Scalar(0);

    Mat dst1(src.size(), src.type());
    src.copyTo(dst1, mask);
    imshow("dst1", dst1);

    waitKey(0);
    return 0;
}

试运行,结果如下:

void create (int rows, int cols, int type)

void  create (Size size, int type)

void create (int ndims, const int *sizes, int type)

void  create (const std::vector< int > &sizes, int type)

以上四个函数都是用以生成新的Mat对象,差别仅是接受的参数不同。

rows 新Mat对象的rows

cols 新Mat对象的cols

type 新Mat兑现的type

size 含新Mat对象rows、cols的vector对象

修改上面的示例代码,来演示该函数的用法,修改后的代码如下:


        return -1;
    }
    imshow("src", src);

    cout << "src cols: "<<src.cols << endl;
    cout << "src rows: "<<src.rows << endl;
    cout << "src type: "<< src.type() << endl;
    */

    /*
    Rect rec(0, 130, 120,90);
    Mat roi = Mat(src, rec);
    imshow("roi", roi);
    moveWindow("roi", 300, 300);

    roi.adjustROI(-5, 0, -5, -10);
    imshow("roi adj", roi);
    moveWindow("roi adj", 500, 500);
    */
    /*
    Mat dst;
    src.assignTo(dst, -1);
    imshow("dst",dst);
    */

    /*
    src.assignTo(src, CV_16FC3);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    */

    /*
    int mdata[] = { 10,10 };
    Mat m = Mat(1, mdata, CV_8UC1);
    for (size_t i = 0; i < 10; i++)
    {
        m.at<uchar>(i) = i;
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << (int)m.at<uchar>(i) << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC1);
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            m.at<uchar>(i, j) = i + j;
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
            cout << (int)m.at<uchar>(i, j);
        cout << endl;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            m.at<uchar>((i, j),0) = j;
            m.at<uchar>((i, j), 1) = j + 1;
            m.at<uchar>((i, j), 2) = j+ 2;
        }
    }
    for (size_t i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            cout << (int)m.at<uchar>((i, j),0);
            cout << (int)m.at<uchar>((i, j), 1);
            cout << (int)m.at<uchar>((i, j), 2) << "  ";
        }
        cout << endl;
    }
    */
 
    
    //Mat m = Mat(5, 5, CV_8UC1);
    //int const length = m.cols * m.rows*m.channels();
    //Point mdata[25] = {};
    //Vec<int, 25> mdata;
   
    /*
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            mdata[i * 5 + j].x = i;
            mdata[i * 5 + j].y = j;
        }
    }
    */

    /*
    for (int i = 0; i < length; i++)
    {
        mdata[i] = i;
    }
    for (size_t i = 0; i < length; i++)
    {
        m.at<uchar>(mdata[i]) = (uchar)i;
        
    }
    for (int i = 0; i < length; i++)
    {
        cout << (int)m.at<uchar>(mdata[i]) << endl;
    }
    */

    //Mat m = Mat(5, 5, CV_8UC1);
    
    //typedef Vec<uchar, 1> vec;
    
    //MatIterator_<uchar> it_start = m.begin<uchar>();
    //MatIterator_<uchar> it_end = m.end<uchar>();


    //MatConstIterator_<uchar> it1_start = m.begin<uchar>();
    //MatConstIterator_<uchar> it1_end = m.end<uchar>();

    
    /*
    while (it_start != it_end)
    {
        *it_start = i + 1;
        i++;
        it_start++;
    }
    */

    /*
    Mat m = Mat(5, 5, CV_8UC3);
    typedef Vec<uchar, 3> vec;

    MatIterator_<vec> it_start = m.begin<vec>();
    MatIterator_<vec> it_end = m.end<vec>();

    MatConstIterator_<vec> it1_start = m.begin<vec>();
    MatConstIterator_<vec> it1_end = m.end<vec>();
    int i = 0;
    while (it_start != it_end)
    {
        it_start[0] = 255-i;
        it_start[1] = 255 - 2 * i;
        it_start[1] = 255 - 3 * i;
        i++;
        it_start++;
    }
    while (it1_start != it1_end)
    {
        cout <<*it1_start<<"  ";
        it1_start++;
    }
    cout <<endl;
    cout << m << endl;
    */
    
    /*
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);

    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;
    cout << "src channels:  " << src.channels() << endl;
    //cout << src.checkVector(src.channels(), -1, true)<<endl;

    Mat dst = src.clone();

    imshow("dst", dst);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;
    cout << " dst channels:  " << dst.channels() << endl;
    */

    /**
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    cout << "src cols: " << src.cols << endl;
    cout << "src rows: " << src.rows << endl;
    cout << "src type: " << src.type() << endl;

    //Mat dst = src.col(5); 

    Mat dst = src.colRange(5,150);
    Mat dst1 = src.colRange(Range(50,220));
    //dst.cols = 100;
    imshow("dst", dst);
    imshow("dst1", dst1);
    moveWindow("dst1", 0, 200);

    cout << " dst cols: " << dst.cols << endl;
    cout << " dst rows: " << dst.rows << endl;
    cout << " dst type: " << dst.type() << endl;

    cout << " dst1 cols: " << dst1.cols << endl;
    cout << " dst1 rows: " << dst1.rows << endl;
    cout << " dst1 type: " << dst1.type() << endl;
    */
    
    /**
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    src.convertTo(src, src.type(), 1.2, 0);
    imshow("src new", src);
    */
    
    /*
    Mat src = Mat(5, 5, CV_8UC1);
    cout << src.size() << endl;

    Mat dst = Mat(8, 8, CV_8UC3);
    
    src.copySize(dst);
    cout << src.size() << endl;
    */
   
    /*
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src", src);
    Mat dst;
    src.copyTo(dst);
    imshow("dst", dst);

    Mat mask(src.size(), CV_8UC1, cv::Scalar(255));
    mask(Rect(0, 0, src.cols/2, src.rows / 2)) = Scalar(0);

    Mat dst1(src.size(), src.type());
    src.copyTo(dst1, mask);
    imshow("dst1", dst1);
    */
    
    Mat src = imread("1.bmp");
    if (src.empty())
    {
        cout << "Load image failed!" << endl;
        return -1;
    }
    imshow("src 1th", src);

    src.create(src.rows-10, src.cols-10, src.type());
    imshow("src 2th", src);

    src.create(Size(src.rows, src.cols + 10), src.type());
    imshow("src 3th", src);

    int size[] = { 250,230 };
    src.create(2, size, CV_8UC3);
    imshow("src 4th", src);

    vector<int> vec(2);
    vec[0] = 230;
    vec[1] = 250;
    
    src.create(vec, CV_8UC3);
    imshow("src 5th", src);
    

    waitKey(0);
    return 0;
}

试运行,结果如下:

        由于篇幅关系,OpenCV Mat类的其他常用成员函数就暂时介绍到这里,剩余部分函数将在下篇中介绍。

        本篇博文示例是基于OpenCV4.8(opencv目录位于d盘根目录下)及VS2022。示例源码已上传到CSDN,其链接为:https://download.csdn.net/download/billliu66/88839772

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/389839.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

BUGKU-WEB game1

题目描述 题目截图如下&#xff1a; 进入场景看看&#xff1a; 是一个盖楼的游戏&#xff01; 解题思路 先看看源码&#xff0c;好像没发现什么特别的是不是要得到一定的分数才会有对应的flag&#xff1f;查看下F12&#xff0c;请求链接发现&#xff0c;这不就提示了 相…

[计算机网络]---序列化和反序列化

前言 作者&#xff1a;小蜗牛向前冲 名言&#xff1a;我可以接受失败&#xff0c;但我不能接受放弃 如果觉的博主的文章还不错的话&#xff0c;还请点赞&#xff0c;收藏&#xff0c;关注&#x1f440;支持博主。如果发现有问题的地方欢迎❀大家在评论区指正 目录 一、再谈协议…

C++动态规划-线性dp算法

莫愁千里路 自有到来风 CSDN 请求进入专栏 X 是否进入《C专栏》? 确定 目录 线性dp简介 斐波那契数列模型 第N个泰波那契数 思路&#xff1a; 代码测试&#xff1a; 三步问题 思路&#xff1a; 代码测试&#xff1a; 最小花费爬楼梯 思路…

Java:如何判断一个链表是否为回文结构?(画图+代码 详解)

一、判断思想 我们设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法&#xff0c;我们在不创建额外空间的基础上来判断是否为回文结构。 思想&#xff1a; 1、使用快慢指针法&#xff0c;找到链表的中间节点。 2、翻转中间节点的后半部分。 3、分别从头节点和尾节点向中间遍…

LeetCode刷题计划

LeetCode刷题计划 推荐 代码随想录&#xff1a;https://github.com/youngyangyang04/leetcode-master 卡码网 练习ACM模式 https://kamacoder.com/ 01 #include <iostream> using namespace std;int main() {int a ,b;while(cin>>a>>b){cout<<ab<…

从汇编分析C语言可变参数的原理,并实现一个简单的sprintf函数

C语言可变参数 使用printf等函数的时候函数原型是printf(const char* fmt, ...), 这一类参数的个数不限的函数是可变参数 使用 使用一个头文件stdarg.h, 主要使用以下的宏 typedef char * va_list;// 把 n 圆整到 sizeof(int) 的倍数 #define _INTSIZEOF(n) ( (sizeo…

IO流---字节输入输出流,字符输入输出流

IO流概述 IO流&#xff0c;即输入输出流&#xff08;Input Output Stream&#xff09;&#xff0c;是一种抽象概念&#xff0c;用来处理设备之间的数据传输问题&#xff0c;例如文件复制、文件上传、文件下载等。在Java中&#xff0c;对数据的操作通常是通过流的方式进行的&…

幻兽帕鲁云服务器搭建零基础教程,新手小白一看就会

以下教程基于阿里云服务器ECS 来搭建幻兽帕鲁游戏服务器&#xff0c;通过一键部署的方式&#xff0c;最快1分钟即可完成部署。 阿里云一键部署幻兽帕鲁的活动地址&#xff1a;1分钟畅玩&#xff01;一键部署幻兽帕鲁联机服务器 首先&#xff0c;打开阿里云的这个游戏服务器活…

【机器学习笔记】8 决策树

决策树原理 决策树是从训练数据中学习得出一个树状结构的模型。 决策树属于判别模型。 决策树是一种树状结构&#xff0c;通过做出一系列决策&#xff08;选择&#xff09;来对数据进行划分&#xff0c;这类似于针对一系列问题进行选择。决策树的决策过程就是从根节点开始&…

二维数组传参的本质(详解)

目录 一、前言二、分析本质三、总结 一、前言 有时候我们有⼀个⼆维数组的需要传参给⼀个函数的时候&#xff0c;我们是这样写的&#xff1a; #include <stdio.h> void test(int a[3][5], int r, int c) {int i 0;int j 0;for (i 0; i < r; i){for (j 0; j <…

网络安全问题概述

1 计算机网络面临的安全性威胁 两大类威胁&#xff1a;被动攻击和主动攻击。 被动攻击 指攻击者从网络上窃听他人的通信内容。 通常把这类攻击称为截获。 攻击者只是观察和分析某一个协议数据单元 PDU&#xff0c;以便了解所交换的数据的某种性质&#xff0c;但不干扰信息…

【PyQt】11-QTextEdit、QPushButton

文章目录 前言一、文本输入-QTextEdit1.1 代码1.2 运行结果 二、QPushButton2.1.1 按钮上添加文本2.1.2 按键的弹跳效果2.1.3 两个信号可以绑定一个槽。2.1.4 带图标的按键运行结果 2.1.5 按键不可用以及回车默认完整代码2.2 单选按键控件运行结果 2.3 复选框&#xff08;多选框…

1Coze平台介绍

2023年随着OpenAI推出GPT 3.5&#xff0c;AI赛道变得更加火热。GPT&#xff08;Generative Pre-trained Transformer&#xff09;是一种自然语言处理&#xff08;NLP&#xff09;模型&#xff0c;用于生成文本、理解语言和进行各种语言任务。GPT是由OpenAI开发的&#xff0c;它…

Python 异常处理及程序调试

Python 是一门功能强大而又易于学习的编程语言&#xff0c;它提供了丰富的工具和库来帮助开发者编写高效、稳定的程序。然而&#xff0c;在编写复杂的应用程序时&#xff0c;错误和异常是难以避免的。本文将介绍 Python 中的异常处理机制以及程序调试技巧&#xff0c;帮助读者提…

枚举,#define,C中程序内存区域划分

目录 一、枚举 1.1枚举类型的声明 1.2枚举类型的优点 1.3枚举类型的使用 二、#define定义常量 三、C中程序内存区域划分 一、枚举 1.1枚举类型的声明 枚举顾名思义就是⼀⼀列举。 把可能的取值⼀⼀列举。 比如我们现实生活中&#xff1a; ⼀周的星期⼀到星期日是有限…

【AIGC】Stable Diffusion 的提示词入门

一、正向提示词和反向提示词 Stable Diffusion 中的提示词通常用于指导用户对生成的图像进行控制。这些提示词可以分为正向提示词&#xff08;Positive Prompts&#xff09;和反向提示词&#xff08;Negative Prompts&#xff09;两类&#xff0c;它们分别影响图像生成过程中的…

MATLAB计算极限和微积分

一.函数与极限 计算极限&#xff1a;lim(3*x^2/(2x1))&#xff0c;x分别趋于0和1&#xff0c;代码如下&#xff1a; syms x; limit(3*x*x/(2*x1),x,0) limit(3*x*x/(2*x1),x,1) 结果分别为0和1&#xff1a; 1.计算双侧极限 计算极限&#xff1a;lim(3*x^2/(2x1))&#xff0…

输入输出自定义映射矩阵(数据结构树)

输出自定义FC其它算法实现&#xff0c;可以参考下面文章&#xff1a; https://rxxw-control.blog.csdn.net/article/details/125994252https://rxxw-control.blog.csdn.net/article/details/125994252下面我们看下我们的控制要求。在学习本篇博客之前大家可以熟悉下数据结构图…

内网横向渗透-1

目录 内网横向渗透 流量监听工具的使用 ARP欺骗 工具使用 服务密码攻击 hydra medusa ncrack hashcat 内网横向渗透 流量监听工具的使用 ARP欺骗 工具使用 ettercap 工具 可以进行arp欺骗、DNS欺骗&#xff0c;网络钓鱼等等&#xff01; driftnet -i eth0 可以用来…

GiantPandaCV | 视觉类表面缺陷检测项目相关技术总结

本文来源公众号“GiantPandaCV”&#xff0c;仅用于学术分享&#xff0c;侵权删&#xff0c;干货满满。 原文链接&#xff1a;视觉类表面缺陷检测项目相关技术总结 本文由海滨撰写&#xff0c;首发于GaintPandaCV。 零、前言 做这个方向的项目也有一段时间了&#xff0c;作为…