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