opencv4-ch2读书笔记

读写图片

#include 
#include 
#include 

using namespace std;

// OpenCV includes
#include 
#include 

using namespace cv;

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

    String path = R"(D:\workspace\cpp_workspace\cv-demo\img\)";
    Mat color = cv::imread(path + "lena.jpg"); // 彩色读取图片
    Mat gray = cv::imread(path + "lena.jpg", IMREAD_GRAYSCALE);// 黑白读取图片

    if (!color.data) // 检测是否读取到图片
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    // 写入images
    cv::imwrite(path + "lenaGray.jpg)", gray);

    // 使用opencv获取像素
    int myRow = color.rows - 1;
    int myCol = color.cols - 1;
    auto pixel = color.at<Vec3b>(myRow, myCol);
    cout << "Pixel value (B,G,R): (" << (int) pixel[0] << "," << (int) pixel[1] << "," << (int) pixel[2] << ")" << endl;

    // 显示images
    cv::imshow("Lena BGR", color); // 彩色
    cv::imshow("Lena Gray", gray); // 黑白
    // wait for any key press
    cv::waitKey(0);
    return 0;
}

读取视频和摄像头

#include 
#include 
#include 

using namespace std;

// OpenCV includes
#include 
#include 

using namespace cv;

// OpenCV command line parser functions
// Keys accecpted by command line parser
const char *keys =
        {
                "{help h usage ? | | print this message}"
                "{@video | | Video file, if not defined try to use webcamera}"
        };

int main(int argc, const char **argv) {
    // CommandLineParser 管理命令行参数
    CommandLineParser parser(argc, argv, keys);
    parser.about("Chapter 2. v1.0.0");
    //If requires help show
    if (parser.has("help")) {
        parser.printMessage();
        return 0;
    }

    // 通过parser来读取任何输入参数
    String videoFile = parser.get<String>(0);

    // Check if params are correctly parsed in his variables
    if (!parser.check()) {
        parser.printErrors();
        return 0;
    }

    VideoCapture cap; // open the default camera
    if (!videoFile.empty())
        cap.open(videoFile);
    else
        cap.open(0);
    if (!cap.isOpened())  // check if we succeeded
        return -1;

    cv::namedWindow("Video", 1);
    for (;;) {
        Mat frame;
        // >> 操作抓取每一帧
        cap >> frame; // get a new frame from camera
        if (frame.empty()) {
            return 0;
        }
        cv::imshow("Video", frame);
        if (waitKey(30) >= 0) break;
    }
    // Release the camera or video cap
    cap.release();

    return 0;
}

int main(int, char **argv) {
    String path = R"(D:\workspace\cpp_workspace\cv-demo\data\)";
    FileStorage fs(path + "test.yml", FileStorage::WRITE); // 创建writter
    int fps = 5;
    fs << "fps" << fps; // 保存一个Int,写入使用 <<
    // Create some mat sample
    Mat m1 = Mat::eye(2, 3, CV_32F);
    Mat m2 = Mat::ones(3, 2, CV_32F);
    Mat result = (m1 + 1).mul(m1 + 3); // 两个矩阵相乘
    fs << "Result" << result;// 保存一个Mat
    fs.release();// 释放文件

    FileStorage fs2(path + "test.yml", FileStorage::READ);// 创建 reader

    Mat r;
    fs2["Result"] >> r; // 读取使用 >>
    std::cout << r << std::endl;
    fs2.release();
    return 0;
}

保存的文件内容为:

%YAML:1.0
---
fps: 5
Result: !!opencv-matrix
   rows: 2
   cols: 3
   dt: f
   data: [ 8., 3., 3., 3., 8., 3. ]

你可能感兴趣的:(opencv,计算机视觉,人工智能)