打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
OpenCV绘制文字、图形

文章目录

  • 一、文字putText
  • 二、线line
  • 三、矩形rectangle
  • 四、圆circle
  • 五、椭圆ellipse()

color问题:图形的颜色会受到图像通道数的影响。如图像是灰度图,那么图形彩色失效,只会在图片上显示出灰度的线条。


一、文字putText

原型

void putText(InputOutputArray img,const String &text,Point org,int fontFace,double fontScale,Scalar color,int thickness = 1,int lineType = LINE_8,bool bottomLeftOrigin = false )

参数

  • img:图像
  • text:文本
  • org:图像中文本字符串的左下角。
  • fontFace:字体类型
  • fontScale:字体比例
  • color:Scalar(b,g,r)
  • thickness:厚度,线的粗细。不能为-1,会出错。
  • lineType:线的类型。请参见LineTypes。
  • shift:转移,点坐标中的小数位数。

例:普通风格的FONT_HERSHEY_SIMPLEX

#include<opencv2/opencv.hpp>using namespace cv;int main(){Mat dstImage=Mat::zeros(300,400,CV_8UC3);String text="Love wins!";putText(dstImage,text,Point(100,200),FONT_HERSHEY_SIMPLEX,1,Scalar::all(255));imshow("dstImage",dstImage);waitKey();return 0;}

例:手写风格的FONT_HERSHEY_SIMPLEX

#include<opencv2/opencv.hpp>using namespace cv;int main(){Mat dstImage=Mat::zeros(300,400,CV_8UC3);String text="Love wins!";putText(dstImage,text,Point(100,200),FONT_HERSHEY_SCRIPT_SIMPLEX,1,Scalar::all(255));imshow("dstImage",dstImage);waitKey();return 0;}

二、线line

原型

void line(InputOutputArray img,Point pt1,Point pt2,const Scalar &color,int thickness=1,int lineType=LINE_8,int shift=0 )

参数

  • img:图像
  • pt1:起点
  • pt2:终点
  • color:Scalar(b,g,r)
  • thickness:厚度,线的粗细。不能为-1,会出错。
  • lineType:线的类型。请参见LineTypes。
  • shift:转移,点坐标中的小数位数。

例:画一条从(100,200)到(250,100)的蓝线

#include<opencv2/opencv.hpp>using namespace cv;int main(){Mat dstImage=Mat::zeros(300,400,CV_8UC3);line(dstImage,Point(100,200),Point(250,100),Scalar(255,102,0));imshow("dstImage",dstImage);waitKey();return 0;}

三、矩形rectangle

原型

void rectangle(InputOutputArray img,Point pt1,Point pt2,const Scalar &color,int thickness=1,int lineType=LINE_8,int shift=0 )

参数

  • img:图像
  • pt1:对角点
  • pt2:另一个对角点
  • color:Scalar(b,g,r)
  • thickness:厚度,线的粗细。当为-1时,表示绘制实心的。
  • lineType:线的类型。请参见LineTypes。
  • shift:转移,点坐标中的小数位数。

例:画一个两个对角点为(100,200)(250,100)的空心矩形

#include<opencv2/opencv.hpp>using namespace cv;int main(){Mat dstImage=Mat::zeros(300,400,CV_8UC3);rectangle(dstImage,Point(100,200),Point(250,100),Scalar(255,102,0));imshow("dstImage",dstImage);waitKey();return 0;}

例:画一个两个对角点为(100,200)(250,100)的实心矩形

#include<opencv2/opencv.hpp>using namespace cv;int main(){Mat dstImage=Mat::zeros(300,400,CV_8UC3);rectangle(dstImage,Point(100,200),Point(250,100),Scalar(255,102,0),-1);imshow("dstImage",dstImage);waitKey();return 0;}

四、圆circle

原型

void circle(InputOutputArray img,Point center,int radius,const Scalar &color,int thickness = 1,int lineType = LINE_8,int shift = 0 )

参数

  • img:图像
  • center:圆心
  • redius:半径
  • color:Scalar(b,g,r)
  • thickness:厚度,线的粗细。当为-1时,表示绘制实心的。
  • lineType:线的类型。请参见LineTypes。
  • shift:转移,点坐标中的小数位数。

例:画一个圆心为(100,200),半径为100的空心圆形

#include<opencv2/opencv.hpp>using namespace cv;int main(){Mat dstImage=Mat::zeros(300,400,CV_8UC3);circle(dstImage,Point(100,200),100,Scalar::all(255));imshow("dstImage",dstImage);waitKey();return 0;}

例:画一个圆心为(100,200),半径为100的实心圆形

#include<opencv2/opencv.hpp>using namespace cv;int main(){Mat dstImage=Mat::zeros(300,400,CV_8UC3);circle(dstImage,Point(100,200),100,Scalar::all(255),-1);imshow("dstImage",dstImage);waitKey();return 0;}

五、椭圆ellipse()

原型

void ellipse(InputOutputArray img, RotatedRect &box, Scalar &color, int thickness = 1, int lineType = 8)

参数

  • img:图像
  • box:一个旋转矩形,用来锁定椭圆
  • color:Scalar(b,g,r)
  • thickness:厚度,线的粗细。当为-1时,表示绘制实心的。
  • lineType:线的类型。请参见LineTypes。

例:

#include<opencv2/opencv.hpp>using namespace std;using namespace cv;int main(){Mat image=Mat::zeros(600,600,CV_8UC3);RotatedRect rect(Point(100,100),Point(300,100),Point(300,400));Point2f hull[4];rect.points(hull);for(int i=0;i<4;i++){line(image,hull[i],hull[(i+1)%4],Scalar::all(255),2);}ellipse(image,rect,Scalar::all(255),2);imshow("Result",image);waitKey();return 0;}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Opencv之<vec3b>是什么东东 </vec3b>
【OpenCV学习笔记】三十、轮廓特征属性及应用(七)
OpenCV学习笔记(四十)——再谈OpenCV数据结构Mat详解
opencv split和merge操作
像素值的读写
OpenCV探索之路(五):图片缩放和图像金字塔
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服