OpenCV绘制文字、图形


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;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值