OpenCV绘制圆角矩形的方法实例
程序员文章站
2022-06-15 12:30:15
功能函数// 绘制圆角矩形void drawrotatedrectchamfer(cv::mat mask,const cv::rotatedrect rotatedrect, float radiu...
功能函数
// 绘制圆角矩形 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 }
测试效果
图1 原图
图2 绘制圆角矩形
绘制圆角矩形其实是绘制了两个旋转矩形,交叉组合,然后以四交点为圆心绘制圆,就组成了圆角矩形,如图3所示。
图3 绘制逻辑
同理,这个最好不要绘制到图像外面,否则它在分析外围线的时候会出错,可以多加些判断来修正,我就不写了,交给兄弟们研究一下~
总结
到此这篇关于opencv绘制圆角矩形的文章就介绍到这了,更多相关opencv绘制圆角矩形内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 【经验分享】零基础Python学习路线及阶段学习目标
下一篇: 深度学习模型的构建