OpenCV绘制圆角矩形的方法实例

作者:翟天保Steven 时间:2023-07-15 11:37:17 

功能函数


// 绘制圆角矩形
void DrawRotatedRectChamfer(cv::Mat mask,const cv::RotatedRect rotatedrect, float radius,const cv::Scalar &color, int thickness, int lineType)
{
// 创建画布
cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);

cv::RotatedRect newrotatedrect = rotatedrect;

//画宽缩减后的矩形
cv::RotatedRect r1 = newrotatedrect;
r1.size.width = r1.size.width - 2 * radius;
DrawRotatedRect(canvas, r1, cv::Scalar(255), 5, lineType);

//画高缩减后的矩形
cv::RotatedRect r2 = newrotatedrect;
r2.size.height = r2.size.height - 2 * radius;
DrawRotatedRect(canvas, r2, cv::Scalar(255), 5,lineType);

//画四个角的圆
cv::RotatedRect r3 = r2;
r3.size.width = r1.size.width;

cv::Point2f ps[4];
r3.points(ps);

for (int i = 0; i != 4; ++i) {
cv::circle(canvas, ps[i], (int)radius, cv::Scalar(255), 5);
}
std::vector<std::vector<cv::Point>> EXcontours;
cv::findContours(canvas, EXcontours, cv::RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
drawContours(mask, EXcontours, 0, color, thickness, lineType);  // 填充mask

}

// 绘制旋转矩形
void DrawRotatedRect(cv::Mat mask,const cv::RotatedRect rotatedrect,const cv::Scalar &color, int thickness,int lineType)
{
cv::Point2f ps[4];
rotatedrect.points(ps);

std::vector<std::vector<cv::Point>> tmpContours;    // 创建一个InputArrayOfArrays 类型的点集
std::vector<cv::Point> contours;
for (int i = 0; i != 4; ++i) {
contours.emplace_back(cv::Point2i(ps[i]));
}
tmpContours.insert(tmpContours.end(), contours);
drawContours(mask, tmpContours, 0, color, thickness, lineType);  // 填充mask
}

测试代码


#include <iostream>
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;

void DrawRotatedRectChamfer(cv::Mat mask, const cv::RotatedRect rotatedrect, float radius, const cv::Scalar &color, int thickness, int lineType);
void DrawRotatedRect(cv::Mat mask, const cv::RotatedRect rotatedrect, const cv::Scalar &color, int thickness, int lineType);

int main()
{
cv::Mat src = imread("test.jpg");
cv::Mat result = src.clone();
cv::RotatedRect rorect(cv::Point(src.cols / 2, src.rows / 2), cv::Size(800, 600), 50);
DrawRotatedRectChamfer(result, rorect, 100, cv::Scalar(0, 0, 255), 5, 16);
imshow("original", src);
imshow("result", result);
waitKey(0);
return 0;
}

// 绘制圆角矩形
void DrawRotatedRectChamfer(cv::Mat mask,const cv::RotatedRect rotatedrect, float radius,const cv::Scalar &color, int thickness, int lineType)
{
// 创建画布
cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);

cv::RotatedRect newrotatedrect = rotatedrect;

//画宽缩减后的矩形
cv::RotatedRect r1 = newrotatedrect;
r1.size.width = r1.size.width - 2 * radius;
DrawRotatedRect(canvas, r1, cv::Scalar(255), 5, lineType);

//画高缩减后的矩形
cv::RotatedRect r2 = newrotatedrect;
r2.size.height = r2.size.height - 2 * radius;
DrawRotatedRect(canvas, r2, cv::Scalar(255), 5,lineType);

//画四个角的圆
cv::RotatedRect r3 = r2;
r3.size.width = r1.size.width;

cv::Point2f ps[4];
r3.points(ps);

for (int i = 0; i != 4; ++i) {
cv::circle(canvas, ps[i], (int)radius, cv::Scalar(255), 5);
}
std::vector<std::vector<cv::Point>> EXcontours;
cv::findContours(canvas, EXcontours, cv::RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
drawContours(mask, EXcontours, 0, color, thickness, lineType);  // 填充mask

}

// 绘制旋转矩形
void DrawRotatedRect(cv::Mat mask,const cv::RotatedRect rotatedrect,const cv::Scalar &color, int thickness,int lineType)
{
cv::Point2f ps[4];
rotatedrect.points(ps);

std::vector<std::vector<cv::Point>> tmpContours;    // 创建一个InputArrayOfArrays 类型的点集
std::vector<cv::Point> contours;
for (int i = 0; i != 4; ++i) {
contours.emplace_back(cv::Point2i(ps[i]));
}
tmpContours.insert(tmpContours.end(), contours);
drawContours(mask, tmpContours, 0, color, thickness, lineType);  // 填充mask
}

测试效果

OpenCV绘制圆角矩形的方法实例

图1 原图

OpenCV绘制圆角矩形的方法实例

图2 绘制圆角矩形

绘制圆角矩形其实是绘制了两个旋转矩形,交叉组合,然后以四交点为圆心绘制圆,就组成了圆角矩形,如图3所示。

OpenCV绘制圆角矩形的方法实例
图3 绘制逻辑

同理,这个最好不要绘制到图像外面,否则它在分析外围线的时候会出错,可以多加些判断来修正,我就不写了,交给兄弟们研究一下~

总结

来源:https://blog.csdn.net/zhaitianbao/article/details/119994876

标签:opencv,圆角,矩形
0
投稿

猜你喜欢

  • 单选的radia,和多选的checkbox如何判定选择?

    2008-11-26 12:29:00
  • Python接口自动化之接口依赖

    2021-09-03 15:53:09
  • 推荐19个很有用的 JavaScript 库

    2011-05-14 16:33:00
  • JS实现在线统计一个页面内鼠标点击次数的方法

    2024-04-28 09:37:35
  • 利用OpenCV+Tensorflow实现的手势识别

    2022-04-03 02:12:33
  • 如何做一个可以让人家申请使用的计数器?

    2010-07-11 21:17:00
  • 如何处理json中不带双引号的key的问题

    2023-02-07 00:25:22
  • 使用Python操作MySql数据库和MsSql数据库

    2024-01-21 02:20:00
  • PHP递归调用数组值并用其执行指定函数的方法

    2023-09-05 15:35:04
  • Python创建增量目录的代码实例

    2021-12-07 04:12:55
  • Python中使用glob和rmtree删除目录子目录及所有文件的例子

    2023-12-26 07:14:18
  • PHP使用缓存即时输出内容(output buffering)的方法

    2023-11-23 20:12:11
  • Web 前端优化最佳实践之 Cookie 篇

    2008-06-25 13:00:00
  • Python MOCK SERVER moco模拟接口测试过程解析

    2023-12-15 06:37:00
  • Python如何使用type()函数查看数据的类型

    2022-05-30 17:49:31
  • Python计算程序运行时间的方法

    2023-08-09 09:53:50
  • VuePress使用Algolia实现全文搜索

    2024-04-28 09:29:56
  • ASP+JAVAScript:复杂表单的动态生成与验证

    2007-10-06 21:51:00
  • python条件和循环的使用方法

    2021-12-16 22:27:59
  • Python+OpenCV之形态学操作详解

    2021-11-24 23:37:58
  • asp之家 网络编程 m.aspxhome.com