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
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 33 34 35 36 37 38 39 40 41 42 43 | #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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现