// 绘制圆角矩形
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::vectorstd::vectorcv::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::vectorstd::vectorcv::Point>> tmpContours; // 创建一个InputArrayOfArrays 类型的点集
std::vectorcv::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::vectorstd::vectorcv::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::vectorstd::vectorcv::Point>> tmpContours; // 创建一个InputArrayOfArrays 类型的点集
std::vectorcv::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绘制圆角矩形的文章就介绍到这了,更多相关OpenCV绘制圆角矩形内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!