OpenCV Mat实例详解 四

这篇具有很好参考价值的文章主要介绍了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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

我们感兴趣的是载入图片左下角的那颗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;
}

     试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例最下面 的图片即为调用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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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

#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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

        可以看出已实现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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,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;
    }
    waitKey(0);
    return 0;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,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;
    }

    waitKey(0);
    return 0;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,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();
    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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,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] = {};
    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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,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;
    }
    
    
    waitKey(0);
    return 0;
}

 试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,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>();
    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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,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;
    waitKey(0);
    return 0;
}

试运行,结果如下: 

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

再修改上面的示例代码,插入一行修改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;
}

试运行,结果如下: 

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

可以看出,当生成新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;
}

试运行,结果如下:  

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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

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

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

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

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

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

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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;
}

 试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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;
}

试运行,结果如下:

OpenCV Mat实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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实例详解 四,opencv,人工智能,计算机视觉,OpenCV Mat实例详解四,Mat常用成员函数用法详解,Mat类成员函数用法示例

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

        本篇博文示例是基于OpenCV4.8(opencv目录位于d盘根目录下)及VS2022。示例源码已上传到CSDN,其链接为:https://download.csdn.net/download/billliu66/88839772文章来源地址https://www.toymoban.com/news/detail-831799.html

到了这里,关于OpenCV Mat实例详解 四的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [当人工智能遇上安全] 9.基于API序列和深度学习的恶意家族分类实例详解

    您或许知道,作者后续分享网络安全的文章会越来越少。但如果您想学习人工智能和安全结合的应用,您就有福利了,作者将重新打造一个《当人工智能遇上安全》系列博客,详细介绍人工智能与安全相关的论文、实践,并分享各种案例,涉及恶意代码检测、恶意请求识别、

    2024年02月04日
    浏览(58)
  • [当人工智能遇上安全] 8.基于API序列和机器学习的恶意家族分类实例详解

    您或许知道,作者后续分享网络安全的文章会越来越少。但如果您想学习人工智能和安全结合的应用,您就有福利了,作者将重新打造一个《当人工智能遇上安全》系列博客,详细介绍人工智能与安全相关的论文、实践,并分享各种案例,涉及恶意代码检测、恶意请求识别、

    2024年02月09日
    浏览(38)
  • opencv Mat详解

    我们有多种方法从现实世界获取数字图像:数码相机、扫描仪、计算机断层扫描和磁共振成像等。在任何情况下,人类看到的都是图像。当将其转换为数字图像在设备中进行存储时,我们记录的是图像中每个点的数值。 例如,在上面的图像中,可以看到汽车的镜子只是一个包

    2024年02月07日
    浏览(36)
  • OpenCV快速入门七:Mat详解

    一切图像皆Mat: Mat是一个类,由矩阵头——说明书(尺寸、存储方法、存储地址、引用次数)和指向存储所有像素值的矩阵指针构成。 1.Mat基本结构 以C++为例 1.构造法 也可用ones,zeros,创建空白图像: 但三通道使用ones只赋值第一个通道,要全部赋值可用Scalar(B,G,R) 2.克隆 3

    2024年02月05日
    浏览(35)
  • 阶段五:深度学习和人工智能(学习人工智能的应用领域,如自然语言处理,计算机视觉等)

    Python是人工智能领域最流行的编程语言之一,它具有简单易学、功能强大、库丰富等优点,因此在自然语言处理、计算机视觉等领域得到了广泛应用。 自然语言处理 自然语言处理是人工智能领域的一个重要分支,它主要研究如何让计算机理解和处理人类语言。Python在自然语

    2024年02月04日
    浏览(76)
  • 深入探索人工智能与计算机视觉

    在当今数字化时代,人工智能(AI)和计算机视觉(CV)作为两大前沿技术,正以惊人的速度改变着我们的生活。本文将深入探讨人工智能与计算机视觉的关系、应用以及未来发展方向。 1. 人工智能与计算机视觉的关系 人工智能是一门涵盖众多技术领域的学科,旨在使计算机

    2024年04月14日
    浏览(57)
  • 探索人工智能 | 智能推荐系统 未来没有人比计算机更懂你

    智能推荐系统(Recommendation Systems)利用机器学习和数据挖掘技术,根据用户的兴趣和行为,提供个性化推荐的产品、内容或服务。 智能推荐系统是一种利用机器学习和数据分析技术的应用程序,旨在根据用户的兴趣、偏好和行为模式,向其推荐个性化的产品、服务或内容。

    2024年02月13日
    浏览(46)
  • 【人工智能课程】计算机科学博士作业三

    来源:李宏毅2022课程第10课的作业 图片攻击是指故意对数字图像进行修改,以使机器学习模型产生错误的输出或者产生预期之外的结果。这种攻击是通过将微小的、通常对人类难以察觉的扰动应用于输入图像来实现的。图片攻击是对深度学习系统中的鲁棒性和安全性的一种测

    2024年03月16日
    浏览(72)
  • 【人工智能课程】计算机科学博士作业一

    模型拟合:用深度神经网络拟合一个回归模型。从各种角度对其改进,评价指标为MSE。 掌握技巧: 熟悉并掌握深度学习模型训练的基本技巧。 提高PyTorch的使用熟练度。 掌握改进深度学习的方法。 数据集下载: Kaggle下载数据: https://www.kaggle.com/competitions/ml2022spring-hw1 百度云

    2024年01月23日
    浏览(58)
  • hnu计算机与人工智能概论5.6

    最近有点忙,好久没更新了,大家见谅!最后一关howell也做不出来  第1关:数据分析基础 1.将scores.xls文件读到名为df的dataframe中 2.添加平均分列:考勤、实验操作、实验报告的平均 3.输出前3行学生的平均分列表,控制小数点后两位 4.输出学生人数和班级数 5.分别输出实验报

    2024年02月04日
    浏览(54)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包