Mat对象的基本构建

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


using namespace std;
using namespace cv;


int main() {
    Mat src = imread("/Users/apple/Desktop/test3.png", IMREAD_COLOR);
    if (src.empty()) { // if (!src.data())
        cout << "could not load image..." << endl;
        return -1;
    }

    namedWindow("test opencv setup", CV_WINDOW_AUTOSIZE);
    imshow("test opencv setup", src);
    
    Mat dst;
//    dst = Mat(src.size(), src.type());
//    dst = Scalar(127, 0, 255);
    
    
    // 两种复制方法(形式+数据)
    //dst = src.clone();
    src.copyTo(dst);
    
    // 仅有形式,无数据
    Mat T(src); 
    imshow("output T", T);
    
    cvtColor(src, dst, CV_BGR2GRAY);
    
    imshow("output", dst);

    cout << "input image channels: " << src.channels() << endl;
    cout << "output image channels: " << dst.channels() << endl;
    
    // 获取第一个像素点
    const uchar* firstRow = dst.ptr(0);
    cout << "The first pix:" << (int)*firstRow << endl;
    
    // 获取图像像素点的长宽
    int rows = dst.rows;
    int cols = dst.cols;
    cout << "rows: " << rows << " cols: " << cols << endl;
    
    
    
    // 以下为几种构建方法
    Mat M(100, 100, CV_8UC1, Scalar(127));
    //cout << "M = : " << endl << M << endl;
    imshow("output M", M);
    
    Mat m1;
    m1.create(src.size(), src.type());
    m1 = Scalar(0, 255, 0);
    imshow("output m1", m1);
    
    
    Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    filter2D(src, dst, -1, kernel);
    imshow("new dst", dst);
    
    Mat m2 = Mat::zeros(2, 2, CV_8UC1);// 同等大小的全黑图片
    cout << "m2 = : " << endl << m2 << endl;
    
    Mat m3 = Mat::eye(2, 2, CV_8UC1);// 同等大小的对角线为1,其他为0
    cout << "m3 = : " << endl << m3 << endl;
    
    waitKey(0);
    
    return 0;
}


猜你喜欢

转载自blog.csdn.net/ringggr_h/article/details/79944445