`
king_c
  • 浏览: 213947 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

opencv轮廓提取与轮廓拟合

阅读更多
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main()
{
	// Read input binary image
	Mat image= imread("./binaryGroup.bmp",0);
	if (!image.data)
		return 0; 

	namedWindow("Binary Image");
	imshow("Binary Image",image);

	// Get the contours of the connected components
	vector<vector<Point>> contours;
	//findContours的输入是二值图像
	findContours(image, 
		contours, // a vector of contours 
		CV_RETR_EXTERNAL, // retrieve the external contours
		CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours

	// Print contours' length轮廓的个数
	cout << "Contours: " << contours.size() << endl;
	vector<vector<Point>>::const_iterator itContours= contours.begin();
	for ( ; itContours!=contours.end(); ++itContours) {

		cout << "Size: " << itContours->size() << endl;//每个轮廓包含的点数
	}

	// draw black contours on white image
	Mat result(image.size(),CV_8U,Scalar(0));
	drawContours(result,contours,      //画出轮廓
		-1, // draw all contours
		Scalar(255), // in black
		2); // with a thickness of 2

	namedWindow("Contours");
	imshow("Contours",result);

	// Eliminate too short or too long contours
	int cmin= 100;  // minimum contour length
	int cmax= 1000; // maximum contour length
	vector<vector<Point>>::const_iterator itc= contours.begin();
	while (itc!=contours.end()) {

		if (itc->size() < cmin || itc->size() > cmax)
			itc= contours.erase(itc);
		else 
			++itc;
	}

	// draw contours on the original image
	Mat original= imread("./group.bmp");
	drawContours(original,contours,
		-1, // draw all contours
		Scalar(255,255,255), // in white
		2); // with a thickness of 2

	namedWindow("Contours on Animals");
	imshow("Contours on Animals",original);

	// Let's now draw black contours on white image
	//result.setTo(Scalar(0));
	//If the third parameter of this function is a negative value, then all contours are drawn.
	//Otherwise, it is possible to specify the index of the contour to be drawn
	drawContours(result,contours,
		-1, // draw all contours
		Scalar(255), // in black
		1); // with a thickness of 1
	image= imread("./binaryGroup.bmp",1);

	// testing the bounding box 
	Rect r0= boundingRect(Mat(contours[0]));//boundingRect获取这个外接矩形
	rectangle(result,r0,Scalar(255,255,255),2);

	// testing the enclosing circle 
	float radius;
	Point2f center;
	minEnclosingCircle(Mat(contours[1]),center,radius);//对轮廓进行多变形逼近
	circle(result,Point(center),static_cast<int>(radius),Scalar(255),2);

	RotatedRect rrect= fitEllipse(Mat(contours[2]));
	ellipse(result,rrect,Scalar(255),2);

	//testing the approximate polygon
	vector<Point> poly;
	approxPolyDP(Mat(contours[2]),poly,5,true);

	cout << "Polygon size: " << poly.size() << endl;

	//// Iterate over each segment and draw it
	vector<Point>::const_iterator itp= poly.begin();
	while (itp!=(poly.end()-1)) {
		line(result,*itp,*(itp+1),Scalar(255),2);
		++itp;
	}
	// last point linked to first point
	line(result,*(poly.begin()),*(poly.end()-1),Scalar(20),2);

	// testing the convex hull
	vector<Point> hull;
	convexHull(Mat(contours[3]),hull);

	// Iterate over each segment and draw it
	vector<Point>::const_iterator it= hull.begin();
	while (it!=(hull.end()-1)) {
		line(result,*it,*(it+1),Scalar(255),2);
		++it;
	}
	// last point linked to first point
	line(result,*(hull.begin()),*(hull.end()-1),Scalar(255),2);

	// testing the moments

	//iterate over all contours
	itc= contours.begin();
	while (itc!=contours.end()) {

		// compute all moments
		Moments mom= moments(Mat(*itc++));

		// draw mass center
		circle(result,
			// position of mass center converted to integer
			Point(mom.m10/mom.m00,mom.m01/mom.m00),
			2,Scalar(255),2); // draw black dot
	}

	namedWindow("Some Shape descriptors");
	imshow("Some Shape descriptors",result);

	// New call to findContours but with CV_RETR_LIST flag
	image= imread("./binaryGroup.bmp",0);

	// Get the contours of the connected components
	findContours(image, 
		contours, // a vector of contours 
		CV_RETR_LIST, // retrieve the external and internal contours
		CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours

	// draw black contours on white image
	result.setTo(Scalar(0));
	drawContours(result,contours,
		-1, // draw all contours
		Scalar(255), // in black
		2); // with a thickness of 2
	namedWindow("All Contours");
	imshow("All Contours",result);

	waitKey();
	return 0;
}

 一、对于相对路径的读取

./ 表示当前目录下,即cpp所在目录下。

../表示上一目录

二、  group.bmp                  binaryGroup.bmp

 groupbinaryGroup

分享到:
评论

相关推荐

    fitContours_opencv_图像轮廓提取_图像轮廓拟合_

    对二值图像进行轮廓提取,并针对每个轮廓进行拟合

    opencv拟合指定大小的椭圆及测试

    用opencv231+vs2008编写的一个拟合椭圆的程序,输入 是二值图,背景是黑色的,还有一个输入是轮廓的面积,能够剔除不需要要轮廓。代码中能测试选定的待拟合的轮廓(已注释),并把轮廓参数输出并测试。

    opencv椭圆拟合长短轴比

    用椭圆拟合轮廓,并求长短轴比,主要应用于模式识别中的特征提取

    opencv3/C++轮廓的提取与筛选方式

    轮廓提取 findContours发现轮廓 findContours( InputOutputArray binImg, //输入8bit图像,0值像素值不变,非0的像素看成1;(变为二值图像) OutputArrayOfArrays contours,//输出找到的轮廓对象 OutputArray, ...

    OpenCV实现轮廓的发现

     当我们通过阈值分割提取到图像中的目标物体后,我们就需要通过边缘检测来提取目标物体的轮廓,使用这两种方法基本能够确定物体的边缘或者前景。接下来,我们通常需要做的是拟合这些边缘的前景,如拟合出包含前景...

    基于椭圆拟合的瞳孔中心精确定位算法研究_余罗.pdf

    对得到的系列轮廓随机选择6个点进行椭圆拟合,计算椭圆中心与边缘距离的方差,方差最小的为瞳孔中心。算法具有对存在白斑干扰及半闭眼状态时的瞳孔中心准确识别的优点。实验表明,该算法能够准确地定位瞳孔中心,且满足...

    opencv的全部基础操作,一共109个实例全部都在anaconda3,python3.7,opencv4调试通过。

    code_056 | [直线拟合与极值点寻找](python/code_056/opencv_056.py) | ✔️ code_057 | [点多边形测试](python/code_057/opencv_057.py) | ✔️ code_058 | [寻找最大内接圆](python/code_058/opencv_058.py) | ✔...

    图像轮廓绘制

    关于如何提取并绘制轮廓的代码,其中包括如何获取轮廓的最大面积,平均面积

    利用python,图像中矩形框识别替换,预处理,边缘检测,投影变换,opencv

    针对难点一: 拟采用边缘检测检测像素突变点提取边缘线条+轮廓提取+拟合外接四边形寻找轮廓中满足面积条件的四边形。考虑到拍摄图像有很多噪点干扰,采用中值滤波进行平滑处理,过滤椒盐噪声。设定矩形区域面积阈值...

    使用OpenCV检测图像中的矩形

    (3) 提取轮廓。 (4)使用图像轮廓点进行多边形拟合。 (5)计算轮廓面积并得到矩形4个顶点。 (6)求轮廓边缘之间角度的最大余弦。 (7)画出矩形。 2.代码 //检测矩形 //第一个参数是传入的原始图像,第二是...

    EmguCV,C#版本Opencv图像识别、处理

    EmguCV,C#版本Opencv图像识别、处理。软件版本:VS2017及其以上。 1、图像处理 (1)颜色处理 (2)图像差 (3)图像拼接 (4)直方图 (5)颜色空间/通道提取 2、预处理 (1)均衡化 (2)阈值处理 (3)滤波 (4)...

    基于数字图片处理的数控G代码生成源程序

    对网络上的字体图片进行处理,提取轮廓以后设定加工尺寸,然后生成G代码,可惜现在只能进行直线拟合,生成基于直线的G代码,不过精度还可以,注:由于在图片处理的过程中用到了openCV库,所以在编译生成之前要配置...

    mineral_identification

    利用多边形拟合轮廓 若有6个角点(是“L”轮廓) 计算轮廓面积,并将轮廓加入select2 若有4个角点(是“口”轮廓) 将轮廓加入select2 判断面类型 若select2中有两个目标(抓取矿石情形,只能看到两个锚点,另外两个...

    最小二乘法求圆心和半径

    该方法采用提取图像轮廓,并进行最小二乘法计算拟合出圆。最后得出圆心和半径。

    智能视频监控中目标检测与识别

    3.2.3 基于AKGMM的背景提取与更新算法 3.2.4 去除阴影 3.3 ROI面积缩减车辆检测搜索算法 3.3.1 改进的帧差法 3.3.2 图像的腐蚀与膨胀 3.3.3 车辆目标分割识别 3.3.4 实验结果与分析 参考文献 第4章 运动目标跟踪技术...

    基于机器视觉的浮选气泡体积和表面积测量研究

    针对重叠气泡,使用曲率尺度空间角点检测算法和方向链码标记凹点,以此分割重叠的轮廓,最小二乘拟合重建独立气泡的边缘。根据边缘计算气泡的偏转角度,自适应选取分割间隔。通过各分割部分的计算,累加得到气泡的...

Global site tag (gtag.js) - Google Analytics