博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
新建空白图片
阅读量:5035 次
发布时间:2019-06-12

本文共 4538 字,大约阅读时间需要 15 分钟。

一、新建空白图片

Mat image = Mat(300,300,CV_8UC3,Scalar(0,0,255));

新建了一个图像image大小为300*300,矩阵元素为8位无符号char3通道,矩阵的内容为Scalar(0,0,255)红色

namedWindow("Test"); 新建一个Test窗口

imshow("Test",image); 在Test窗口中显示image

waitKey(0);  等待

二、画矩形,圆形等

rectangle(image,Point(50,50),Point(100,100),Scalar(255,0,0),2);

以image的(50,50)和(100,100)为对角线,线粗为2的(255,0,0)色矩形

圆circle(image,Point(100,100),50,Scalar(255,255,255),2);

三、载入、修改、保存图片

Mat image = imread("01.jpg");  //写绝对路径

rectangle(image,Point(50,50),Point(100,100),Scalar(255,0,0),2);

保存:imwrite("....",image);

四、定位框选图片

imgROI = image(Rect(40,40,40,40));

对应image图上的(40,40)为左上角,长宽为40的矩形

 五、基本图形的绘制

1 #include
2 #include
3 #include
4 using namespace cv; 5 using namespace std; 6 7 #define WINDOW_NAME1 "【绘制图1】" 8 #define WINDOW_NAME2 "【绘制图2】" 9 #define WINDOW_WIDTH 600 10 11 12 void DrawEllipse(Mat img, double angle); 13 void DrawFilledCircle(Mat img, Point center); 14 void DrawPloygon(Mat img); 15 void DrawLine(Mat img, Point start, Point end); 16 17 int main() { 18 //创建空白的Mat图像 19 Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3); 20 Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3); 21 22 //【1.1】先绘制出椭圆 23 DrawEllipse(atomImage, 90); 24 DrawEllipse(atomImage, 0); 25 DrawEllipse(atomImage, 45); 26 DrawEllipse(atomImage, -45); 27 28 //【1.2】绘制圆心 29 DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2)); 30 31 DrawPloygon(rookImage); 32 33 imshow(WINDOW_NAME1, atomImage); 34 moveWindow(WINDOW_NAME1, 0, 200); 35 36 imshow(WINDOW_NAME2, rookImage); 37 moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200); 38 39 waitKey(); 40 return 0; 41 } 42 43 void DrawEllipse(Mat img, double angle) { //椭圆 44 int thickness = 2; 45 int lineType = 8; 46 47 ellipse(img, 48 Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2), 49 Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16), 50 angle, 51 0, 52 360, 53 Scalar(255, 129, 0), 54 thickness, 55 lineType 56 ); 57 } 58 59 void DrawFilledCircle(Mat img, Point center) { //实心圆 60 int thickness = -1; 61 int lineType = 8; 62 63 circle(img, 64 center, 65 WINDOW_WIDTH / 32, 66 Scalar(0, 0, 255), 67 thickness, 68 lineType); 69 } 70 71 void DrawPloygon(Mat img) { //凹多边形 72 int lineType = 8; 73 74 //创建一些点 75 Point rookPoint[1][20]; 76 rookPoint[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8); 77 rookPoint[0][1] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8); 78 rookPoint[0][2] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16); 79 rookPoint[0][3] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16); 80 81 rookPoint[0][4] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8); 82 rookPoint[0][5] = Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8); 83 rookPoint[0][6] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8); 84 rookPoint[0][7] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8); 85 86 rookPoint[0][8] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4); 87 rookPoint[0][9] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4); 88 rookPoint[0][10] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8); 89 rookPoint[0][11] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8); 90 91 rookPoint[0][12] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4); 92 rookPoint[0][13] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4); 93 rookPoint[0][14] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8); 94 95 rookPoint[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8); 96 rookPoint[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8); 97 98 rookPoint[0][17] = Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8); 99 rookPoint[0][18] = Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);100 rookPoint[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);101 102 const Point* ppt[1] = { rookPoint[0] };103 int npt[] = { 20 };104 105 fillPoly(img,106 ppt,107 npt,108 1,109 Scalar(255, 255, 255),110 lineType);111 }112 113 void DrawLine(Mat img, Point start, Point end) { //线的绘制114 int thickness = 2;115 int lineType = 8;116 line(img,117 start,118 end,119 Scalar(0, 0, 0),120 thickness,121 lineType);122 }

 

转载于:https://www.cnblogs.com/Nelsoner/p/6713324.html

你可能感兴趣的文章
C和指针 第十章 结构和联合 习题
查看>>
对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC。
查看>>
双向循环链表
查看>>
Iterator和for...of
查看>>
jupyter notebook 安装代码提示功能
查看>>
状压DP概念 及例题(洛谷 P1896 互不侵犯)
查看>>
java 复习
查看>>
【心情】此时此刻。
查看>>
【题解】 CF242E XOR on Segment
查看>>
maven使用内嵌tomcat7
查看>>
2017.10.9 java笔记
查看>>
gcolor2-拾色器
查看>>
【转】Appium如何定位安卓APP元素
查看>>
WPF用流的方式上传/显示/下载图片文件(保存在数据库)
查看>>
wenbao与右键菜单
查看>>
mysql 常用基准测试工具
查看>>
Beta冲刺第一天
查看>>
用eval 动态编译代码
查看>>
Learn Objective‐C
查看>>
设计模式之单例
查看>>