opencv学习-绘制形状
Posted 殇堼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv学习-绘制形状相关的知识,希望对你有一定的参考价值。
point:点的坐标
scalar:颜色向量
绘制线、矩形、圆、椭圆
Scalar表示四个元素的向量
Scalar(a, b, c);// a = blue, b = green, c = red表示RGB三个通道
Scalar color = Scalar(0, 0, 255);//颜色设置为红色
1、绘制直线API- 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.1端点表示方法
Point表示2D平面上一个点x,y
Point p;
1 p.x = 10; p.y = 8;
或是 2 p = Pont(10,8);
1.2实例代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat src1 = imread("D:/images/hand.jpg");
void addLine() {
Point p1, p2;
p1 = Point(src1.cols / 2, src1.rows / 2);
p2.x = 10;
p2.y = 10;
Scalar color = Scalar(0, 0, 255);
line(src1, p1, p2, color, 1, LINE_4);
}
int main() {
if (src1.empty()) {
printf("could not load image...\\n");
return -1;
}
imshow("【input picture】", src1);
addLine();
imshow("【addLine】", src1);
waitKey(0);
}
2、绘制矩形-rectangle
2.1实例代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat src1 = imread("D:/images/hand.jpg");
void addRectangle()//矩形设置
{
Rect rect = Rect(20, 10, 150, 200);//左上角坐标(20,10)以及宽(150)、高(200)
Scalar color = Scalar(0, 0, 255);
rectangle(src1, rect, color, 1, 4);
}
int main(){
if (src1.empty()) {
printf("could not load image...\\n");
return -1;
}
imshow("【input picture】", src1);
addRectangle();//调用矩形
imshow("【addRectangle】", src1);//显示调用矩阵后的图像
waitKey(0);
destroyAllWindows;
}
3、绘制椭圆-Ellipse
3.1实例代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat src1 = imread("D:/images/hand.jpg");
void addEllipse()
{
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);
}
int main(){
if (!src1.data) {
printf("could not load image...\\n");
return -1;
}
imshow("【input picture】", src1);
addEllipse();
imshow("【addEllipse】", src1);
waitKey(0);
}
4.绘制圆
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
Mat src1 = imread("D:/images/hand.jpg");
void addCircle(){
Point center = Point(src1.cols / 2, src1.rows / 2);
Scalar color = Scalar(0, 255, 255);
circle(src1, center, 50, color, 1, 4);
}
int main(){
if (src1.empty()) {
printf("could not load image...\\n");
return -1;
}
imshow("【input picture】", src1);
addCircle();
imshow("【addCircle】", src1);
waitKey(0);
}
以上是关于opencv学习-绘制形状的主要内容,如果未能解决你的问题,请参考以下文章
OpenCV学习实践 opencv代码学习实践:Basic Drawing