OpenCV-基本图形绘制(圆、矩形、椭圆)

作者:翟天保Steven
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处

circle函数原型

void circle(InputOutputArray img, Point center, int radius,
            const Scalar& color, int thickness = 1,
            int lineType = LINE_8, int shift = 0);

circle参数说明

  1. InputOutputArray类型的img,输入图像也是输出图像,如Mat类型。
  2. Point类型的center,圆心位置。
  3. int类型的radius,圆形半径。
  4. Scalar类型的color,文字颜色。
  5. int类型的thickness,文字线条宽度。
  6. int类型的line_type,绘制线的类型,-1就是FILLED(填满),4是LINE_4(4连通域),8是LINE_8(8连通域),LINE_AA(抗锯齿线)。
  7. int类型的shift,中心坐标和半径值中的小数位数。

 rectangle函数原型

void rectangle(InputOutputArray img, Rect rec,
               const Scalar& color, int thickness = 1,
               int lineType = LINE_8, int shift = 0);

 rectangle参数说明

  1. InputOutputArray类型的img,输入图像也是输出图像,如Mat类型。
  2. Rect类型的rec,矩形位置。
  3. Scalar类型的color,文字颜色。
  4. int类型的thickness,文字线条宽度。
  5. int类型的line_type,绘制线的类型,-1就是FILLED(填满),4是LINE_4(4连通域),8是LINE_8(8连通域),LINE_AA(抗锯齿线)。
  6. int类型的shift,中心坐标和半径值中的小数位数。

 ellipse函数原型

void ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color,
             int thickness = 1, int lineType = LINE_8);

ellipse参数说明

  1. InputOutputArray类型的img,输入图像也是输出图像,如Mat类型。
  2. RotatedRect类型的box,椭圆位置,里面有三个参数,中心,长轴短轴尺寸,角度。
  3. Scalar类型的color,文字颜色。
  4. int类型的thickness,文字线条宽度。
  5. int类型的line_type,绘制线的类型,-1就是FILLED(填满),4是LINE_4(4连通域),8是LINE_8(8连通域),LINE_AA(抗锯齿线)。

测试代码

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

using namespace std;
using namespace cv;

void DrawCircle(cv::Mat mask,const cv::Point2i &center, int radius,const cv::Scalar &color, int thickness);
void DrawRect(cv::Mat mask, const cv::Rect &rect, const cv::Scalar &color, int thickness);
void DrawEllipse(cv::Mat mask, const cv::RotatedRect &ellipse, const cv::Scalar &color, int thickness);

int main()
{
	cv::Mat src = imread("test.jpg");
	cv::Mat result = src.clone();
	DrawCircle(result, cv::Point(src.cols / 2, src.rows / 2), 150, Scalar(0, 0, 255), 16);
	DrawRect(result, cv::Rect(100, 50, 1200, 1000), Scalar(0, 255, 255), 16);
	DrawEllipse(result, cv::RotatedRect(cv::Point(src.cols / 2, src.rows / 2),cv::Size(300,200),135), Scalar(255, 255, 255), 16);
	imshow("original", src);
	imshow("result", result);
	waitKey(0);
	return 0;
}
// 绘制圆形
void DrawCircle(cv::Mat mask,const cv::Point2i &center, int radius,const cv::Scalar &color, int thickness)
{
	cv::circle(mask, center, radius, color, thickness);
}
// 绘制矩形
void DrawRect(cv::Mat mask,const cv::Rect &rect, const cv::Scalar &color, int thickness)
{
	cv::rectangle(mask, rect, color, thickness);
}
// 画椭圆
void DrawEllipse(cv::Mat mask,const cv::RotatedRect &ellipse,const cv::Scalar &color, int thickness)
{
	cv::ellipse(mask, ellipse, color, thickness);
}

测试效果 

图1 原图
图2 图形绘制

       图形绘制是图像处理中常用的功能之一,圆形和矩形没什么好说的。如图2所示,椭圆创建有三个参数,中心同圆一样,尺寸是全长轴和全短轴,注意不是半长,旋转的坐标系同Mat坐标系一样,往下往右为正,所以旋转135°就是图中的样子。

       写这篇文章是为了后面绘制复杂图形做铺垫,如圆端矩形、圆角矩形、多边形、同心圆等等。

       如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!

© 版权声明
THE END
喜欢就支持一下吧
点赞714 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容