图形以及文字绘制
Mat上绘制基本几何图形和文本,包括矩形,直线,圆,椭圆,还有文本文字
Point 点对象
就和绘图的点一样,两个点构造一条直线
Point pt1 = new Point(15,30);// pt1(15,30) 坐标
绘制直线
Api函数如下:
line(Mat img, Point pt1, Point pt2, Scalar color)
- img:绘制图形到此Mat上
- pt1:第一个点坐标
- pt2:第二个点坐标
- color :代表颜色
绘制矩形
rectangle(Mat img, Point pt1, Point pt2, Scalar color)
- img:绘制图形到此Mat上
- pt1:第一个点坐标
- pt2:第二个点坐标
- color :代表颜色
绘制圆形
circle(Mat img, Point v, int radius, Scalar color)
- img:绘制图形到此Mat上
- center:中心点
- radius:半径
- color :代表颜色
绘制椭圆形
ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle, Scalar color)
- img:绘制图形到此Mat上
- center:中心点
- axes:椭圆的长轴与端州大小,单位是像素
- angle:代表旋转角度
- startAngle:代表开始调度大小
- endAngle:结束角度大小
- color :代表颜色
绘制文字
putText(Mat img, String text, Point org, int fontFace, double fontScale, Scalar color)
- img:绘制图形到此Mat上
- text:要绘制的文本
- org:开始点屏幕坐标
- fontFace:字体类型
- fontScale:字体大小
- color:字体颜色
结果和代码
//要绘制的Mat
Mat s1 = new Mat(600,600, CvType.CV_8UC3);
Point pt1,pt2;//定义两个点
//B:0 G:0 R:255 红色
Scalar color = new Scalar(0,0,255);
//绘制圆形
pt1 = new Point(15,30);
pt2 = new Point(155,80);
Imgproc.line(s1,pt1,pt2,color);
//绘制矩形
pt1 = new Point(80,120);
pt2 = new Point(180,180);
Imgproc.rectangle(s1,pt1,pt2,color);
//绘制圆形
pt1 = new Point(380,280);
Imgproc.circle(s1,pt1,80,color);
//绘制椭圆形
pt1 = new Point(120,280);
Size axes = new Size(100,50);
Imgproc.ellipse(s1,pt1,axes,360,0,360,color);
//绘制文字
pt1 = new Point(10,450);
Imgproc.putText(s1,"Happy new year ",pt1,Imgproc.FONT_HERSHEY_COMPLEX,2,color);
//展示
HighGui.imshow("图形",s1);