[naive] delaunay trianglation

[naive] delaunay trianglation_第1张图片
imgWithDelaunay.jpg
#include 
#include 
#include 
#include 
#include 

#include 

using namespace std;
using namespace cv;

static void draw_subdiv( Mat& img, Subdiv2D& subdiv, Scalar delaunay_color )
{
    vector triangleList;
    subdiv.getTriangleList(triangleList);
    vector pt(3);

    for( size_t i = 0; i < triangleList.size(); i++ )
    {
        Vec6f t = triangleList[i];
        pt[0] = Point(cvRound(t[0]), cvRound(t[1]));
        pt[1] = Point(cvRound(t[2]), cvRound(t[3]));
        pt[2] = Point(cvRound(t[4]), cvRound(t[5]));
        line(img, pt[0], pt[1], delaunay_color, 1, LINE_AA, 0);
        line(img, pt[1], pt[2], delaunay_color, 1, LINE_AA, 0);
        line(img, pt[2], pt[0], delaunay_color, 1, LINE_AA, 0);
    }
}

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        cout << "usage: ./demo input_image" << endl;
        return -1;
    }

    Mat img = imread(argv[1]);

    if (img.empty())
    {
        cout << "error: image is empty" << endl;
    }

    Mat imgGray, imgBlur, imgEdge;

    cvtColor(img, imgGray, CV_BGR2GRAY);

    //GaussianBlur(imgGray, imgBlur, Size(3, 3), 0, 0);
    blur(imgGray, imgBlur, Size(3, 3));

    double threshold = 10.0;
    Canny(imgBlur, imgEdge, threshold, threshold * 3, 3);

    // imshow("img with edge", imgEdge);
    // waitKey(0);

    vector supportPoints;

    uint32_t imgHeight = img.rows;
    uint32_t imgWidth = img.cols;

    uint32_t sampleInterval = 3;
    for (int row = 0; row < imgHeight; row += sampleInterval)
    {
        for (int col = 0; col < imgWidth; col += sampleInterval)
        {
            if (imgEdge.at(row, col) == 255)
            {
                supportPoints.push_back(Point2f(col, row));
            }
        }
    }

    Mat imgSupportPoints = img.clone();

    for (int i = 0; i < supportPoints.size(); i++)
    {
        circle( imgSupportPoints, supportPoints[i], 1, Scalar(0, 0, 255), FILLED, LINE_8, 0 );
    }

    // imshow("img with support points", imgSupportPoints);
    // waitKey(0);


    Scalar delaunay_color(0,0,255);
    Rect rect(0, 0, imgWidth, imgHeight);

    Subdiv2D subdiv(rect);

    subdiv.insert(supportPoints);

    draw_subdiv(img, subdiv, delaunay_color);

    imshow("img with Delaunay", img);
    waitKey(0);

    return 0;
}

你可能感兴趣的:([naive] delaunay trianglation)