C++ 对于二值图像的连接的处理函数应该怎么写

超级小胖 发布于 2013/05/19 03:30
阅读 260
收藏 0

我已经将一个图像进行了二值化处理,现在想将图像中连续的图块分离出来。

我通过图像获取了所有图块的点,存在了一个vector<Point> points,的数组中,其中Point是一个自己写的类,里面存的就是点的坐标,x和y
我现在想通过一个算法,实现将points中每一个相连的图块的坐标提取到一个二维数组vector< vetcor<Point> > tagPoints中。
也就是说。二维数组中存放的就是将points分块后的坐标组
希望有大神能过帮帮忙。

void EdgeCompute::setCount()
{
    do 
    {
        compute();
    } while (!points.empty());
}

void EdgeCompute::compute()
{
    //获取points中的第一个元素,并从points中删除
    std::vector<Point>::iterator itr;
    Point head = points[0];
    itr = points.begin();
    itr = points.erase(itr);
    std::vector<Point> tmpPoints;
    tmpPoints.push_back(head);
    execute(&head, tmpPoints);
    tagPoints.push_back(tmpPoints);
    
}

inline void EdgeCompute::execute(Point* head, std::vector<Point> &tmpPoints)
{
    std::vector<Point>::iterator itr;
    
    Point* up = head->up();
    itr = std::find(points.begin(), points.end(), *up);
    if (itr != points.end())
    {
        tmpPoints.push_back(*itr);
        itr = points.erase(itr);
        execute(up, tmpPoints);
    }

    Point* down = head->down();
    itr = std::find(points.begin(), points.end(), *down);
    if (itr != points.end())
    {
        tmpPoints.push_back(*itr);
        itr = points.erase(itr);
        execute(down, tmpPoints);
    }

    Point* left = head->left();
    itr = std::find(points.begin(), points.end(), *left);
    if (itr != points.end())
    {
        tmpPoints.push_back(*itr);
        itr = points.erase(itr);
        execute(left, tmpPoints);
    }

    Point* right = head->right();
    itr = std::find(points.begin(), points.end(), *right);
    if (itr != points.end())
    {
        tmpPoints.push_back(*itr);
        itr = points.erase(itr);
        execute(right, tmpPoints);
    }

    Point* upAndRight = head->upAndRight();
    itr = std::find(points.begin(), points.end(), *upAndRight);
    if (itr != points.end())
    {
        tmpPoints.push_back(*itr);
        itr = points.erase(itr);
        execute(upAndRight, tmpPoints);
    }

    Point* upAndLeft = head->upAndLeft();
    itr = std::find(points.begin(), points.end(), *upAndLeft);
    if (itr != points.end())
    {
        tmpPoints.push_back(*itr);
        itr = points.erase(itr);
        execute(upAndLeft, tmpPoints);
    }

    Point* downAndRight = head->downAndRight();
    itr = std::find(points.begin(), points.end(), *downAndRight);
    if (itr != points.end())
    {
        tmpPoints.push_back(*itr);
        itr = points.erase(itr);
        execute(upAndRight, tmpPoints);
    }

    Point* downAndLeft = head->downAndLeft();
    itr = std::find(points.begin(), points.end(), *downAndLeft);
    if (itr != points.end())
    {
        tmpPoints.push_back(*itr);
        itr = points.erase(itr);
        execute(downAndLeft, tmpPoints);
    }

}
这是我自己写的代码。有个问题。就是当点过多的时候,会产生内存栈空间不够的现象。希望有大神能够帮助解决。万分感谢!!!!
加载中
0
Finlay
Finlay

这种情况,我感觉是数组内存太大造成的,可以考虑对图片分块处理。

返回顶部
顶部