📢:如果你也对机器人、人工智能感兴趣,看来我们志同道合✨
📢:不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852】
📢:文章若有幸对你有帮助,可点赞 👍 收藏 ⭐不迷路🙉
📢:内容若有错误,敬请留言 📝指正!原创文,转载请注明出处
文章目录
point:点的坐标
scalar:颜色向量
绘制线、矩形、圆、椭圆
Scalar表示四个元素的向量
Scalar(a, b, c);// a = blue, b = green, c = red表示RGB三个通道
Scalar color = Scalar(0, 0, 255);//颜色设置为红色
一、绘制直线
1.1API- line绘制直线
void line(
InputOutputArray img, //输入图像
Point pt1, //端点1
Point pt2, //端点2
const Scalar& color,//颜色设置
int thickness = 1, //粗细程度
int lineType = LINE_8, //直线类型,一共三种:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。
int shift = 0//点坐标中的小数位数,一般用默认值0。
);
1.2端点表示方法
Point表示2D平面上一个点x,y
第一种方法是分别给横纵坐标赋值:
Point p;
p.x = 10; p.y = 8;
第二种方法是一次性赋值:
Point p;
p = Pont(10,8);
1.3全部代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main() {
Mat src1 = imread("D:/images/hand.jpg");
if (src1.empty()) {
printf("could not load image...\n");
return -1;
}
imshow("【input picture】", src1);
//定义端点p1,p2
Point p1, p2;
p1 = Point(100, 3);
p2 = Point(100,400);
Scalar color = Scalar(0, 0, 255);
line(src1, p1, p2, color, 1, LINE_4);
imshow("【addLine】", src1);
waitKey(0);
}
1.4效果展示
二、绘制矩形
2.1API-rectangle
函数原型:
void rectangle(
Mat& img,
Rect rec,
const Scalar& color,
int thickness = 1,
int lineType = LINE_8,
int shift = 0
);
函数中参数含义如下:
img :输入图像。
rec :一个矩形,包含四个参数,横纵坐标以及长宽。
color :直线的颜色。
thickness :直线的粗细程度。
lineType :直线类型,一共有三个:LINE_4,LINE_8,LINE_AA,其中LINE_AA是无锯齿直线。
shift :点坐标中的小数位数,一般用默认值。
2.2实例代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main() {
Mat src = imread("D:/images/hand.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
imshow("【input picture】", src);
Rect rect = Rect(20, 10, 150, 200);//左上角坐标(20,10)以及矩形宽(150)、高(200)
Scalar color = Scalar(0, 0, 255);
rectangle(src, rect, color, 1, 4);//在原图src中绘制矩形
imshow("【addRectangle】", src);//显示调用矩阵后的图像
waitKey(0);
destroyAllWindows;
}
2.3效果展示
三、绘制椭圆
3.1API-Ellipse
Ellipse是绘制椭圆操作的函数原型,其参数如下:
void ellipse(
InputOutputArray img,
Point center,
Size axes,
double angle,
double startAngle,
double endAngle,
const Scalar& color,
int thickness = 1,
int lineType = LINE_8,
int shift = 0
);
参数含义如下:
img :输入图像。
center:椭圆中心。
axes:轴线。
angle:椭圆旋转角度,单位为度。
startAngle:椭圆弧的起始角,单位为度。
endAngle:椭圆弧的终止角,单位为度。
color:椭圆的颜色。
thickness:椭圆的粗细程度。
lineType :椭圆线类型,一共有三个:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。
shift :点坐标中的小数位数,一般用默认值。
3.2全部代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main() {
Mat src1 = imread("D:/images/hand.jpg");
if (!src1.data) {
printf("could not load image...\n");
return -1;
}
imshow("【input picture】", src1);
Point p1 = Point(src1.cols / 2, src1.rows / 2);
Size s1 = Size(src1.cols / 4, src1.rows / 4);
Scalar color = Scalar(242, 123, 215);
ellipse(src1, p1, s1, 40, 0, 360, color, 1, LINE_AA);
imshow("【addEllipse】", src1);
waitKey(0);
}
3.3效果展示
四、绘制圆
4.1API-circle
函数原型的参数如下:
void circle(
InputOutputArray img,
Point center,
int radius,
const Scalar& color,
int thickness = 1,
int lineType = LINE_8,
int shift = 0
);
参数含义如下:
img :输入图像。
center:圆心。
radius:半径。
color :圆的颜色。
thickness :圆的粗细程度。
lineType :圆线的类型,一共有三个:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。
shift:点坐标中的小数位数,一般用默认值。
4.2全部代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main() {
Mat src1 = imread("D:/images/hand.jpg");
if (src1.empty()) {
printf("could not load image...\n");
return -1;
}
imshow("【input picture】", src1);
Point center = Point(src1.cols / 2, src1.rows / 2);//确定圆心
Scalar color = Scalar(0, 255, 255);
circle(src1, center, 50, color, 1, 4);
imshow("【addCircle】", src1);
waitKey(0);
}