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)
{
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);
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);
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);
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);
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)
{
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);
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);
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);
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);
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;
}