轮廓周围绘制介绍 没什么概念,就是给得出来的轮廓绘制周围图形,例如下图给左侧得出的轮廓去绘图得到右侧图像: 相关api 减少多边形轮廓点数:approxPolyDP 函数作用:基于
没什么概念,就是给得出来的轮廓绘制周围图形,例如下图给左侧得出的轮廓去绘图得到右侧图像:
函数作用:基于RDP算法实现,目的是减少多边形轮廓点数
函数原型:
//减少多边形轮廓点数
approxPolyDP(
InputArray curve, // 一般是由图像的轮廓点组成的点集 Mat(vector)
OutputArray approxCurve, // 表示输出的多边形点集
double epsilon, // 主要表示输出的精度,就是两个轮廓点之间最大距离数,5,6,7,,8,,,,
bool closed // 表示输出的多边形是否封闭
)
RDP算法介绍:
cv::boundingRect(InputArray points) 得到轮廓周围最小矩形左上交点坐标和右下角点坐标,绘制一个矩形
cv::minAreaRect(InputArray points) 得到一个旋转的矩形,返回旋转矩形
// 得到轮廓周围最小椭圆
cv::minEnclosingCircle(
InputArray points, // 得到最小区域圆形
Point2f& center, // 圆心位置 输出参数
float& radius // 圆的半径 输出参数
)
// 得到轮廓周围最小椭圆
cv::fitEllipse(InputArray points)
#include <iOStream>
#include <math.h>
#include <OpenCV2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui_c.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("./test2.jpg");
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, CV_WINDOW_AUTOSIZE);
namedWindow(output_win, CV_WINDOW_AUTOSIZE);
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 it
drawImg = 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;
到此这篇关于OpenCV 轮廓周围绘制矩形框和圆形框的文章就介绍到这了,更多相关OpenCV 绘制矩形框和圆形框内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: OpenCV轮廓周围绘制矩形框和圆形框的方法
本文链接: https://www.lsjlt.com/news/163988.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0