OpenCV绘制线、圆、椭圆、矩形
一、概述
案例:使用opencv在一张图片上绘制线、圆、椭圆、矩形
二、示例图片
三、示例代码
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main( int argc, char const *argv[]) { /* 绘制线、圆、椭圆、矩形 */ Mat src = imread( "girl.jpg" ); if (!src.data){ cout << "can't not found image" <<endl; return -1; } //绘制线 Point p1 = Point(20,30); Point p2; p2.x = 400; p2.y = 400; Scalar color(0,0,255); line(src,p1,p2,color,LINE_8); //绘制矩形 Rect rect = Rect(200,100,300,300); Scalar color2 = Scalar(255,0,0); rectangle(src,rect,color,2,LINE_8); //绘制椭圆 Scalar color3 = Scalar(0, 255, 0); ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 90, 0, 360, color3, 2, LINE_8); //绘制圆 Scalar color4 = Scalar(0, 255, 255); Point center = Point(src.cols / 2, src.rows / 2); circle(src, center, 150, color4, 2, 8); //填充一个矩形 Point pts[1][5]; pts[0][0] = Point(100, 100); pts[0][1] = Point(100, 200); pts[0][2] = Point(200, 200); pts[0][3] = Point(200, 100); pts[0][4] = Point(100, 100); const Point* ppts[] = { pts[0] }; int npt[] = { 5 }; Scalar color5 = Scalar(255, 12, 255); fillPoly(src, ppts, npt, 1, color5, 8); //展示图片 imshow( "src" ,src); waitKey(0); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探