OpenCV [c++](图像处理基础示例小程序汇总)

时间:2022-02-01 01:19:49

一、图像读取与显示

#include<opencv2/opencv.hpp>
#include<iostream>
 
using namespace cv;
using namespace std;
 
int main()
{
	string path = "Resources/lambo.png";//图片的路径名
	Mat img = imread(path);//将图片加载后赋值到图像变量img中
    //if (path.empty()) { cout << "file not loaded" << endl; }
    //检查文件是否打开 没打开时执行打印语句
    //namedWindow("Image", WINDOW_FREERATIO);//创建一个名为Image的可调节的窗口
	imshow("Image", img);//创建一个窗口来显示图像img
	waitKey(0);//不断刷新图像
	return 0;
}
  • waitKey()函数的功能是不断刷新图像,频率为delay,单位是ms。
  • delay为0时,则会一直显示这一帧。
  • delay不为0时,则在显示完一帧图像后程序等待“delay"ms再显示下一帧图像。

二、图像预处理[高斯滤波、canny边缘检测、膨胀腐蚀]

#include <opencv2/opencv.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
void main() {
 
	string path = "Resources/test.png";
	Mat img = imread(path);
	Mat imgGray,imgBlur,imgCanny,imgDil,imgErode;
	//将照片转换为灰度
	cvtColor(img, imgGray, COLOR_BGR2GRAY);
	//高斯模糊
	GaussianBlur(imgGray, imgBlur, Size(3, 3), 3, 0);
	//Canny边缘检测器  一般在使用Canny边缘检测器之前会做一些模糊处理
	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("Image Gray", imgGray);
	imshow("Image Blur", imgBlur);
	imshow("Image Canny", imgCanny);
	imshow("Image Dilation", imgDil);
	imshow("Image Erode", imgErode);
 
	waitKey(0);
}

OpenCV [c++](图像处理基础示例小程序汇总)

Canny边缘检测

Canny(imgBlur, imgCanny, 25, 75);

第3和第4个参数分别代表底阈值和高阈值,其中底阈值常取高阈值的1/2或1/3

OpenCV [c++](图像处理基础示例小程序汇总)

三、图像裁剪

#include <opencv2/opencv.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
void main() {
 
	string path = "Resources/test.png";
	Mat img = imread(path);
	Mat imgResize,imgCrop;
	//调整图像大小
	//cout << img.size() << endl;//查看原图像的大小
	//resize(img, imgResize, Size(640, 480));//按自定义的宽度与高度缩放
	resize(img, imgResize, Size(),0.5,0.5);//按比例缩放
	//图像裁剪
	Rect roi(200, 100, 300, 300);
    //前面两个参数为距左上原点的x方向与y方向的距离,后两个参数为延伸的x,y长度
	imgCrop = img(roi);
 
	imshow("Image", img);
	imshow("Image Resize", imgResize);
	imshow("Image Crop", imgCrop);
	waitKey(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 派生的。

OpenCV [c++](图像处理基础示例小程序汇总)

四、绘制形状和添加文本

#include <opencv2/opencv.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, "Murtaza's Workshop", Point(137, 262), FONT_HERSHEY_DUPLEX, 0.95, Scalar(0, 69, 255), 2);
 
	imshow("Image", img);
	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 在图像中呈现指定的文本字符串。无法使用指定字体呈现的符号将替换为问号。

OpenCV [c++](图像处理基础示例小程序汇总)

五、透视投影变换矫正

#include <opencv2/opencv.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
float w = 250, h = 350;
Mat matrix, imgWarp;
// 透视变换
void main() {
 
	string path = "card.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);//获取透视变换矩阵
    //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("Image Warp", imgWarp);
 
	waitKey(0);
}

OpenCV [c++](图像处理基础示例小程序汇总)

六、颜色检测

#include <opencv2/opencv.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
Mat imgHSV,mask;
int hmin = 0, smin = 0, vmin = 0;
int hmax =179, smax = 255, vmax = 255;
 
void main() {
 
	string path = "temp.png";
	Mat img = imread(path);
	cvtColor(img, imgHSV, COLOR_BGR2HSV);
    //HSV颜色空间  H(色调):0~180  S(饱和度):0~255  V(亮度):0~255
 
	namedWindow("Trackbars", (640, 200));//创建一个名为Trackbars的窗口,大小为640*200
	createTrackbar("Hue Min", "Trackbars", &hmin, 179);
	createTrackbar("Hue Max", "Trackbars", &hmax, 179);
	createTrackbar("Sat Min", "Trackbars", &smin, 255);
	createTrackbar("Sat Max", "Trackbars", &smax, 255);
	createTrackbar("Val Min", "Trackbars", &vmin, 255);
	createTrackbar("Val Max", "Trackbars", &vmax, 255);
    //createTrackbar函数是创建轨迹条,
    //4个参数分别是 轨迹条名字,输出的窗口,一个指向整数的指针来表示当前的值,可到达的最大值
 
	while (true)
	{
		//检测我们所要的颜色 设置一个遮罩 在范围内的颜色
		Scalar lower(hmin, smin, vmin);//HSV范围最低值
		Scalar upper(hmax, smax, vmax);//HSV范围最高值
		inRange(imgHSV, lower, upper, mask);//输入,低值,高值,输出
//inRange是将在阈值区间内的像素值设置为白色(255),而不在阈值区间内的像素值设置为黑色(0)
 
		imshow("Image", img);
		imshow("Image HSV", imgHSV);
		imshow("Image Mask", mask);
 
		waitKey(1);
	}
}

OpenCV [c++](图像处理基础示例小程序汇总)

七、形状检测和轮廓检测[findContours(),approxPolyDP()]

#include <opencv2/opencv.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
Mat imgGray, imgBlur, imgCanny, imgDil, imgErode;
//定义一个轮廓处理函数
void getContours(Mat imgDil,Mat img) {
 
	vector<vector<Point>> contours;//{ {Point(20,30),Point(50,60)},{}, {}}
	vector<Vec4i>hierarchy;//vector里放置了四个int类型的变量
	findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	//drawContours(img, contours, -1, Scalar(255, 0, 255), 2);
	vector<vector<Point>>conPoly(contours.size());
	vector<Rect> boundRect(contours.size());
	
	for (int i = 0; i < contours.size(); i++)
	{
		int area = contourArea(contours[i]);
		cout << area << endl;//需要正确过滤的面积(过滤噪点)
 
		string objectType;
        //判断形状
		if (area>1000)
		{
			float peri = arcLength(contours[i], true);
			approxPolyDP(contours[i], conPoly[i], 0.02 * peri, true);//找到近似值
			
			cout << conPoly[i].size() << endl;
			boundRect[i] = boundingRect(conPoly[i]);//边界矩形
			
			int objCor = (int)conPoly[i].size();
 
			if (objCor == 3) { objectType = "Tri"; }
			if (objCor == 4) { 
				
				float aspRatio = (float)boundRect[i].width / (float)boundRect[i].height;
				cout << aspRatio << endl;
				if (aspRatio > 0.95 && aspRatio < 1.05) { objectType = "Square"; }
				else { objectType = "Rect"; 
				}
			}
			if (objCor > 4) { objectType = "Circle"; }
 
			drawContours(img, conPoly, i, Scalar(255, 0, 255), 2);//描绘计数轮廓
			rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 5);//绘制边界矩形
			//打印图形的名字
			putText(img, objectType, { boundRect[i].x,boundRect[i].y - 5 }, FONT_HERSHEY_PLAIN, 1, Scalar(0, 69, 255), 2);
		}
	}
}
 
void main() {
 
	string path = "temp.png";
	Mat img = imread(path);
 
//图像的预处理
	//1.将照片转换为灰度
	cvtColor(img, imgGray, COLOR_BGR2GRAY);
	//2.高斯模糊
	GaussianBlur(imgGray, imgBlur, Size(3, 3), 3, 0);
	//3.Canny边缘检测器
	Canny(imgBlur, imgCanny, 25, 75);
	//4.创建一个可以使用膨胀的内核
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
	//5.图像膨胀
	dilate(imgCanny, imgDil, kernel);
 
	getContours(imgDil,img);
 
	imshow("Image", img);
 
	waitKey(0);
}

八、人脸识别

#include <opencv2/opencv.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
void main() {
 
	string path = "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;//创建人脸存放的vector
	faceCascade.detectMultiScale(img, faces, 1.1, 10);
//detectMultiScale函数可以检测出图片中所有的人脸,并用vector保存各个人脸的坐标、大小
 
    //在原图像中画出人脸矩形边框
	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);
}
class cv::CascadeClassifier
用于对象检测的级联分类器类。

bool load (const String &filename)
从文件加载分类器。

bool empty() const
检查分类器是否已加载。

void detectMultiScale(InputArray image, std::vector<Rect> &objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())
检测输入图像中不同大小的对象。检测到的对象作为矩形列表返回。

OpenCV [c++](图像处理基础示例小程序汇总)

九、虚拟画笔作画

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
 
int main()
{
	VideoCapture cap(1);
	Mat img;
	Mat imgHSV, mask, imgColor;
	int hmin = 0, smin = 0, vmin = 0;
	int hmax = 179, smax = 255, vmax = 255;
 
	namedWindow("Trackbars", (640, 200)); // Create Window
	createTrackbar("Hue Min", "Trackbars", &hmin, 179);
	createTrackbar("Hue Max", "Trackbars", &hmax, 179);
	createTrackbar("Sat Min", "Trackbars", &smin, 255);
	createTrackbar("Sat Max", "Trackbars", &smax, 255);
	createTrackbar("Val Min", "Trackbars", &vmin, 255);
	createTrackbar("Val Max", "Trackbars", &vmax, 255);
 
	while (true) {
 
		cap.read(img);
		cvtColor(img, imgHSV, COLOR_BGR2HSV);
 
		Scalar lower(hmin, smin, vmin);
		Scalar upper(hmax, smax, vmax);
 
		inRange(imgHSV, lower, upper, mask);
		// hmin, smin, vmin, hmax, smax, vmax;
		cout << hmin << ", " << smin << ", " << vmin << ", " << hmax << ", " << smax << ", " << vmax << endl;
		imshow("Image", img);
		imshow("Mask", mask);
		waitKey(1);
	}
}
#include <opencv2/opencv.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
Mat img;
vector<vector<int>> newPoints;
 
vector<vector<int>> myColors{ {124, 48, 117, 143, 170, 255}, //purple
								{68, 72, 156, 102, 126, 255} }; //green
 
vector<Scalar> myColorValues{ {255, 0, 255}, //purple
								{0, 255, 0} }; //green
 
Point getContours(Mat imgDil) {
 
	vector<vector<Point>> contours; //轮廓数据
	vector<Vec4i> hierarchy;
 
	findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); //通过预处理的二值图像找到所有轮廓contours
	//drawContours(img, contours, -1, Scalar(255, 0, 255), 2); //绘制所有轮廓(不滤除噪声)
	vector<vector<Point>> conPoly(contours.size());
	vector<Rect> boundRect(contours.size());
	Point myPoint(0, 0);
 
	for (int i = 0; i < contours.size(); i++)
	{
		double area = contourArea(contours[i]); //计算每个轮廓区域
		cout << area << endl;
 
		if (area > 1000) //过滤噪声
		{
			//找轮廓的近似多边形或曲线
			double peri = arcLength(contours[i], true);
			approxPolyDP(contours[i], conPoly[i], 0.02 * peri, true);
 
			cout << conPoly[i].size() << endl;
			boundRect[i] = boundingRect(conPoly[i]); //找每个近似曲线的最小上边界矩形
			myPoint.x = boundRect[i].x + boundRect[i].width / 2;
			myPoint.y = boundRect[i].y;
 
			//drawContours(img, conPoly, i, Scalar(255, 0, 255), 2); //绘制滤除噪声后的所有轮廓
			//rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 5); //绘制边界框
		}
	}
	return myPoint; //返回矩形框上边界中点坐标
}
 
vector<vector<int>> findColor(Mat img)
{
	Mat imgHSV, mask;
	cvtColor(img, imgHSV, COLOR_BGR2HSV);
 
	for (int i = 0; i < myColors.size(); i++) 
	{
		Scalar lower(myColors[i][0], myColors[i][1], myColors[i][2]);
		Scalar upper(myColors[i][3], myColors[i][4], myColors[i][5]);
		inRange(imgHSV, lower, upper, mask);
		//imshow(to_string(i), mask);
		Point myPoint = getContours(mask); //根据mask得到检测到当前颜色矩形框的上边界中点坐标
 
		if (myPoint.x != 0 && myPoint.y != 0) 
		{
			newPoints.push_back({ myPoint.x, myPoint.y, i }); //得到当前帧检测颜色的目标点
		}
	}
	return newPoints;
}
 
void drawOnCanvas(vector<vector<int>> newPoints, vector<Scalar> myColorValues)
{
	for (int i = 0; i < newPoints.size(); i++) 
	{
		circle(img, Point(newPoints[i][0], newPoints[i][1]), 10, myColorValues[newPoints[i][2]], FILLED);
	}
}
 
int main()
{
	VideoCapture cap(0);
 
	while (true) 
	{
		cap.read(img);
		newPoints = findColor(img);
		drawOnCanvas(newPoints, myColorValues);
 
		imshow("Canvas Img", img);
		waitKey(1);
	}
 
	return 0;
}

OpenCV [c++](图像处理基础示例小程序汇总)

十、文档扫描

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
Mat imgOriginal, imgGray, imgBlur,imgCanny, imgThre, imgDil, imgErode, imgWarp, imgCrop;
vector<Point> initialPoints, docPoints;
 
float w = 420, h = 596;
 
 
Mat preProcessing(Mat img)
{
	cvtColor(img, imgGray, COLOR_BGR2GRAY); 
	GaussianBlur(imgGray, 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);
	return imgDil;
}
 
vector<Point> getContours(Mat imgDil) {
 
	vector<vector<Point>> contours; //轮廓数据
	vector<Vec4i> hierarchy;
 
	findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); //通过预处理的二值图像找到所有轮廓contours
	//drawContours(img, contours, -1, Scalar(255, 0, 255), 2); //绘制所有轮廓(不滤除噪声)
	vector<vector<Point>> conPoly(contours.size());
	vector<Point> biggest;
	int maxArea = 0;
 
	for (int i = 0; i < contours.size(); i++)
	{
		double area = contourArea(contours[i]); //计算每个轮廓区域
		cout << area << endl;
 
		if (area > 1000) //过滤噪声
		{
			//找轮廓的近似多边形或曲线
			double peri = arcLength(contours[i], true);
			approxPolyDP(contours[i], conPoly[i], 0.02 * peri, true);
 
			if (area > maxArea && conPoly[i].size() == 4) {
 
				//drawContours(imgOriginal, conPoly, i, Scalar(255, 0, 255), 5); //绘制滤除噪声后的所有轮廓
				biggest = { conPoly[i][0], conPoly[i][1], conPoly[i][2], conPoly[i][3] };
				maxArea = area;
 
			}
		}
	}
	return biggest; //返回最大轮廓四个点的坐标
}
 
void drawPoints(vector<Point> points, Scalar color)
{
	for (int i = 0; i < points.size(); i++) 
	{
		circle(imgOriginal, points[i], 10, color, FILLED);
		putText(imgOriginal, to_string(i), points[i], FONT_HERSHEY_PLAIN, 4, color, 4);
	}
}
 
vector<Point> reorder(vector<Point> points)
{
	vector<Point> newPoints;
	vector<int> sumPoints, subPoints;
 
	for (int i = 0; i < 4; i++) 
	{
		sumPoints.push_back(points[i].x + points[i].y);
		subPoints.push_back(points[i].x - points[i].y);
	}
 
	newPoints.push_back(points[min_element(sumPoints.begin(), sumPoints.end()) - sumPoints.begin()]); //0
	newPoints.push_back(points[max_element(subPoints.begin(), subPoints.end()) - subPoints.begin()]); //1
	newPoints.push_back(points[min_element(subPoints.begin(), subPoints.end()) - subPoints.begin()]); //2
	newPoints.push_back(points[max_element(sumPoints.begin(), sumPoints.end()) - sumPoints.begin()]); //3
 
	return newPoints;
}
 
Mat getWarp(Mat img, vector<Point> points, float w, float h)
{
	Point2f src[4] = { points[0], points[1], points[2], points[3] };
	Point2f dst[4] = { {0.0f, 0.0f}, {w, 0.0f}, {0.0f, h}, {w, h} };
 
	Mat matrix = getPerspectiveTransform(src, dst);
	warpPerspective(img, imgWarp, matrix, Point(w, h));
	return imgWarp;
}
 
 
int main()
{
	string path = "paper.jpg";
	imgOriginal = imread(path);
	//resize(imgOriginal, imgOriginal, Size(), 0.5, 0.5);
 
	//Preprocessing
	imgThre = preProcessing(imgOriginal);
	//Get Contours - Biggest
	initialPoints = getContours(imgThre);
	//drawPoints(initialPoints, Scalar(0, 0, 255));
	docPoints = reorder(initialPoints);
	//drawPoints(docPoints, Scalar(0, 255, 0));
 
	//Warp
	imgWarp = getWarp(imgOriginal, docPoints, w, h);
 
	//Crop
	int cropValue = 5;
	Rect roi(cropValue, cropValue, w - (2 * cropValue), h - (2 * cropValue));
	imgCrop = imgWarp(roi);
 
	imshow("Image", imgOriginal);
	imshow("Image Dilation", imgThre);
	imshow("Image Warp", imgWarp);
	imshow("Image Crop", imgCrop);
	waitKey(0);
 
	return 0;
}

OpenCV [c++](图像处理基础示例小程序汇总)

十一.车牌区域级联检测定位

#include <opencv2/opencv.hpp>
#include <iostream>
 
using namespace cv;
using namespace std;
 
int main()
{
	VideoCapture cap(0);
	Mat img;
 
	CascadeClassifier plateCascade;
	plateCascade.load("haarcascade_russian_plate_number.xml");
 
	if (plateCascade.empty()) { cout << "XML file not loaded" << endl; }
 
	vector<Rect> plates;
 
	while (true) {
 
		cap.read(img);
 
		plateCascade.detectMultiScale(img, plates, 1.1, 10);
 
		for (int i = 0; i < plates.size(); i++)
		{
			Mat imgCrop = img(plates[i]);
			imshow(to_string(i), imgCrop);
			imwrite("车牌.png", imgCrop);
			rectangle(img, plates[i].tl(), plates[i].br(), Scalar(0, 0, 255), 3);
		}
 
		imshow("Image", img);
		waitKey(1);
	}
	return 0;
}

OpenCV [c++](图像处理基础示例小程序汇总)

OpenCV [c++](图像处理基础示例小程序汇总)