imgWithDelaunay.jpg
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdint>
#include <cstdlib>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
static void draw_subdiv( Mat& img, Subdiv2D& subdiv, Scalar delaunay_color )
{
vector<Vec6f> triangleList;
subdiv.getTriangleList(triangleList);
vector<Point> 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<Point2f> 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<uchar>(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;
}