OpenCV-绘制圆端矩形(药丸)

简介: OpenCV-绘制圆端矩形(药丸)

功能函数

// 绘制圆端矩形(药丸状,pill)
void DrawPill(cv::Mat mask, const cv::RotatedRect &rotatedrect, const cv::Scalar &color, int thickness, int lineType)
{
  cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);
  // 确定短边,短边绘制圆形
  cv::RotatedRect rect = rotatedrect;
  float r = rect.size.height / 2.0f;
  if (rect.size.width > rect.size.height) {
    rect.size.width -= rect.size.height;
  }
  else {
    rect.size.height -= rect.size.width;
    r = rect.size.width / 2.0f;
  }
  cv::Point2f ps[4];
  rect.points(ps);
  // 绘制边缘
  std::vector<std::vector<cv::Point>> tmpContours;
  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(canvas, tmpContours, 0, cv::Scalar(255),5, lineType);  // 填充mask
  // 计算常长短轴
  float a = rotatedrect.size.width;
  float b = rotatedrect.size.height;
  int point01_x = (int)((ps[0].x + ps[1].x) / 2.0f);
  int point01_y = (int)((ps[0].y + ps[1].y) / 2.0f);
  int point03_x = (int)((ps[0].x + ps[3].x) / 2.0f);
  int point03_y = (int)((ps[0].y + ps[3].y) / 2.0f);
  int point12_x = (int)((ps[1].x + ps[2].x) / 2.0f);
  int point12_y = (int)((ps[1].y + ps[2].y) / 2.0f);
  int point23_x = (int)((ps[2].x + ps[3].x) / 2.0f);
  int point23_y = (int)((ps[2].y + ps[3].y) / 2.0f);
  cv::Point c0 = a < b ? cv::Point(point12_x, point12_y) : cv::Point(point23_x, point23_y);
  cv::Point c1 = a < b ? cv::Point(point03_x, point03_y) : cv::Point(point01_x, point01_y);
  // 长轴两端以填充的方式画圆,直径等于短轴
  cv::circle(canvas, c0, (int)r, cv::Scalar(255), 5, lineType);
  cv::circle(canvas, c1, (int)r, cv::Scalar(255), 5, lineType);
  // 绘制外围轮廓,如果不这样操作,会得到一个矩形加两个圆形,丑。。。
  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
}

测试代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void DrawPill(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(1000, 800), 50);
  DrawPill(result, rorect, cv::Scalar(0, 255, 255),8,16);
  imshow("original", src);
  imshow("result", result);
  waitKey(0);
  return 0;
}
// 绘制圆端矩形(药丸状,pill)
void DrawPill(cv::Mat mask, const cv::RotatedRect &rotatedrect, const cv::Scalar &color, int thickness, int lineType)
{
  cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);
  // 确定短边,短边绘制圆形
  cv::RotatedRect rect = rotatedrect;
  float r = rect.size.height / 2.0f;
  if (rect.size.width > rect.size.height) {
    rect.size.width -= rect.size.height;
  }
  else {
    rect.size.height -= rect.size.width;
    r = rect.size.width / 2.0f;
  }
  cv::Point2f ps[4];
  rect.points(ps);
  // 绘制边缘
  std::vector<std::vector<cv::Point>> tmpContours;
  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(canvas, tmpContours, 0, cv::Scalar(255),5, lineType);  // 填充mask
  // 计算常长短轴
  float a = rotatedrect.size.width;
  float b = rotatedrect.size.height;
  int point01_x = (int)((ps[0].x + ps[1].x) / 2.0f);
  int point01_y = (int)((ps[0].y + ps[1].y) / 2.0f);
  int point03_x = (int)((ps[0].x + ps[3].x) / 2.0f);
  int point03_y = (int)((ps[0].y + ps[3].y) / 2.0f);
  int point12_x = (int)((ps[1].x + ps[2].x) / 2.0f);
  int point12_y = (int)((ps[1].y + ps[2].y) / 2.0f);
  int point23_x = (int)((ps[2].x + ps[3].x) / 2.0f);
  int point23_y = (int)((ps[2].y + ps[3].y) / 2.0f);
  cv::Point c0 = a < b ? cv::Point(point12_x, point12_y) : cv::Point(point23_x, point23_y);
  cv::Point c1 = a < b ? cv::Point(point03_x, point03_y) : cv::Point(point01_x, point01_y);
  // 长轴两端以填充的方式画圆,直径等于短轴
  cv::circle(canvas, c0, (int)r, cv::Scalar(255), 5, lineType);
  cv::circle(canvas, c1, (int)r, cv::Scalar(255), 5, lineType);
  // 绘制外围轮廓,如果不这样操作,会得到一个矩形加两个圆形,丑。。。
  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
}

测试效果

      绘制圆端矩形其实就是绘制了一个旋转矩形,然后分析哪个轴更长,就在哪个轴上的两端画圆,再取外围轮廓,大功告成,通俗来讲就画了一个矩形两个圆,如图3所示。

图3 绘制逻辑

      不过注意,这个图形最好不要超过图像边界,因为超过后再分析外围轮廓,它认为的外围就到了内部,如图4所示。

图4 外围线

      然后,你就会得到一个奇葩图形,如图5所示。

图5 示意图

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

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