enum SortCriterion
{
FIRST_POINT, //区域第一行的最左侧的点
LAST_POINT, //区域最后一行的最右侧的点
UPPER_LEFT, //区域周围矩形的左上角
UPPER_RIGHT, //区域周围矩形的右上角
LOWER_LEFT, //区域周围矩形的左下角
LOWER_RIGHT //区域周围矩形的右下角
};
enum SortDirection
{
ROW, //区域按行排列,即从左到右,从上到下
COLUMN //区域按列排列,即从上到下,从左到右
};
//功能:由区域的相对位置对区域进行排序
//参数:
// src:输入图像
// contours:输入图像中的轮廓组
// pos:已排序的轮廓索引
// sc:排序基准点
// isDue:
// true:从小到大进行排序
// false:从大到小进行排序
// sd:排序方向
//返回值:true:排序成功
// false:排序失败
bool sort_region(Mat src,vector<vector<Point>> contours, vector<int> &pos,
SortCriterion sc, bool isDue, SortDirection sd)
{
int count = contours.size();
pos.resize(count);
vector<Point> points;
for (int i = 0; i < count; i++)
{
pos[i] = i;
Rect rect = boundingRect(contours[i]);
if (sc == FIRST_POINT)
{
int row = rect.y;
for (int col = rect.x; col <= rect.x + rect.width; col++)
{
if (src.at<uchar>(row, col) > 0)
{
points.push_back(Point(col, row));
break;
}
}
}
else if (sc == LAST_POINT)
{
int row = rect.y + rect.height;
for (int col = rect.x + rect.width; col >= rect.x; col--)
{
if (src.at<uchar>(row, col) > 0)
{
points.push_back(Point(col, row));
break;
}
}
}
else if (sc == UPPER_LEFT)
points.push_back(rect.tl());
else if (sc == UPPER_RIGHT)
points.push_back(Point(rect.x + rect.width, rect.y));
else if (sc == LOWER_LEFT)
points.push_back(Point(rect.x, rect.y + rect.height));
else if (sc == LOWER_RIGHT)
points.push_back(rect.br());
}
int np = points.size();
if (np != count)
return false;
if (sd == ROW)
{
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - 1 - i; j++)
{
if (isDue)
{
if (points[j].y > points[j + 1].y)
{
Point temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
int index = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = index;
}
}
else
{
if (points[j].y < points[j + 1].y)
{
Point temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
int index = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = index;
}
}
}
}
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - 1 - i; j++)
{
if (points[j].y == points[j + 1].y)
{
if (isDue)
{
if (points[j].x > points[j + 1].x)
{
Point temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
int index = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = index;
}
}
else
{
if (points[j].x < points[j + 1].x)
{
Point temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
int index = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = index;
}
}
}
}
}
}
else if (sd == COLUMN)
{
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - 1 - i; j++)
{
if (isDue)
{
if (points[j].x > points[j + 1].x)
{
Point temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
int index = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = index;
}
}
else
{
if (points[j].x < points[j + 1].x)
{
Point temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
int index = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = index;
}
}
}
}
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - 1 - i; j++)
{
if (points[j].x == points[j + 1].x)
{
if (isDue)
{
if (points[j].y > points[j + 1].y)
{
Point temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
int index = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = index;
}
}
else
{
if (points[j].y < points[j + 1].y)
{
Point temp = points[j];
points[j] = points[j + 1];
points[j + 1] = temp;
int index = pos[j];
pos[j] = pos[j + 1];
pos[j + 1] = index;
}
}
}
}
}
}
return true;
}
OpenCV实现halcon的sort_region函数
最新推荐文章于 2024-06-23 17:05:46 发布