c++ - 合并 HoughLines

标签 c++ opencv houghlinesp

我在我的代码中卡住了一点。首先简要说明一下我在做什么:作为输入,有一张地板的图像。使用 Canny 和 HoughLinesP 算法,我想将整面墙分割成许多“小”部分,正如您在此处看到的,同时 是理想的输出(这里没有 canny),我想得到 - 一段在两条红线之间。

enter image description here

好吧,因为我实际上在这里

enter image description here

我想知道如何合并彼此太靠近的线。例如,第 2、4、3、5、6 行应该是一行,也算作一行。比第 7 到 15 行也应该是一个,这将是第二行。

当然,我做了一些研究并尝试了这个:

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <experimental/filesystem>

using namespace cv;
using namespace std;

Mat srcReal = //here's this image http://imgur.com/a/Kcjp6
Mat src, dst, cdst;
vector<Vec4i> lines;

void wallMapping(Mat src) {

  Scalar mu, sigma;
  meanStdDev(src, mu, sigma);

  Canny(src, dst, mu.val[0] - sigma.val[0], mu.val[0] + sigma.val[0], 3, false);

  cvtColor(dst, cdst, CV_GRAY2BGR);
  HoughLinesP(dst, lines, 1, CV_PI / 2, 50, 50, 200);

  sort(lines.begin(), lines.end(), vec4iSortByX());                                 ///sort all lines by number

  for (size_t i = 1; i < lines.size(); i++) {                                   
        Vec4i current = lines[i];                                                   ///set current lines
        Point pt1 = Point(current[0], current[1]);
        Point pt2 = Point(current[2], current[3]);

        Vec4i previous = lines[i - 1];                                              ///set previous lines
        Point ppt1 = Point(previous[0], previous[1]);
        Point ppt2 = Point(previous[2], previous[3]);

        int gradient1, gradient2;
        if (pt1.x - pt2.x != 0) {
            gradient1 = (pt1.y - pt2.y) / (pt1.x - pt2.x);
            gradient2 = (ppt1.y - ppt2.y) / (ppt1.x - ppt2.x);
        }
        Point avrgpt1, avrgpt2;

        if (gradient1 == gradient2) {

            avrgpt1.x = (pt1.x + ppt1.x) / 2;
            avrgpt2.x = (pt2.x + ppt2.x) / 2;

            avrgpt1.y = (pt1.y + ppt1.y) / 2;
            avrgpt2.y = (pt2.y + ppt2.y) / 2;
        }

        double angle = atan2(ppt2.y - ppt1.y, ppt2.x - ppt1.x) * 180.0 / CV_PI;     ///draw only vertical lines (90 degree)
        if (angle) {
            std::vector<int> lineLabel;
            int numLines = cv::partition(lines, lineLabel, isEqual);
            line(cdst, avrgpt1, avrgpt2, Scalar(0, 0, 255), 2, CV_AA);
        }

        cv::putText(cdst, to_string(i+1), pt2 + Point(0, 10), 4, 1, Scalar(0, 255, 0), 1, 8, false);

        //some other stuff
 }

//main
int main(int argc, char *argv[]) {
       wallMapping(srcReal);

       waitKey(0);
       return 0;
}

但是当你看上面的图片时,你会发现我在代码中的合并想法是行不通的。所以我很高兴我的代码有想法、方法或更正!谢谢!

最佳答案

在单独的二值图像中绘制线条,对它们进行形态学闭合,使彼此靠近的线条合并,然后用一些小内核腐 eclipse 图像,例如3x3 留下细线。

之后,从刚刚创建的二值图像中获取非零坐标,并将这些坐标绘制到所需图像中。

为了让这些新线回到 vector 中,您可以使用 findContours() 在二值图像中找到轮廓,然后只找到 2 个极值点(极值 x 或 y 值的点)每个轮廓的 - 这些是描述线的点。

关于c++ - 合并 HoughLines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45310604/

相关文章:

javascript - requestAnimFrame 被多次执行

c++ - 仅计算强度降低的边缘

python - 如何从图像中删除线条(由 HoughLines 检测到)?

c++ - PID Controller ,用于以速度行进精确的距离

c# - 外部应用程序使用共享内存接收消息但没有包含数据

C++ 悬空 const 对临时对象的引用

opencv - 了解霍夫变换

c++ - 计算省略号而不是字符串中的句点

c++ - 带有 OpenCV 的 Qt Creator 中的错误代码 0xc0000135

python - Python OpenCV HoughLinesP无法检测线