第0章 前言

参考书籍:学习OpenCV3 中文版(Learning OpenCV 3:computer vision with the OpenCV Library)

OpenCV适用于图像处理、模式识别、三维重建、物体跟踪、机器学习和线性代数

先修课程:C++编程、线性代数、概率论

使用方法:本书提供了三种阅读方法,由于工具书的定位,作者建议,第一,可以“抓取重点”,先阅读1~5章,之后只阅读需要的章节;第二,可以“精读”,坚持每周阅读两章;第三,“速成”,快速阅读。都提到了以项目为导向。

目录

第1章 概述(略)

第2章 OpenCV初探

第3章 了解OpenCV的数据类型

第4章 图像和大型数组类型

第5章 矩阵操作

第6章 绘图和注释

第7章 OpenCV中的函数子

第8章 图像、视频与数据文件

第9章 跨平台和Windows系统

第10章 滤波与卷积

第11章 常见的图像变换

第12章 图像分析

第13章 直方图和模板

第14章 轮廓

第15章 背景提取

第16章 关键点和描述子

第17章 跟踪

第18章 相机模型与标定

第19章 投影与三维视觉

第20章 机器学习基础(略)

第21章 StatModel:OpenCV中的基准学习模型(略)

第22章 目标检测(略)

第23章 OpenCV的未来

英文书籍与国内书籍有很大的不同。


第3章  了解OpenCV的数据类型

Point类

point类顾名思义,就是图像中的点,point有x,y或者x,y,z,也就是坐标。由此出发可以做一些向量运算。

//包含OpenCV的头文件
//参照github https://github.com/yoyoyo-yo/Gasyori100knock 
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace  std;
//使用OpenCV的命名空间
using namespace cv;
//Point 类型操作 类型
int main()
{
	//1.默认构造函数
	Point2i p1;//数据为[0,0]
	Point3f p2;//数据为[0,0,0]
	cout << p1 << '\n' << p2 << endl;

	//2.赋值构造函数
	Point3f p3(1, 2, 3);//数据为[1,2,3]
	Point3f p4(p3);//数据为[1,2,3]
	cout << p3 << "\n" << p4 << endl;

	//3,带参数的构造函数
	Point2i p5(1, 2);//数据为[1,2]
	Point3f p6(1.1, 1.2, 1.3);//数据为[1.1,1.2,1.3]
	cout << p5 << '\n' << p6 << endl;

	//4,隐式类型转换,转换为Vec3f
	Vec3f v = p6;//数据为[1.1,1.2,1.3]
	cout << v << endl;

	//5,成员函数访问
	cout << p5.x << '\t' << p5.y << endl;
	cout << p6.x << '\t' << p6.y << '\t' << p6.z << endl;

	//6.点乘 --可以用来计算两个向量之间的夹角
	Point3f p7(2.0f, 3.0f, 4.0f);
	float value = p6.dot(p7);
	cout << value << endl;

	//7.十字相乘 仅仅适用与三维点影像
	//结果返回两个向量的垂直向量
	Point3f p8(1, 0, 0);//x方向的单位向量
	Point3f p9(0, 1, 0);//Y方向的单位向量
	Point3f p10 = p8.cross(p9);//计算出来的Z方向的单位向量
	cout << p10 << endl;

	//8.判断点是否在矩阵内 仅仅适用与二维点
	Rect2f r(0, 0, 1, 1);//注意,这个构造函数是(x,y,width,height)X坐标系向右为正,Y坐标系向上为正
	Point2f p(0.5, 0.5);
	bool bValue = p.inside(r);//返回为true
	p.x = 2;
	bValue = p.inside(r);//返回为false
	return 0;
}

Scalar类

Scalar中文翻译为标量,可以认为是长度为4的数组,没有提供的值默认为0。

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

using namespace cv;

int main() {
    cv::Scalar scalar(125);
    cv::Mat mat(2, 3, CV_8UC1, scalar);
    std::cout << mat << std::endl;
    std::cout << std::endl;

    cv::Scalar scalar1(0, 255);
    cv::Mat mat1(4, 4, CV_32FC2, scalar1);
    std::cout << mat1 << std::endl;
    std::cout << std::endl;

    cv::Scalar scalar2(0, 255, 255);
    cv::Mat mat2(400, 400, CV_32FC3, scalar2);
    //std::cout << mat2 << std::endl;
    imshow("scalar2", mat2);
    waitKey(0);
    std::cout << std::endl;

    cv::Scalar scalar3(0, 255, 255, 0);
    cv::Mat mat3(4, 4, CV_32FC4, scalar3);
    std::cout << mat3 << std::endl;
    waitKey(0);
    
    Scalar s1(1, 2, 3, 4);
    Scalar s2(4, 3, 2, 1);
    cout << s1.mul(s2) << endl;
    //[4,6,6,4]
    return 0;

}

其可以是一维、二维、三维也可以是四维数组。

Size类

Point类的数据成员有x、y,size类的数据成员为height、width。

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

using namespace cv;
using namespace std;

int main() {
    Size s1(10, 20);
    cout << "长:" << s1.height << endl;
    cout << "宽:" << s1.width << endl;
    cout << "面积:" << s1.area() << endl;
    return 0;

}

Rect类

又名矩形类,数据成员有,x、y、height、width,从(x,y)出发,长height,宽width。

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

using namespace cv;
using namespace std;

int main() {
    Mat image = imread("Lena.png");
    Rect rect1(256, 256, 128, 128);
    Rect rect2(224, 224, 128, 128);

    Mat roi1, roi2;
    image(rect1).copyTo(roi1);
    imshow("1", roi1);
    waitKey(0);
    imwrite("1.png", roi1);

    image(rect2).copyTo(roi2);
    imshow("2", roi2);
    waitKey(0);
    imwrite("2.png", roi2);

    imshow("0", image);
    waitKey(0);
    return 0;

}

注意:原点在左上角!!!

RotateRect类

包含一个中心点Point2f、一个大小Size2f、一个额外的角度float

Matx固定矩阵类

Vec固定向量类

Complex复数类

辅助对象

TermCriteria类

Range类

InputArray类和Output类

工具函数

模板函数

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐