头文件:
#pragma once
#include <iostream>
#include <vector>
#include <map>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define UL unsigend long
using namespace std;
using namespace cv;
int test_triangulation();
void show_a_image_mat(const Mat& img);
cv::Mat VisualizePoints(const cv::Mat& src_img, const vector<cv::Point2f>& pts);
源文件:
#include "delaunay_triangulation.h"
void show_a_image_mat(const Mat& img){
namedWindow("src_img", WINDOW_AUTOSIZE);
imshow("src_img", img);
waitKey();
destroyAllWindows();
}
Mat VisualizePoints(const Mat& src_img, const vector<Point2f>& pts)
{
// draw circles on Mat
Mat render = src_img.clone();
for (auto& i: pts) {
circle(render, i, 2, Scalar(255, 255, 255), -1, CV_AA);
}
return render;
}
int test_triangulation(){
cout << "start test_triangulation" << endl;
// 读图
string src_img_path = "/Users/liujiashu/CLionProjects/debug_lrn/jpegs/JJY.jpg";
Mat src_img = imread(src_img_path);
if(src_img.empty()){
cout << "empty img" << endl;
return -1;
}
auto img_height = src_img.size[0];
auto img_width = src_img.size[1];
// 选取一些点
vector<Point2f> pts_vec;
vector<int> pts_index_vec;
map<int, int> pt_id2pt_index;
int pt_index = 0;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++ ){
pts_vec.emplace_back(img_width / 20.0 * i, img_height / 20.0 * j);
pts_index_vec.emplace_back(pt_index);
pt_index++;
}
}
CvRect rect = {0, 0, img_width, img_height};
Subdiv2D sub_div;
sub_div.initDelaunay(rect);
for(int i = 0; i < pts_vec.size(); i++) {
int pt_id = sub_div.insert(pts_vec[i]);
pt_index = pts_index_vec[i];
pt_id2pt_index.insert(pair<int, int>(pt_id, pt_index));
}
Point2f target = {50, 50};
auto vertex_ptr = sub_div.findNearest(target);
auto nearest_point_cord = sub_div.getVertex(vertex_ptr);
cout << nearest_point_cord.x << endl;
cout << nearest_point_cord.y << endl;
cout << pt_id2pt_index[vertex_ptr] << endl;
if(true){
Mat render = src_img.clone();
for (auto& i: pts_vec) {
circle(render, i, 2, Scalar(255, 255, 255), -1, CV_AA);
}
circle(render, target, 2, Scalar(0, 0, 255), -1, CV_AA);
circle(render, nearest_point_cord, 5, Scalar(0, 255, 0), -1, CV_AA);
show_a_image_mat(render);
}
return 0;
}