站内已经有很多大神写过了,而且还是配图的,这里自己之前敲过一个笔记(没有配图的),也就发出来当作备份了


输入及输出

图片

Mat cv::imread(const String &filename, int flags = IMREAD_COLOR)

从文件加载图像,若无法读取图像将返回一个空矩阵;彩色图像通道以 BGR 顺序存储

void cv::imshow(cosnst String &winnanme, InputArray mat)

在指定窗口显示图像,函数后应伴随 cv::waitKey 函数,用于指定显示时长,否则不会显示图像。例如 waitKey(0) 将无限显示图像,直到有中断发生。waitKey(25) 将显示一帧 25 毫秒,之后显示将自动关闭。(如果你把它放在一个循环中读取视频,它会逐帧显示视频)。

int cv::waitKey(int delay = 0)

等待按下的键。函数 waitKey 无限等待按键事件(当 delay≤0 时)或延迟毫秒,当它为正时。由于操作系统在切换线程之间有最短时间,因此该函数不会完全等待延迟毫秒,它会至少等待延迟毫秒,具体取决于当时您计算机上正在运行的其他内容。如果在指定的时间过去之前没有按下任何键,则返回被按下键的代码或 -1。

示例1:输入输出
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#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;

}

视频

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

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 视频流进行视频捕获。

virtual bool cv::VideoCapture::isOpened() const

如果视频捕获已经初始化,则返回true。如果先前对 VideoCapture 构造函数或 VideoCapture::open() 的调用成功,则该方法返回 true。

virtual bool cv::VideoCapture::read(OutputArray image)

抓取、解码并返回下一个视频帧。

virtual double cv::VideoCapture::get(int proId) const

返回指定的VideoCapture属性。

virtual double cv::VideoCapture::set(int proId, double value)

在VideoCapture中设置一个属性。

示例2:捕获视频
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
	string path = "Resources/test_video.mp4";
	VideoCapture cap(path); //视频捕捉对象
	Mat img;
	while (true) {

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

}
示例3:读摄像头
#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;

}

基础函数

void cv::cvtColor(InputArray src, OutputArray dst, int code, int dstCn = 0)

将图像从一种颜色空间转换为另一种颜色空间。该函数将输入图像从一种颜色空间转换为另一种颜色空间。在从 RGB 颜色空间转换的情况下,应明确指定通道的顺序(RGB 或 BGR)。但要注意,OpenCV 中的默认颜色格式虽然称为 RGB,但实际上存储方式是 BGR(字节反转)。因此,标准(24 位)彩色图像中的第一个字节将是 8 位蓝色分量,第二个字节将是绿色,第三个字节将是红色。 然后第四、第五和第六个字节将是第二个像素(蓝色,然后是绿色,然后是红色)……依此类推。

void cv::GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, doube sigmaY = 0, int borderType = BORDER_DEFAULT)

使用高斯滤波器模糊图像。该函数将源图像与指定的高斯核进行卷积。

void cv::Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize = 3, bool L2gradient = false)

使用 Canny 算法在图像中查找边缘。

Mat cv::getStructuringElement(int shape, Size ksize, Point anchor = Point(-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())

使用特定的结构元素膨胀图像。

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())

使用特定的结构元素腐蚀图像。

示例4
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
	string path = "resources/test.png";
	Mat img = imread(path);
	Mat imgGray, imgBlur, imgCanny, imgDil, imgErode;

	cvtColor(img, imgGray, COLOR_BGR2GRAY); //灰度化
	GaussianBlur(img, imgBlur, Size(3, 3), 3, 0); //高斯模糊
	Canny(imgBlur, imgCanny, 25, 75); //边缘检测
	
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
	dilate(imgCanny, imgDil, kernel);//膨胀图像
	erode(imgDil, imgErode, kernel);//腐蚀图像
	
	imshow("Image", img);
	imshow("ImageGray", imgGray);
	imshow("ImageBlur", imgBlur);
	imshow("ImageCanny", imgCanny);
	imshow("ImageDilation", imgDil);
	imshow("ImageErode", imgErode);
	waitKey(0);
	
	return 0;

}

调整和剪裁

void cv::resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation = INTER_LINEAR)

调整图像的大小。函数 resize 将图像 src 的大小缩小到或最大到指定的大小。请注意,不考虑初始 dst 类型或大小。相反,大小和类型是从 src、dsize、fx 和 fy 派生的。

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
	string path = "resources/test.png";
	Mat img = imread(path);
	Mat imgResize, imgCrop;

	cout << img.size() << endl;
	resize(img, imgResize, Size(), 0.5, 0.5);
	
	Rect roi(200, 100, 300, 300);
	imgCrop = img(roi);
	
	imshow("Image", img);
	imshow("ImageResieze", imgResize);
	imshow("ImageCrop", imgCrop);
	waitKey(0);
	
	return 0;

}

绘制形状和文字

Mat(int rows, int cols, int type, const Scalar &s)

重载的构造函数

void cv::circle(InputOutputArray img, Point center, int radius, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)

函数 cv::circle 用于给定的中心和半径绘制一个简单的实心圆。

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)

绘制一个简单的、粗的或填充的右上矩形。函数 cv::rectangle 绘制一个矩形轮廓或两个对角为 pt1 和 pt2 的填充矩形。

void cv::line (InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)

绘制连接两点的线段。函数line绘制图像中 pt1 和 pt2 点之间的线段。

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)

绘制一个文本字符串。函数 cv::putText 在图像中呈现指定的文本字符串。无法使用指定字体呈现的符号将替换为问号。

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
	//Blank Image
	Mat img(512, 512, CV_8UC3, Scalar(255, 255, 255));

	circle(img, Point(256, 256), 155, Scalar(0, 69, 255), FILLED);
	rectangle(img, Point(130, 226), Point(382, 286), Scalar(255, 255, 255), -1);
	line(img, Point(130, 296), Point(382, 296), Scalar(255, 255, 255), 2);
	
	putText(img, "mY Workshop", Point(137, 262), FONT_HERSHEY_DUPLEX, 0.95, Scalar(0, 69, 255), 2);
	
	imshow("Image", img);
	waitKey(0);
	
	return 0;

}

透视变换

Mat cv::getPerspectiveTransform (const Point2f src[], const Point2f dst[])

返回相应 4 个点对的 3x3 透视变换。

void cv::warpPerspective (InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())

对图像应用透视变换。

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

float w = 250, h = 350;
Mat matrix, imgWarp;

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

	Point2f src[4] = { {529, 142}, {771, 190}, {405, 395}, {674, 457} };
	Point2f dst[4] = { {0.0f, 0.0f}, {w, 0.0f}, {0.0f, h}, {w, h} };
	
	matrix = getPerspectiveTransform(src, dst);
	warpPerspective(img, imgWarp, matrix, Point(w, h));
	
	for (int i = 0; i < 4; i++) {
		circle(img, src[i], 10, Scalar(0, 0, 255), FILLED);
	}
	
	imshow("Image", img);
	imshow("ImageWarp", imgWarp);
	waitKey(0);
	
	return 0;

}
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐