打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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°就是图中的样子。

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

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Opencv系列1.6--画图和注释
支持向量机(SVM)介绍 — OpenCV 2.3.2 documentation
【OpenCV3】文字绘制
OpenCV中如何正确的给文字区域加上底色
【从零学习OpenCV 4】绘制几何图形
opencv轮廓的简单应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服