OpenCV中可以绘制的图形有直线、矩形、多边形、圆、椭圆。以及一个写文本的函数puttext
1. 基本函数
Line
C++: void line(Mat& img, Point pt1,Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)Parameters:
img – 图像.
pt1 – 线条起点.
pt2 – 线条终点.
color – 线条颜色.
thickness – 线条宽度.
lineType – 线型
Type of the line: 8 (or omitted) - 8-连接线.
4 - 4-连接线.
CV_AA - 反走样线条.
shift – 坐标点小数点位数.
Rectangle
C++: void rectangle(Mat& img,Point pt1, Pointpt2, const Scalar&color, intthickness=1,intlineType=8, intshift=0)C++: void rectangle(Mat& img,Rect rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )
Parameters:
img – 画矩形的对象
pt1 – 矩形的一个顶点,左上角的.
pt2 – 另一个顶点,右下角的.
rec – 确定矩形的另一种方式,给左上角坐标和长宽
color – 指定矩形的颜色或亮度(灰度图像),scalar(255,0,255)既可指定.
thickness – 矩形边框的粗细. 负值(like CV_FILLED)表示要画一个填充的矩形
lineType – 边框线型. ( 8 (or 0) - 8-connected line(8邻接)连接线。
4 - 4-connected line(4邻接)连接线。
CV_AA - antialiased 线条。)
shift –坐标点的小数点位数
PolyLine
C++: void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )C++: void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
Parameters:
img – 折线所在图像.
pts – 折线中拐点坐标指针.
npts – 折线拐点个数指针.
ncontours – 折线线段数量.
isClosed – 折线是否闭合.
color – 折线颜色.
thickness – 折线宽度.
lineType – 线型.
shift – 顶点坐标小数点位数.
Circle
C++: void circle(Mat&img, Point center, intradius, const Scalar&color,intthickness=1, intlineType=8, intshift=0)Parameters:
img – 要画圆的那个矩形.
center – 圆心坐标.
radius – 半径.
color – 圆边框颜色,scalar类型的
thickness – 正值表示圆边框宽度. 负值表示画一个填充圆形
lineType – 圆边框线型
shift – 圆心坐标和半径的小数点位数
Ellipse
C++: void ellipse(Mat& img, Point center,Size axes, double angle, double startAngle, double endAngle, const Scalar& color,int thickness=1, int lineType=8, int shift=0)C++: void ellipse(Mat& img, constRotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)
Parameters:
img – 椭圆所在图像.
center – 椭圆中心.
axes – 椭圆主轴一半的长度
angle – 椭圆旋转角度
startAngle – 椭圆弧起始角度
endAngle –椭圆弧终止角度
box – 指定椭圆中心和旋转角度的信息,通过 RotatedRect 或 CvBox2D. 这表示椭圆画在旋转矩形上(矩形是不可见的,只是指定了一个框而已)
color – 椭圆边框颜色.
thickness – 正值代表椭圆边框宽度,负值代表填充的椭圆
lineType – 线型
shift – 椭圆中心坐标和坐标轴的小数点位数
PutText
C++: void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )Parameters:
img – 显示文字所在图像.
text – 待显示的文字.
org – 文字在图像中的左下角 坐标.
font – 字体结构体.
fontFace – 字体类型, 可选择字体:
FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX,FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN,
FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL,
FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX,以上所有类型都可以配合 FONT_HERSHEY_ITALIC使用,产生斜体效果。
fontScale – 字体大小,该值和字体内置大小相乘得到字体大小
color – 文本颜色
thickness – 写字的线的粗细
lineType – 线型.
bottomLeftOrigin – true, 图像数据原点在左下角. Otherwise, 图像数据原点在左上角.
2. 一些参数取值情况
lineType: 8或0 8-连接线
4 4-连接线
CV_AA 反走样线条
thickness: >0,线条粗细 <0,填充
fontFace :
CV_FONT_HERSHEY_SIMPLEX 正常尺寸sanserif字体
CV_FONT_HERSHEY_PLAIN 小尺寸sanserif字体
CV_FONT_HERSHEY_DUPLEX 正常尺寸sanserif, 比CV_FONT_HERSHEY_SIMPLEX更复杂
CV_FONT_HERSHEY_COMPLEX 正常尺寸serif, 比CV_FONT_HERSHEY_DUPLEX更复杂
CV_FONT_HERSHEY_TRIPLEX 正常尺寸serif, 比CV_FONT_HERSHEY_COMPLEX更复杂
CV_FONT_HERSHEY_COMPLEX_SMALL 小尺寸的CV_FONT_HERSHEY_COMPLEX
CV_FONT_HERSHEY_SCRIPT_SIMPLEX 手写风格
CV_FONT_HERSHEY_SCRIPT_COMPLEX 比CV_FONT_HERSHEY_SCRIPT_SIMPLEX更复杂的风格
常见颜色RGB值对照表
| R | G | B | 值 |
| R | G | B | 值 |
| R | G | B | 值 |
黑色 | 0 | 0 | 0 | #000000 | 黄色 | 255 | 255 | 0 | #FFFF00 | 浅灰蓝色 | 176 | 224 | 230 | #B0E0E6 |
象牙黑 | 41 | 36 | 33 | #292421 | 香蕉色 | 227 | 207 | 87 | #E3CF57 | 品蓝 | 65 | 105 | 225 | #4169E1 |
灰色 | 192 | 192 | 192 | #C0C0C0 | 镉黄 | 255 | 153 | 18 | #FF9912 | 石板蓝 | 106 | 90 | 205 | #6A5ACD |
冷灰 | 128 | 138 | 135 | #808A87 | dougello | 235 | 142 | 85 | #EB8E55 | 天蓝 | 135 | 206 | 235 | #87CEEB |
石板灰 | 112 | 128 | 105 | #708069 | forum gold | 255 | 227 | 132 | #FFE384 |
|
|
|
|
|
暖灰色 | 128 | 128 | 105 | #808069 | 金黄色 | 255 | 215 | 0 | #FFD700 | 青色 | 0 | 255 | 255 | #00FFFF |
|
|
|
|
| 黄花色 | 218 | 165 | 105 | #DAA569 | 绿土 | 56 | 94 | 15 | #385E0F |
白色 | 225 | 225 | 225 | #FFFFFF | 瓜色 | 227 | 168 | 105 | #E3A869 | 靛青 | 8 | 46 | 84 | #082E54 |
古董白 | 250 | 235 | 215 | #FAEBD7 | 橙色 | 255 | 97 | 0 | #FF6100 | 碧绿色 | 127 | 255 | 212 | #7FFFD4 |
天蓝色 | 240 | 255 | 255 | #F0FFFF | 镉橙 | 255 | 97 | 3 | #FF6103 | 青绿色 | 64 | 224 | 208 | #40E0D0 |
白烟 | 245 | 245 | 245 | #F5F5F5 | 胡萝卜色 | 237 | 145 | 33 | #ED9121 | 绿色 | 0 | 255 | 0 | #00FF00 |
白杏仁 | 255 | 235 | 205 | #FFFFCD | 桔黄 | 255 | 128 | 0 | #FF8000 | 黄绿色 | 127 | 255 | 0 | #7FFF00 |
cornsilk | 255 | 248 | 220 | #FFF8DC | 淡黄色 | 245 | 222 | 179 | #F5DEB3 | 钴绿色 | 61 | 145 | 64 | #3D9140 |
蛋壳色 | 252 | 230 | 201 | #FCE6C9 |
|
|
|
|
| 翠绿色 | 0 | 201 | 87 | #00C957 |
花白 | 255 | 250 | 240 | #FFFAF0 | 棕色 | 128 | 42 | 42 | #802A2A | 森林绿 | 34 | 139 | 34 | #228B22 |
gainsboro | 220 | 220 | 220 | #DCDCDC | 米色 | 163 | 148 | 128 | #A39480 | 草地绿 | 124 | 252 | 0 | #7CFC00 |
ghostWhite | 248 | 248 | 255 | #F8F8FF | 锻浓黄土色 | 138 | 54 | 15 | #8A360F | 酸橙绿 | 50 | 205 | 50 | #32CD32 |
蜜露橙 | 240 | 255 | 240 | #F0FFF0 | 锻棕土色 | 135 | 51 | 36 | #873324 | 薄荷色 | 189 | 252 | 201 | #BDFCC9 |
象牙白 | 250 | 255 | 240 | #FAFFF0 | 巧克力色 | 210 | 105 | 30 | #D2691E | 草绿色 | 107 | 142 | 35 | #6B8E23 |
亚麻色 | 250 | 240 | 230 | #FAF0E6 | 肉色 | 255 | 125 | 64 | #FF7D40 | 暗绿色 | 48 | 128 | 20 | #308014 |
navajoWhite | 255 | 222 | 173 | #FFDEAD | 黄褐色 | 240 | 230 | 140 | #F0E68C | 海绿色 | 46 | 139 | 87 | #2E8B57 |
old lace | 253 | 245 | 230 | #FDF5E6 | 玫瑰红 | 188 | 143 | 143 | #BC8F8F | 嫩绿色 | 0 | 255 | 127 | #00FF7F |
海贝壳色 | 255 | 245 | 238 | #FFF5EE | 肖贡土色 | 199 | 97 | 20 | #C76114 |
|
|
|
|
|
雪白 | 255 | 250 | 250 | #FFFAFA | 标土棕 | 115 | 74 | 18 | #734A12 | 紫色 | 160 | 32 | 240 | #A020F0 |
|
|
|
|
| 乌贼墨棕 | 94 | 38 | 18 | #5E2612 | 紫罗蓝色 | 138 | 43 | 226 | #8A2BE2 |
红色 | 255 | 0 | 0 | #FF0000 | 赫色 | 160 | 82 | 45 | #A0522D | jasoa | 160 | 102 | 211 | #A066D3 |
砖红 | 156 | 102 | 31 | #9C661F | 马棕色 | 139 | 69 | 19 | #8B4513 | 湖紫色 | 153 | 51 | 250 | #9933FA |
镉红 | 227 | 23 | 13 | #E3170D | 沙棕色 | 244 | 164 | 96 | #F4A460 | 淡紫色 | 218 | 112 | 214 | #DA70D6 |
珊瑚色 | 255 | 127 | 80 | #FF7F50 | 棕褐色 | 210 | 180 | 140 | #D2B48C | 梅红色 | 221 | 160 | 221 | #DDA0DD |
耐火砖红 | 178 | 34 | 34 | #B22222 |
|
|
|
|
|
|
|
|
| |
印度红 | 176 | 23 | 31 | #B0171F | 蓝色 | 0 | 0 | 255 | #0000FF |
|
|
|
| |
栗色 | 176 | 48 | 96 | #B03060 | 钴色 | 61 | 89 | 171 | #3D59AB |
|
|
|
| |
粉红 | 255 | 192 | 203 | #FFC0CB | dodger blue | 30 | 144 | 255 | #1E90FF |
|
|
|
| |
草莓色 | 135 | 38 | 87 | #872657 | jackie blue | 11 | 23 | 70 | #0B1746 |
|
|
|
| |
橙红色 | 250 | 128 | 114 | #FA8072 | 锰蓝 | 3 | 168 | 158 | #03A89E |
|
|
|
| |
蕃茄红 | 255 | 99 | 71 | #FF6347 | 深蓝色 | 25 | 25 | 112 | #191970 |
|
|
|
| |
桔红 | 255 | 69 | 0 | #FF4500 | 孔雀蓝 | 51 | 161 | 201 | #33A1C9 |
|
|
|
| |
深红色 | 255 | 0 | 255 | #FF00FF | 土耳其玉色 | 0 | 199 | 140 | #00C78C |
|
|
|
|
|
3. 示例代码
例1.
结果
例2.
结果