2022.3.14
chapter1
#include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace cv; using namespace std; /*导入图像视频和网络摄像头*/ /////////////////////////////////////// video ///////////////////////////////////// //遍历每一帧图像捕获并显示 //int main() //{ // string path = "Resources/test_video.mp4"; // VideoCapture cap(path); // Mat img = imread(path); // while(true) // { // cap.read(img); // imshow("image", img); // waitKey(60); // } // return 0; //} /////////////////////////////////////// webcam ///////////////////////////////////// int main() { //写一个相机的id号 VideoCapture cap(0); Mat img; while (true) { cap.read(img); imshow("image", img); waitKey(1); } return 0; }
chapter2
#include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace cv; using namespace std; /////////////////// basic function ////////////////////// int main() { string path = "Resources/test.png"; Mat img = imread(path); //转变灰度 Mat imgGray, imgBlur, imgCanny,imgDil,imgErode; cvtColor(img,imgGray, COLOR_BGR2GRAY); //模糊(高斯模糊)(原图,目标图,高斯核大小,x方向标准偏差,y方向标准偏差) GaussianBlur(img, imgBlur,Size(3,3),3,0); //边缘检测(Canny边缘检测器) Canny(imgBlur, imgCanny, 25, 150); //创建轨迹栏,观察实际需要多少边缘 //扩大和侵蚀图像(为了更好的边缘检测) 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); return 0; }
chapter3
#include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace cv; using namespace std; /////////////////// Resize and Crop ////////////////////// int main() { string path = "Resources/test.png"; Mat img = imread(path); Mat imgResize, imgResize1,imageCrop; //cout << img.size() << endl;//768*559 //调整图片大小 resize(img, imgResize, Size(640,480));//精确调整 resize(img, imgResize1, Size(),0.5,0.5);//按比例缩放 //裁剪 Rect roi(100, 100,300,250);//定义矩形(起始点x,y,宽高x,y) imageCrop = img(roi); imshow("image", img); imshow("image Resize", imgResize); imshow("image Resize1", imgResize1); imshow("image Crop", imageCrop); waitKey(0); return 0; }
chapter4
#include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace cv; using namespace std; /////////////////// Draw Shapes and Text ////////////////////// int main() { //创建空白图像 Mat img(512, 512,CV_8UC3,Scalar(255, 255, 255)); //绘制圆形(原图,圆心,半径,颜色,厚度(可省略)) circle(img, Point(256, 256), 155, Scalar(0, 69, 255),FILLED);//填充 //circle(img, Point(256, 256), 50, Scalar(0, 69, 255), 10);//厚度 //绘制矩形(对角点坐标) rectangle(img, Point(130, 226), Point(382, 286), Scalar(255, 255, 255),FILLED); //或者用chapter3中的先定义矩形再绘制,用起始点和长宽 //Rect roi(100, 100, 300, 250); //rectangle(img, roi, Scalar(255, 255, 222),5); //画线 line(img, Point(130, 296), Point(382,296), Scalar(255,255,255),2); //文本(图片,文字内容,起始坐标,字体,比例,颜色,厚度) putText(img, "As balance dictates", Point(137, 262), FONT_HERSHEY_PLAIN,1.48,Scalar(0,69,255),2); imshow("image", img); waitKey(0); return 0; }
来自b站及其评论区:https://www.bilibili.com/video/BV11A411T7rL?spm_id_from=333.1007.top_right_bar_window_history.content.click