OpenCV-绘制圆角矩形

简介: OpenCV-绘制圆角矩形

功能函数

// 绘制圆角矩形
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 绘制逻辑

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

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

相关文章
|
6月前
|
API C++ 计算机视觉
【opencv3】鼠标框选矩形并显示当前像素点坐标和矩形中心点坐标C++
【opencv3】鼠标框选矩形并显示当前像素点坐标和矩形中心点坐标C++
|
6月前
|
计算机视觉 Python
OpenCV中线段、矩形、圆形、多边形的讲解与绘制实战(附Python源码)
OpenCV中线段、矩形、圆形、多边形的讲解与绘制实战(附Python源码)
131 0
|
计算机视觉
OpenCV-矩形边框cv::boundingRect
OpenCV-矩形边框cv::boundingRect
|
6月前
|
计算机视觉
OpenCV(三十四):轮廓外接最大、最小矩形和多边形拟合
OpenCV(三十四):轮廓外接最大、最小矩形和多边形拟合
439 0
|
6月前
|
计算机视觉
OpenCV(十三):图像中绘制直线、圆形、椭圆形、矩形、多边形和文字
OpenCV(十三):图像中绘制直线、圆形、椭圆形、矩形、多边形和文字
82 0
|
11月前
|
数据安全/隐私保护 计算机视觉
opencv边缘检测加提取(圆形和矩形)
opencv边缘检测加提取(圆形和矩形)
68 0
|
计算机视觉
OpenCV-最小包围旋转矩形边框cv::minAreaRect
OpenCV-最小包围旋转矩形边框cv::minAreaRect
161 0
|
计算机视觉
OpenCV-绘制旋转矩形
OpenCV-绘制旋转矩形
164 0
|
计算机视觉
OpenCV-基本图形绘制(圆、矩形、椭圆)
OpenCV-基本图形绘制(圆、矩形、椭圆)