OpenCV 学习笔记 Chapter 2 Part I

OpenCV 学习笔记 Chapter 2 Part I

教材:冯振、郭延宁、吕跃勇·《OpenCV4快速入门》

Compile with cmake

In case to compile mat.cpp, write CMakeLists.txt :

cmake_minimum_required(VERSION 2.8)
project( mat )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( mat mat.cpp )
target_link_libraries( mat ${OpenCV_LIBS} )

Then generate Makefile and make:

cmake .
make

Ways to initialize Mat object

  • Initialize a header (pointer) to Mat object:
#include <opencv2/core.hpp>
#include <iostream>

using namespace cv;

int main()
{
    Mat a;
    std::cout << a;
    return 0;
}

output:

[]
  • Initialize with type and size (row, col) (1):
#include <opencv2/core.hpp>
#include <iostream>

using namespace cv;

int main()
{
    Mat a = Mat_<int>(3, 4);
    std::cout << a;
    return 0;
}

output:

[0, 0, 0, 0;                                                                                                                                                                         
 0, 0, 0, 0;                                                                                                                                                                         
 0, 0, 0, 0]
  • Initialize with type and size (2):
#include <opencv2/core.hpp>
#include <iostream>

using namespace cv;

int main()
{
    Mat a(8, 6, CV_8UC3);
    std::cout << a;
    return 0;
}

output:
在这里插入图片描述

  • Initialize with Size struct
#include <opencv2/core.hpp>
#include <iostream>
#include <opencv2/core/hal/interface.h>

using namespace cv;

int main()
{
    Size size(3, 4);
    Mat a(size, CV_8UC3);
    std::cout << a;
    return 0;
}

output:
在这里插入图片描述

  • Initialize with specific value / mode
#include <opencv2/core.hpp>
#include <iostream>
#include <opencv2/core/hal/interface.h>

using namespace cv;

int main()
{
    Mat a = Mat::ones(3, 2, CV_8U);
    std::cout << a;
    return 0;
}

output:
在这里插入图片描述

Similar to numpy, there are

Mat::ones
Mat::zeros
...
  • Initialize with values from array
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/core/hal/interface.h>

using namespace cv;

int main() {
    float arr[8];
    for (int i = 0; i < 8; i++) {
        arr[i] = float(i) / 10;
    }

    Mat a(2, 3, CV_32F, arr);
    Mat b(3, 4, CV_32F, arr);
    std::cout << a << std::endl << b;
    return 0;
}

output :
在这里插入图片描述

Shallow copy of Mat object

#include <opencv2/core.hpp>
#include <iostream>
#include <opencv2/core/hal/interface.h>

using namespace cv;

int main()
{
    Size size(3, 4);
    Mat a(size, CV_8UC1);
    Mat b(a);
    a.at<unsigned int>(0, 0) = 1;
    std::cout << a << std::endl;
    std::cout << b;
    return 0;
}

output:
在这里插入图片描述

Deep copy of Mat object

#include <opencv2/core.hpp>
#include <iostream>
#include <opencv2/core/hal/interface.h>

using namespace cv;

int main()
{
    Size size(3, 4);
    Mat a(size, CV_8UC1);
    Mat b = a.clone();
    a.at<unsigned int>(0, 0) = 1;
    std::cout << a << std::endl;
    std::cout << b;
    return 0;
}

output:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值