如何在二值化骨架图像中跟踪线段?

3
我有一张二值化和骨架化的图像。我使用了Zhang-Suen算法进行骨架化处理。现在我需要以2点格式(线段起始点和终止点)从图像中获取线段。 到目前为止,我一直使用OpenCV函数findContours,使用CV_CHAIN_APPROX_SIMPLE和CV_RETR_LIST选项。但是,出现了三个问题:
  1. 这种方法会返回重复的线段(方向相反)。
  2. 由于"层次结构功能",连接的结构有时会断开。
  3. 在线的交汇处周围出现混乱的结果。
是否有另一种方法可以从图像中跟踪线段?
需要跟踪的图像的放大部分:Magnified section of the image I need to trace

你需要每条8连通线的起点和终点吗?那么对于具有多条路径(例如最低路径)的轮廓,该怎么办? - Miki
是的,它们有什么问题吗?我只需要所有直线和对角线线段的坐标。 - Leprechaun
那么,以顶部的那行为例。对于你来说,它是由一行还是两行组成的?也就是说,你所说的“直线”是指具有相同y值的线吗? - Miki
顶部的第一行?共分为三个线段。两个水平线和一个斜线(长度仅为两个像素)。 - Leprechaun
好的,我明白了。如果您发布原始图像,我会尝试翻译它。 - Miki
好的,先生。http://imgur.com/BOvU4t8 - Leprechaun
1个回答

1

那应该可以工作。它对图像进行了4次扫描(您可能可以减少扫描次数,但逻辑会更复杂)。

在每次扫描中,它跟踪水平线、垂直线、向下对角线和向上对角线。

线条存储在vector<Vec4i>中,其中每个Vec4i都是一条带有Xstart,Ystart,Xend,Yend的线条;

如果这对您有用,请告诉我。

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);

    Mat1b w;
    // Add bg border
    copyMakeBorder(img, w, 1, 1, 1, 1, BORDER_CONSTANT, Scalar(0));

    vector<Vec4i> lines;
    Vec4i line;

    // Scan horizontal lines 
    for (int y = 1; y < w.rows - 1; ++y)
    {
        for (int x = 1; x < w.cols - 1; ++x)
        {
            if (w(y, x) == 255)
            {
                int yy = y;
                int xx = x;

                //Save first point
                line[0] = xx - 1;
                line[1] = yy - 1;

                while (true)
                {
                    if (w(yy, xx + 1))
                    {
                        // Mark as detected
                        w(yy, xx) = 1;
                        ++xx;
                    }
                    else
                    {
                        // End of the line
                        line[2] = xx - 1;
                        line[3] = yy - 1;

                        if (line[2] - line[0] > 0)
                        {
                            lines.push_back(line);
                        }
                        break;
                    }
                }
            }
        }
    }

    // Scan vertical lines 
    for (int y = 1; y < w.rows - 1; ++y)
    {
        for (int x = 1; x < w.cols - 1; ++x)
        {
            if (w(y, x) == 255)
            {
                int yy = y;
                int xx = x;

                //Save first point
                line[0] = xx - 1;
                line[1] = yy - 1;

                while (true)
                {
                    if (w(yy + 1, xx))
                    {
                        // Mark as detected
                        w(yy, xx) = 1;
                        ++yy;
                    }
                    else
                    {
                        // End of the line
                        line[2] = xx - 1;
                        line[3] = yy - 1;

                        if (line[3] - line[1] > 0)
                        {
                            lines.push_back(line);
                        }
                        break;
                    }
                }
            }
        }
    }

    // Scan for diagonal low lines 
    for (int y = 1; y < w.rows - 1; ++y)
    {
        for (int x = 1; x < w.cols - 1; ++x)
        {
            if (w(y, x) == 255)
            {
                int yy = y;
                int xx = x;

                //Save first point
                line[0] = xx - 1;
                line[1] = yy - 1;

                while (true)
                {
                    if (w(yy + 1, xx + 1))
                    {
                        // Mark as detected
                        w(yy, xx) = 1;
                        ++xx;
                        ++yy;
                    }
                    else
                    {
                        // End of the line
                        line[2] = xx - 1;
                        line[3] = yy - 1;
                        if (line[2] - line[0] > 0)
                        {
                            lines.push_back(line);
                        }
                        break;
                    }
                }
            }
        }
    }

    // Scan for diagonal high lines 
    for (int y = 1; y < w.rows - 1; ++y)
    {
        for (int x = 1; x < w.cols - 1; ++x)
        {
            if (w(y, x) == 255)
            {
                int yy = y;
                int xx = x;

                //Save first point
                line[0] = xx - 1;
                line[1] = yy - 1;

                while (true)
                {
                    if (w(yy - 1, xx + 1))
                    {
                        // Mark as detected
                        w(yy, xx) = 1;
                        ++xx;
                        --yy;
                    }
                    else
                    {
                        // End of the line
                        line[2] = xx - 1;
                        line[3] = yy - 1;
                        if (line[2] - line[0] > 0)
                        {
                            lines.push_back(line);
                        }
                        break;
                    }
                }
            }
        }
    }

    RNG rng(12345);
    Mat3b res;
    cvtColor(img, res, COLOR_GRAY2BGR);

    for (int i = 0; i < lines.size(); ++i)
    {
        const Vec4i& lin = lines[i];
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        cv::line(res, Point(lin[0], lin[1]), Point(lin[2], lin[3]), color);
    }

    imshow("res", res);
    waitKey();

    return 0;
}

从这张图片开始:

enter image description here

用随机颜色绘制每条检测到的线(从起点到终点)如下:

enter image description here

缩放:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接