Samxander's home

You shall see the difference now that we are back again!

0%

OpenCV简单函数速查(C++)

OpenCV 简单函数速查

读取图片/视频/摄像头

从文件读取图片

1
Mat cv::imread(const String &filename, int flags = IMREAD_COLOR)
  • 上述代码实现了从文件加载图像。函数 imread 从指定文件加载图像并返回。在彩色图像的情况下,解码图像的通道将以 B G R 顺序存储。
1
void cv::imshow(const String &winname, InputArray mat)
  • 上述代码实现了在指定窗口中显示图像。其后应跟 cv::waitKey 函数。
1
int cv::waitKey(int delay = 0)
  • 上述代码实现了等待按下的键waitKey(0) 将无限显示窗口,直到有任何按键。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <opencv2/imgcodecs.hpp> // 读写图像文件
#include <opencv2/highgui.hpp> // 图像处理
#include <opencv2/imgproc.hpp> // 高级GUI
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
string path = "Resources/test.png";
Mat img = imread(path);
imshow("Image", img);
waitKey(0); //显示图片不会一闪而过

return 0;
}

最终,会显示一个名为 Image 的窗口。(懒得放图了)

从文件读取视频

要捕获视频,需要创建一个 VideoCapture 对象。它的参数可以是视频文件的名称或设备索引。

1
2
3
4
5
cv::VideoCapture::VideoCapture()
cv::VideoCapture::VideoCapture(const String &filename)
cv::VideoCapture::VideoCapture(const String &filename, int apiPreference)
cv::VideoCapture::VideoCapture(int index)
cv::VideoCapture::VideoCapture(int index, int apiPreference)
  • 上述代码实现打开视频文件或捕获设备或IP视频流进行视频捕获
1
virtual bool cv::VideoCapture::isOpened() const
  • 如果视频捕获已经初始化,则返回 true
1
virtual bool cv::VideoCapture::read(OutputArray image)
  • 抓取、解码并返回下一个视频帧。
1
virtual double cv::VideoCapture::get(int proID) const;
  • 返回指定的 VideoCapture 属性。<-
1
virtual double cv::VideoCapture::set(int proID, double value)
  • VideoCapture 中设置一个属性。

读摄像头代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
VideoCapture cap(0);
Mat img;

while (true)
{
cap.read(img);
imshow("Image", img);
waitKey(1);
}
return 0;
}

基础函数

1
void cv::cvtColor(InputArray src, OutputArray dst, int code, int dstCn = 0)
  • 上述代码实现了将一种颜色空间转换为另一种颜色空间
1
void cv::GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY = 0, int borderType = BORDER_DEFAULT)
  • 高斯模糊
1
void cv::Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize = 3, bool L2gradient = false)
  • 使用Canny算法在图像中查找边缘
1
Mat cv::getStructuringElement(int shape, Size ksize, Point anchor = Point(-1, -1))
  • 返回指定大小和形状的结构元素,用于形态学操作。
1
void cv::dilate(InputArray src, OutputArray dst, InuputArray kernel, Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue())
  • 使用特定的结构元素膨胀图像。
1
void cv::erode(InputArray src, OutputArray dst, InuputArray kernel, Point anchor = Point(-1, -1), int iterations = 1, int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue())
  • 使用特定的结构元素腐蚀图像。

调整和剪裁

1
void cv::resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation = INTER_LINEAR)
  • 调整图像大小。函数 resize 将图像 src 的大小缩小到或最大到指定的大小。

绘制形状和文字

1
Mat(int rows, int cols, int type, const Scalar &s)
  • 重载的构造函数
1
void cv::circle(InputOutputArray img, Point center, int radius, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
  • 函数 cv::circle 用给定的中心和半径绘制一个简单的实心圆
1
2
3
void cv::rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)

void cv::rectangle(Mat &img, Rect rec, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
  • 绘制一个简单的、粗的或填充的右上矩形
1
void cv::line (InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
  • 绘制连接两点的线段
1
void cv::putText (InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
  • 绘制一个文本字符串

透视变换

1
Mat cv::getPerspectiveTransform (const Point2f src[], const Point2f dst[])
  • 返回相应 4 个点对的 3x3 透视变换。
1
void cv::warpPerspective (InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())
  • 对图像应用透视变换。

颜色检测

1
void cv::inRange (InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)
  • 检测数组元素时候位于其他两个数组的元素之间。
1
void cv::namedWindow (const String &winname, int flags = WINDOW_AUTOSIZE)
  • 创建一个窗口。函数创建一个可用作图像和轨迹栏占位符的窗口。创建的窗口由它们的名称引用。
1
int cv::createTrackbar (const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange = 0, void *userdata = 0)
  • 创建一个 trackbar 并将其附加到指定窗口。函数 createTrackbar创建一个具有指定名称和范围的trackbar(滑块或范围控件),分配一个变量值作为与 trackbar 同步的位置,并指定回调函数onChange 为 在跟踪栏位置变化时被调用。创建的轨迹栏显示在指定的窗口 winname 中。

形状/轮廓检测

1
void cv::findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset = Point())
  • 在二值图像中查找轮廓
参数 含义
image 二值输入图像
contours 检测到的轮廓,每个轮廓都存储为点向量(例如 std::vector<std::vector<cv::Point> >
hierarchy 可选的输出向量(例如 std::vector<cv::Vec4i>),包含有关图像拓扑的信息
mode 轮廓检索模式
method 轮廓近似方式
offset 每个轮廓点移动的可选偏移量
1
double cv::contourArea(InputArray contour, bool oriented = false)
  • 计算轮廓区域
1
double cv::arcLength(InputArray curve, bool closed)
  • 计算曲线长度或闭合轮廓周长
1
void cv::approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
  • 用另一个具有较少顶点的曲线/多边形来逼近一条曲线或多边形,以使他们之间的距离小于或等于指定的精度。
1
void cv::drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness = 1, int lineType = LINE_8, InputArray hierarchy = noArray(), int maxLevel = INT_MAX, Point offset = Point())
  • 绘制轮廓轮廓或填充轮廓。如果厚度 ≥ 0,该函数在图像中绘制轮廓轮廓,如果厚度 < 0,则填充轮廓所包围的区域。
1
Point_< _Tp > tl() const
  • 左上角
1
Point_< _Tp > br() const
  • 右下角

人脸检测

(挖个坑,这里仅给出速查代码,后面写一篇从原理开始分析的人脸检测)

涉及模块 objdetect:Object Detection

1
class cv::CascadeClassifier
  • 用于对象检测的级联分类器类
1
bool load (const String &filename)
  • 从文件加载分类器
1
bool empty() const
  • 检测分类器是否已加载。
1
void detectMultiScale(InputArray image, std::vector<Rect> &objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())
  • 检测输入图像中不同大小的对象。检测到的对象作为矩形列表返回。

代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
string path = "Resources/test.png";
Mat img = imread(path);

CascadeClassifier faceCascade;
faceCascade.load("Resources/haarcascade_frontalface_default.xml");

if (faceCascade.empty()) { cout << "XML file not loaded" << endl; }

vector<Rect> faces;
faceCascade.detectMultiScale(img, faces, 1.1, 10);

for (int i = 0; i < faces.size(); i++)
{
rectangle(img, faces[i].tl(), faces[i].br(), Scalar(255, 0, 255), 3);
}

imshow("Image", img);
waitKey(0);

return 0;
}
Insist on writing original high-quality articles. Your support is my biggest motivation.