Computer vision 将线的图像转换为坐标集

Computer vision 将线的图像转换为坐标集,computer-vision,polygon,computational-geometry,edge-detection,Computer Vision,Polygon,Computational Geometry,Edge Detection,假设我有一个平面图,如上图所示。是否有一种简单的处理方法将该图像转换为一组由坐标[(x1,y1),(x2,y2)],[(x2,y2),(x3,y3)],…,表示的直线,[(xn-1,yn-1),(xn,yn)]我想你想找到图像中所有线条的坐标。在c++中,通过以下简单步骤很容易找到坐标: 阈值二进制反转 适用于减少厚度 在二值图像中查找非零像素 查找轮廓并应用近似多边形来近似点 这是一张代表你需要处理的图片,还是一张解释性的草图?这可能会有很大的不同。这是计算机视觉任务,在简历1中没有简单的事


假设我有一个平面图,如上图所示。是否有一种简单的处理方法将该图像转换为一组由坐标[(x1,y1),(x2,y2)],[(x2,y2),(x3,y3)],…,表示的直线,[(xn-1,yn-1),(xn,yn)]

我想你想找到图像中所有线条的坐标。在c++中,通过以下简单步骤很容易找到坐标:

  • 阈值二进制反转
  • 适用于减少厚度
  • 在二值图像中查找非零像素
  • 查找轮廓并应用近似多边形来近似点


  • 这是一张代表你需要处理的图片,还是一张解释性的草图?这可能会有很大的不同。这是计算机视觉任务,在简历1中没有简单的事情。你需要提供真实的输入图像,以便我们了解你真正在处理什么。2.我会首先通过一些树来对图像进行二值化(但如果真实图像的类型/噪声/照明不同,则可能不是这样)3.通过hough变换或通过检查/回归将直线连接在一起的发现像素来检测直线。此过程也称为矢量化,因此您知道要查找什么。如果直线是轴/角度对齐的,或已知宽度恒定,则可以显著简化此过程。您解决问题了吗?你还有什么问题吗?那只是返回他需要的向量形式的多边形的图像。。。另外,你忘了指定你的代码是用什么语言编写的(或者我错过了),谢谢你指出我的误解@Spektre。我想现在一切都有道理了!
    void thinningIteration(Mat& im, int iter)
    {
        Mat marker = Mat::zeros(im.size(), CV_8UC1);
        for (int i = 1; i < im.rows-1; i++)
        {
            for (int j = 1; j < im.cols-1; j++)
            {
                uchar p2 = im.at<uchar>(i-1, j);
                uchar p3 = im.at<uchar>(i-1, j+1);
                uchar p4 = im.at<uchar>(i, j+1);
                uchar p5 = im.at<uchar>(i+1, j+1);
                uchar p6 = im.at<uchar>(i+1, j);
                uchar p7 = im.at<uchar>(i+1, j-1);
                uchar p8 = im.at<uchar>(i, j-1);
                uchar p9 = im.at<uchar>(i-1, j-1);
    
                int A  = (p2 == 0 && p3 == 1) + (p3 == 0 && p4 == 1) + 
                         (p4 == 0 && p5 == 1) + (p5 == 0 && p6 == 1) + 
                         (p6 == 0 && p7 == 1) + (p7 == 0 && p8 == 1) +
                         (p8 == 0 && p9 == 1) + (p9 == 0 && p2 == 1);
                int B  = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;
                int m1 = iter == 0 ? (p2 * p4 * p6) : (p2 * p4 * p8);
                int m2 = iter == 0 ? (p4 * p6 * p8) : (p2 * p6 * p8);
    
                if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0)
                    marker.at<uchar>(i,j) = 1;
            }
        }
        im &= ~marker;
    }
    
    void thinning(Mat& im)
    {
        im /= 255;
        Mat prev = Mat::zeros(im.size(), CV_8UC1);
        Mat diff;
        do 
        {
            thinningIteration(im, 0);
            thinningIteration(im, 1);
            absdiff(im, prev, diff);
            im.copyTo(prev);
        } 
        while (countNonZero(diff) > 0);
    
        im *= 255;
    }
    
    void main()
    {
        Mat mSource_Bgr,mSource_Gray,mThreshold,mThinning;
        mSource_Bgr= imread(FileName_S.c_str(),IMREAD_COLOR);
        mSource_Gray= imread(FileName_S.c_str(),0);
    
        threshold(mSource_Gray,mThreshold,120,255,THRESH_BINARY_INV);
    
        mThinning= mThreshold.clone();
    
        thinning(mThinning);
    
        imshow("mThinning",mThinning);
    
        vector<Point2i> locations;   // output, locations of non-zero pixels 
        findNonZero(mThinning, locations);
    
        for (int i = 0; i < locations.size(); i++)
        {
            circle(mSource_Bgr,locations[i],2,Scalar(0,255,0),1);
        }
    
        imshow("mResult",mSource_Bgr);
    
        //To Find the Lines from the image
        vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;
        /// Find contours
        findContours( mThinning.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
        /// Approximate contours to polygons + get bounding rects and circles
        vector<vector<Point> > contours_poly( contours.size() );
        for( size_t i = 0; i < contours.size(); i++ )
        { 
            approxPolyDP( Mat(contours[i]), contours_poly[i], 2, true );
        }
    
        for (int i = 0; i < contours_poly.size(); i++)
        {
            drawContours(mSource_Bgr,contours_poly,i,Scalar(255,0,0),2);
        }
        imshow("mResult",mSource_Bgr);
    
    }