<LearnOpenCV(7) C++>矩形、圆形等基本图像绘制

矩形、圆形等基本图像绘制

简单记录OpenCV库提供的圆形、椭圆、线段等基础图象绘制API,顺便记录下TXT绘制。

API

cv::Point 图像坐标

typedef Point_<int> Point2i;
typedef Point2i Point;

二维int数组表图像坐标系下的一个像素。详见Point_类

cv::Scalar 颜色

Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);
typedef Scalar_<double> Scalar;

表示4元素的向量,定义颜色用,详见Scalar_类

cv::Rect 区域

Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
typedef Rect_<int> Rect2i;
typedef Rect2i Rect;

根据基点和尺寸找确定区域,详见Rect_类

cv::line 直线

CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
                     int thickness = 1, int lineType = LINE_8, int shift = 0);
  • img 目标图像
  • pt1 端点1
  • pt2 端点2
  • color 线颜色
  • thickness 粗细
  • linttype 类型
代码
void setlines() {
	Point p1 = Point(20, 30);
	Point p2;
	p2.x = 200;
	p2.y = 300;
	Scalar color = Scalar(250, 250, 100);
	line(src, p1, p2, color, 1, LINE_8);
	return;
}

cv::rectangle 矩形

CV_EXPORTS_W void rectangle(InputOutputArray img, Rect rec,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);
  • img 图像
  • rec 目标区域
  • color 颜色
  • thickness 粗细
  • linetype 类型
代码
void setRectangle()
{
	Rect rect = Rect(250, 100, 300, 300);
	Scalar color = Scalar(250, 250, 100);
	rectangle(src, rect, color, 2, LINE_8);
	return;
}

cv::ellipse 椭圆

CV_EXPORTS_W 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);
  • src 图像
  • center 圆心
  • axes 长短轴长度
  • angle 旋转角
  • startAngle 开始角
  • endAngele 结束角
  • color 颜色
  • thickness 宽度
  • lintType 类型
代码
void setEllipse()

{
	Scalar color = Scalar(250, 250, 100);
	ellipse(src, Point(src.cols / 2, src.rows / 2), Size( src.rows / 4, src.cols / 4),90,0,360,color,2,LINE_8);
	return;
}

cv::circle 圆

CV_EXPORTS_W 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 类型
代码
void setCircle() {
	Scalar color = Scalar(250, 250, 100);
	circle(src, Point(src.cols / 2, src.rows / 2), (int)src.rows / 2, color, 2, LINE_8);
	return;
}

cv::fillPoly 多边形

CV_EXPORTS void fillPoly(InputOutputArray img, const Point** pts,
                         const int* npts, int ncontours,
                         const Scalar& color, int lineType = LINE_8, int shift = 0,
                         Point offset = Point() );
void setPolygon()
{
	Point pts[1][5];
	pts[0][0] = Point(100,100);
	pts[0][1] = Point (100,200);
	pts[0][2] = Point (200, 200);
	pts[0][3] = Point (200, 100);
	pts[0][4] = Point (100, 100);
	Scalar color = Scalar(255, 0, 100);
	const Point* ppts[] = { pts[0] };
	int npt[] = { 5 };
	fillPoly(src, ppts, npt, 1, color, 8);
	return;
}

结果及代码

在这里插入图片描述

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;
Mat src, dst;

void setlines();
void setRectangle();
void setEllipse();
void setCircle();
void setPolygon();
int main() {
	src = imread("girl6.jpg");
	if (!src.data)
	{
		printf("Wrong with Loading img!\n");
		return -1;
	}
	setlines();
	setRectangle();
	setEllipse();
	setCircle();
	setPolygon();

	putText(src, "Hello OpenCV",Point(src.cols/2,src.rows/2),FONT_HERSHEY_COMPLEX_SMALL,1.0,Scalar(255,255,100),1,8);//这里是txt忘了写了
	namedWindow("draw_window", WINDOW_AUTOSIZE);
	imshow("draw_window", src);
	waitKey(0);
	return -1;
}

void setlines() {
	Point p1 = Point(20, 30);
	Point p2;
	p2.x = 200;
	p2.y = 300;
	Scalar color = Scalar(250, 250, 100);
	line(src, p1, p2, color, 1, LINE_8);
	return;
}

void setRectangle()
{
	Rect rect = Rect(250, 100, 300, 300);
	Scalar color = Scalar(250, 250, 100);
	rectangle(src, rect, color, 2, LINE_8);
	return;
}
void setEllipse()

{
	Scalar color = Scalar(250, 250, 100);
	ellipse(src, Point(src.cols / 2, src.rows / 2), Size( src.rows / 4, src.cols / 4),90,0,360,color,2,LINE_8);
	return;
}
void setCircle() {
	Scalar color = Scalar(250, 250, 100);
	circle(src, Point(src.cols / 2, src.rows / 2), (int)src.rows / 2, color, 2, LINE_8);
	return;
}
void setPolygon()
{
	Point pts[1][5];
	pts[0][0] = Point(100,100);
	pts[0][1] = Point (100,200);
	pts[0][2] = Point (200, 200);
	pts[0][3] = Point (200, 100);
	pts[0][4] = Point (100, 100);
	Scalar color = Scalar(255, 0, 100);
	const Point* ppts[] = { pts[0] };
	int npt[] = { 5 };
	fillPoly(src, ppts, npt, 1, color, 8);
	return;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wdmcs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值