版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons)
color问题:图形的颜色会受到图像通道数的影响。如图像是灰度图,那么图形彩色失效,只会在图片上显示出灰度的线条。
一、线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;
}
二、矩形rect
原型
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;
}