opencv contours的问题

第一个问题:

问题:假如我有如下一张图,我要把边上两个小点去除,又要保留大轮廓内部的空洞,怎么办?


Fig 1.1

函数原型:

C++: void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
C++: void findContours(InputOutputArray image, OutputArrayOfArrays contours, int mode, int method, Point offset=Point())
这两个重载函数只有一个hierarchy的差别。说明如下:




测试:   用以下代码测试一下 contours 和 hierarchy 是怎么对轮廓进行标号的?选择mode = CV_RETR_CCOMP 双层结构。

#include <opencv2\opencv.hpp>
#include <time.h>
using namespace std;
using namespace cv;

//测试findContours,drawContours函数用法

bool verify(vector<Point> input, int min, int max);

int main()
{
	Mat src = imread("test.bmp", 1);
	Mat dst = Mat::zeros(src.size(), src.type());

	//cout<<src.channels();  //画图软件生成3通道图
	Mat gray;
	cvtColor(src, gray, CV_BGR2GRAY);
	Mat thre;
	threshold(gray, thre, 127, 255, CV_THRESH_BINARY );
	//thre = gray > 1;  //这种写法可以替代上面那一句
	imshow("thre", thre);
	imwrite("thre.bmp", thre);
	
	vector< vector<Point> > contours;
	vector<Vec4i> hierarchy;
	
	findContours(thre, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
	
	//计算每个轮廓的面积
	float temp = 0;
	for(int i=0; i<contours.size(); ++i) {
		temp = fabs(contourArea(contours[i]));
		cout<<"i="<<i<<"  area="<<temp<<endl;
	}

	//我要把单独的小轮廓去掉,而保留大轮廓内部的孔洞
	int idx = 0;
	for( ; idx >=0; idx = hierarchy[idx][0])
	{
		if(verify(contours[idx], 200, 90000)) {
			Scalar color( rand()&255, rand()&255, rand()&255);
			drawContours(dst
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值