OpenCV实现halcon的sort_region函数


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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值