opencv 学习之 轮廓操作笔记(一)

本文介绍了如何使用OpenCV将轮廓的外部区域像素置为0,并通过最小外接矩形进行仿射变换,转换成无偏转的矩形。虽然外接矩形的顶点按顺时针顺序排列,但起点不固定,因此在匹配轮廓时,不能像常规模板匹配那样确定最佳位置。作者通过遍历轮廓列表,找到了最匹配的轮廓索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、轮廓之外的区域像素置0

根据最小外接矩形,仿射变换至无偏转矩形。外接矩形结构体导出顶点是按顺时针结构排列的,但是起点不定。

int SetExcludeRectZero(Mat & gray, RotatedRect rect)
{
	Point2f vertex1[4], vertex2[4];      //定义外接矩形的4个顶点  
	//定点的排序为逆时针
	rect.points(vertex1);
	rect.points(vertex2);
	int width = rect.size.width;
	int height = rect.size.height;
	Point2f certer = rect.center;
	//修正vertex2,使其无倾斜
	for (int k = 0; k < 4; k++)
	{
		int index1 = k;
		int index2 = (k + 1) % 4;
		if (vertex2[index1].x < rect.center.x && vertex2[index2].x < rect.center.x)
		{		
			//坐标逆时针排序
			vertex2[index1].x = certer.x - width / 2;
			vertex2[index1].y = certer.y + height / 2;
			vertex2[index2].x = certer.x - width / 2;
			vertex2[index2].y = certer.y - height / 2;
			index1 = (k + 2) % 4;
			index2 = (k + 3) % 4;
			vertex2[index1].x = certer.x + width / 2;
			vertex2[index1].y = certer.y - height / 2;
			vertex2[index2].x = certer.x + width / 2;
			vertex2[index2].y = certer.y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值