矩形排序。。。。





bool compRECx(cv::Rect &a, cv::Rect &b)
{
	return a.x < b.x;
}
bool compRECy(cv::Rect &a, cv::Rect &b)
{
	return a.y < b.y;
}

void HTChipDetector::resortrecAndloop(std::vector<cv::Rect> &rec, std::vector<cv::Point> &loop, int maxHj, int maxHj1, int flag)
{
	if (rec.size() > 1)
	{
		if (flag == 1)//先y再x
		{
			std::sort(rec.begin(), rec.end(), compRECy);
			int x = 1;
			int y = 1;
			std::vector<cv::Rect> rectemp, recres;
			std::vector<std::vector<cv::Rect>> rectempV;
			loop.push_back(cv::Point(x, y));
			rectemp.push_back(rec[0]);
			int firsty = rec[0].y;
			for (int i = 1; i < rec.size(); i++)
			{
				if (rec[i].y - firsty > maxHj)//间距过大,属于下一行
				{
					std::sort(rectemp.begin(), rectemp.end(), compRECx);//x排序后保存
					rectempV.push_back(rectemp);
					rectemp.clear();
					x++;
					y = 0;
					firsty = rec[i].y;
				}
				rectemp.push_back(rec[i]);
				y++;
				loop.push_back(cv::Point(x, y));
			}
			std::sort(rectemp.begin(), rectemp.end(), compRECx);//x排序后保存
			rectempV.push_back(rectemp);
			for (int i = 0; i < rectempV.size(); i++)
			{
				if (rectempV[i].size() > 0)
				{
					recres.insert(recres.end(), rectempV[i].begin(), rectempV[i].end());
				}
			}
			rec = recres;

		}
		else
		{
			std::sort(rec.begin(), rec.end(), compRECx);
			int x = 1;
			int y = 1;
			std::vector<cv::Rect> rectemp, recres;
			std::vector<std::vector<cv::Rect>> rectempV;
			loop.push_back(cv::Point(x, y));
			rectemp.push_back(rec[0]);
			int firstx = rec[0].x;
			for (int i = 1; i < rec.size(); i++)
			{
				if (rec[i].x - firstx > maxHj1)//间距过大,属于下一行
				{
					std::sort(rectemp.begin(), rectemp.end(), compRECy);//x排序后保存
					rectempV.push_back(rectemp);
					rectemp.clear();
					y++;
					x = 0;
					firstx = rec[i].x;
				}
				rectemp.push_back(rec[i]);
				x++;
				loop.push_back(cv::Point(x, y));
			}
			std::sort(rectemp.begin(), rectemp.end(), compRECy);//x排序后保存
			rectempV.push_back(rectemp);
			for (int i = 0; i < rectempV.size(); i++)
			{
				if (rectempV[i].size() > 0)
				{
					recres.insert(recres.end(), rectempV[i].begin(), rectempV[i].end());
				}
			}
			rec = recres;
		}

	}

	return;
}

使用:

	resortrecAndloop(charRect, charPoint, meanHeight, meanWidth, 1);






struct MyRect {
	cv::Rect rect;
	int index;
};

bool compRECx(MyRect& a, MyRect& b)
{
	return a.rect.x < b.rect.x;
}
bool compRECy(MyRect& a, MyRect& b)
{
	return a.rect.y < b.rect.y;
}

void CbaseMFCprojectDlg::resortrecAndloop(std::vector<MyRect>& rec, std::vector<cv::Point>& loop, int maxHj, int maxHj1, int flag)
{
	if (rec.size() > 1)
	{
		if (flag == 1)//先y再x
		{
			std::sort(rec.begin(), rec.end(), compRECy);
			int x = 1;
			int y = 1;
			std::vector<MyRect> rectemp, recres;
			std::vector<std::vector<MyRect>> rectempV;
			loop.push_back(cv::Point(x, y));
			rectemp.push_back(rec[0]);
			int firsty = rec[0].rect.y;
			for (int i = 1; i < rec.size(); i++)
			{
				if (rec[i].rect.y - firsty > maxHj)//间距过大,属于下一行
				{
					std::sort(rectemp.begin(), rectemp.end(), compRECx);//x排序后保存
					rectempV.push_back(rectemp);
					rectemp.clear();
					x++;
					y = 0;
					firsty = rec[i].rect.y;
				}
				rectemp.push_back(rec[i]);
				y++;
				loop.push_back(cv::Point(x, y));
			}
			std::sort(rectemp.begin(), rectemp.end(), compRECx);//x排序后保存
			rectempV.push_back(rectemp);
			for (int i = 0; i < rectempV.size(); i++)
			{
				if (rectempV[i].size() > 0)
				{
					recres.insert(recres.end(), rectempV[i].begin(), rectempV[i].end());
				}
			}
			rec = recres;

		}
		else
		{
			std::sort(rec.begin(), rec.end(), compRECx);
			int x = 1;
			int y = 1;
			std::vector<MyRect> rectemp, recres;
			std::vector<std::vector<MyRect>> rectempV;
			loop.push_back(cv::Point(x, y));
			rectemp.push_back(rec[0]);
			int firstx = rec[0].rect.x;
			for (int i = 1; i < rec.size(); i++)
			{
				if (rec[i].rect.x - firstx > maxHj1)//间距过大,属于下一行
				{
					std::sort(rectemp.begin(), rectemp.end(), compRECy);//x排序后保存
					rectempV.push_back(rectemp);
					rectemp.clear();
					y++;
					x = 0;
					firstx = rec[i].rect.x;
				}
				rectemp.push_back(rec[i]);
				x++;
				loop.push_back(cv::Point(x, y));
			}
			std::sort(rectemp.begin(), rectemp.end(), compRECy);//x排序后保存
			rectempV.push_back(rectemp);
			for (int i = 0; i < rectempV.size(); i++)
			{
				if (rectempV[i].size() > 0)
				{
					recres.insert(recres.end(), rectempV[i].begin(), rectempV[i].end());
				}
			}
			rec = recres;
		}

	}

	return;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值