一、图像读取与显示
#include
#include
using namespace cv;
using namespace std;
int main()
{
string path = "Resources/lambo.png";//图片的路径名
Mat img = imread(path);//将图片加载后赋值到图像变量img中
//if (path.empty()) { cout
- waitKey()函数的功能是不断刷新图像,频率为delay,单位是ms。
- delay为0时,则会一直显示这一帧。
- delay不为0时,则在显示完一帧图像后程序等待“delay”ms再显示下一帧图像。
二、图像预处理[高斯滤波、canny边缘检测、膨胀腐蚀]
#include
#include
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);
}
Canny边缘检测
Canny(imgBlur, imgCanny, 25, 75);
第3和第4个参数分别代表底阈值和高阈值,其中底阈值常取高阈值的1/2或1/3
三、图像裁剪
#include
#include
using namespace cv;
using namespace std;
void main() {
string path = "Resources/test.png";
Mat img = imread(path);
Mat imgResize,imgCrop;
//调整图像大小
//cout
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
#include
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 在图像中呈现指定的文本字符串。无法使用指定字体呈现的符号将替换为问号。
五、透视投影变换矫正
#include
#include
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
六、颜色检测
#include
#include
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);
}
}
七、形状检测和轮廓检测[findContours(),approxPolyDP()]
#include
#include
using namespace cv;
using namespace std;
Mat imgGray, imgBlur, imgCanny, imgDil, imgErode;
//定义一个轮廓处理函数
void getContours(Mat imgDil,Mat img) {
vector> contours;//{ {Point(20,30),Point(50,60)},{}, {}}
vectorhierarchy;//vector里放置了四个int类型的变量
findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
//drawContours(img, contours, -1, Scalar(255, 0, 255), 2);
vector>conPoly(contours.size());
vector boundRect(contours.size());
for (int i = 0; i 1000)
{
float peri = arcLength(contours[i], true);
approxPolyDP(contours[i], conPoly[i], 0.02 * peri, true);//找到近似值
cout 0.95 && aspRatio 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
#include
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()){coutfaces;//创建人脸存放的vector
faceCascade.detectMultiScale(img, faces, 1.1, 10);
//detectMultiScale函数可以检测出图片中所有的人脸,并用vector保存各个人脸的坐标、大小
//在原图像中画出人脸矩形边框
for (int i = 0; i
class cv::CascadeClassifier
用于对象检测的级联分类器类。
bool load (const String &filename)
从文件加载分类器。
bool empty() const
检查分类器是否已加载。
void detectMultiScale(InputArray image, std::vector &objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())
检测输入图像中不同大小的对象。检测到的对象作为矩形列表返回。
九、虚拟画笔作画
#include
#include
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
#include
#include
using namespace cv;
using namespace std;
Mat img;
vector> newPoints;
vector> myColors{ {124, 48, 117, 143, 170, 255}, //purple
{68, 72, 156, 102, 126, 255} }; //green
vector myColorValues{ {255, 0, 255}, //purple
{0, 255, 0} }; //green
Point getContours(Mat imgDil) {
vector> contours; //轮廓数据
vector hierarchy;
findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); //通过预处理的二值图像找到所有轮廓contours
//drawContours(img, contours, -1, Scalar(255, 0, 255), 2); //绘制所有轮廓(不滤除噪声)
vector> conPoly(contours.size());
vector boundRect(contours.size());
Point myPoint(0, 0);
for (int i = 0; i 1000) //过滤噪声
{
//找轮廓的近似多边形或曲线
double peri = arcLength(contours[i], true);
approxPolyDP(contours[i], conPoly[i], 0.02 * peri, true);
cout > findColor(Mat img)
{
Mat imgHSV, mask;
cvtColor(img, imgHSV, COLOR_BGR2HSV);
for (int i = 0; i > newPoints, vector myColorValues)
{
for (int i = 0; i
十、文档扫描
#include
#include
#include
#include
using namespace cv;
using namespace std;
Mat imgOriginal, imgGray, imgBlur,imgCanny, imgThre, imgDil, imgErode, imgWarp, imgCrop;
vector 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 getContours(Mat imgDil) {
vector> contours; //轮廓数据
vector hierarchy;
findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); //通过预处理的二值图像找到所有轮廓contours
//drawContours(img, contours, -1, Scalar(255, 0, 255), 2); //绘制所有轮廓(不滤除噪声)
vector> conPoly(contours.size());
vector biggest;
int maxArea = 0;
for (int i = 0; i 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 points, Scalar color)
{
for (int i = 0; i reorder(vector points)
{
vector newPoints;
vector sumPoints, subPoints;
for (int i = 0; i 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;
}
十一.车牌区域级联检测定位
#include
#include
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 plates;
while (true) {
cap.read(img);
plateCascade.detectMultiScale(img, plates, 1.1, 10);
for (int i = 0; i
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net