Linuxkiss
    首页 Linux C/C++ C++面试 Qt答疑 Qml中文手册 Qt CMake Python 工具
Linuxkiss
www.linuxkiss.com 你可以精通一门IT技术
  1. 首页
  2. openCv
  3. 正文

opencv系列30--轮廓周围绘制矩形框和圆形框 [ approxPolyDP,minEnclosingCircle,fitEllipse ]

2018年04月02日 14点热度 0人点赞

轮廓周围绘制矩形 -API

approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)

基于RDP算法实现,目的是减少多边形轮廓点数

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

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

轮廓周围绘制圆和椭圆-API

cv::minEnclosingCircle(InputArray points, //得到最小区域圆形

Point2f& center, // 圆心位置

float& radius// 圆的半径

)

cv::fitEllipse(InputArray points)得到最小椭圆

步骤

1:首先将图像变为二值图像。

2:发现轮廓,找到图像轮廓。

3:通过相关API在轮廓点上找到最小包含矩形和圆,旋转矩形与椭圆。

4:绘制它们。

代码实例

#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:/linuxkiss/images/leo.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, 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++) {
            //------api调用------
            approxPolyDP(Mat(contours[i]), contours_ploy[i], 3, true);
            ploy_rects[i] = boundingRect(contours_ploy[i]);
            //------api调用------
            minEnclosingCircle(contours_ploy[i], ccs[i], radius[i]);
            if (contours_ploy[i].size() > 5) {
                //------api调用------
                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
最后更新:2020年06月17日

Leo

保持饥渴的专注,追求最佳的品质

点赞
< 上一篇
下一篇 >
关注公众号

日历
2021年4月
一 二 三 四 五 六 日
« 2月    
 1234
567891011
12131415161718
19202122232425
2627282930  
最新 热点 随机
最新 热点 随机
windows中出现"无法解析的外部符号"到底是什么原因 Qt5中lambda表达式用法,非常实用 warning: class 'InterFace' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator 无法解析的外部符号 "public: static struct QMetaObject const Windows下Qt代码出现的错误总结 QT Creator如何在创建项目的时候,头文件和cpp文件的首字母默认大写
opencv系列23--直方图均衡化 [ api:equalizeHist ] C语言中如何在堆区开辟指针数组?【C语言实例】 8.1.1 权限管理–ACL权限简介与开启[笔记] opencv系列24--直方图计算 [ api:calcHist ] opencv系列32--点多边形测试 [ api:pointPolygonTest ] 装饰器模式_设计模式(Qt开发环境)
标签聚合
Linux qml中文文档 openCv C/C++面试 qmake qml中文手册 Qt C++

COPYRIGHT © 2015-2021 Linuxkiss. ALL RIGHTS RESERVED.

苏ICP备12059464号-2