当前位置: 首页 > news >正文

28 OpenCV 轮廓周围绘制图形

文章目录

  • approxPolyDP 轮廓周围绘制矩形
  • boundingRect
  • minAreaRect
  • 绘制圆和椭圆
  • 示例

approxPolyDP 轮廓周围绘制矩形

approxPolyDP(InputArray  curve, OutputArray approxCurve,  double  epsilon,  bool  closed)curve:输入点集,二维点向量的集合approxCurve:输出点集,表示拟合曲线或多边形,数据与输入参数 curve 一致epsilon:指定的近似精度,原始曲线与近似曲线之间的最大距离close: 闭合标志,True 表示闭合多边形,False 表示多边形不闭合

boundingRect

boundingRect(InputArray points)得到轮廓周围最小矩形左上交点坐标和右下角点坐标,绘制一个矩形

minAreaRect

minAreaRect(InputArray  points)得到一个旋转的矩形,返回旋转矩形

绘制圆和椭圆

cv::minEnclosingCircle(InputArray points, //得到最小区域圆形Point2f& center, // 圆心位置float& radius)// 圆的半径
cv::fitEllipse(InputArray  points)得到最小椭圆

示例

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>using namespace std;
using namespace cv;
Mat src, gray_src, drawImg;
int threshold_v = 170;
int threshold_max = 255;
const char* output_win = "rectangle-demo";
RNG rng(12345);
void Contours_Callback(int, void*);
int main(int argc, char** argv) {src = imread("D:/vcprojects/images/hotball.png");if (!src.data) {printf("could not load image...\n");return -1;}cvtColor(src, gray_src, CV_BGR2GRAY);blur(gray_src, gray_src, Size(3, 3), Point(-1, -1));const char* source_win = "input image";namedWindow(source_win);namedWindow(output_win);imshow(source_win, src);createTrackbar("Threshold Value:", output_win, &threshold_v, threshold_max, Contours_Callback);Contours_Callback(0, 0);waitKey(0);return 0;
}void Contours_Callback(int, void*) {Mat binary_output;vector<vector<Point>> contours;vector<Vec4i> hierachy;threshold(gray_src, binary_output, threshold_v, threshold_max, THRESH_BINARY);//imshow("binary image", binary_output);findContours(binary_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));vector<vector<Point>> contours_ploy(contours.size());vector<Rect> ploy_rects(contours.size());vector<Point2f> ccs(contours.size());vector<float> radius(contours.size());vector<RotatedRect> minRects(contours.size());vector<RotatedRect> myellipse(contours.size());for (size_t i = 0; i < contours.size(); i++) {approxPolyDP(Mat(contours[i]), contours_ploy[i], 3, true);ploy_rects[i] = boundingRect(contours_ploy[i]);minEnclosingCircle(contours_ploy[i], ccs[i], radius[i]);if (contours_ploy[i].size() > 5) {myellipse[i] = fitEllipse(contours_ploy[i]);minRects[i] = minAreaRect(contours_ploy[i]);}}// draw itdrawImg = Mat::zeros(src.size(), src.type());Point2f pts[4];for (size_t t = 0; t < contours.size(); t++) {Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));//rectangle(drawImg, ploy_rects[t], color, 2, 8);//circle(drawImg, ccs[t], radius[t], color, 2, 8);if (contours_ploy[t].size() > 5) {ellipse(drawImg, myellipse[t], color, 1, 8);minRects[t].points(pts);for (int r = 0; r < 4; r++) {line(drawImg, pts[r], pts[(r + 1) % 4], color, 1, 8);}}}imshow(output_win, drawImg);return;
}

在这里插入图片描述

http://www.lryc.cn/news/323252.html

相关文章:

  • 校企合作,助力人才培养——黄冈师范学院-唯众 “实习实训基地”揭牌仪式顺利举行
  • npm audit fix --force
  • 递增四元组
  • 蓝桥杯每日一题——棋盘
  • QT6实现创建与操作sqlite数据库及读取实例(一)
  • 第十四届蓝桥杯JavaB组省赛真题 - 阶乘求和
  • Java毕业设计 基于springboot医院挂号系统 医院管理系统
  • 【MySQL】基本查询(1)
  • 一文讲清!进销存管理系统如何实现锁库及库存冻结?计算月加权平均成本?
  • 将本地项目上传至码云
  • 虚拟化技术
  • 鸿蒙一次开发,多端部署(一)简介
  • 数据结构——单向链表(C语言版)
  • ideaSSM 工厂效能管理系统bootstrap开发mysql数据库web结构java编程计算机网页源码maven项目
  • Java反射机制的讲解及其示例说明
  • 20240309web前端_第二周作业_完成游戏导航栏
  • 五、大模型-Prompt
  • 【网络安全】 MSF提权
  • iPHoP:病毒宿主预测
  • 网工内推 | 数通工程师,IE认证优先,五险一金,绩效奖
  • 2024 年 AI 辅助研发趋势将更加强调智能化、自动化和个性化
  • Jackson 2.x 系列【1】概述
  • 深入理解并优化Android中的文件描述符(FD)
  • 「JS 基础」异步解决方案入门
  • 408学习笔记-16-C-动态内存管理
  • vuex - 21年的笔记 - 后续更新
  • ngrok实现内网穿透
  • 开发chrome扩展( 禁止指定域名使用插件)
  • Flink:Lookup Join 实现与示例代码
  • python基础知识(四)