zoukankan      html  css  js  c++  java
  • 根据人脸关键点实现平面三角剖分和最近邻搜索 ( KNN, K=1 ), opencv3.4.2, C++

    头文件:

    
    #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;
    }
    
    
    
    
  • 相关阅读:
    C语言学习笔记(二)数据类型、常量和变量简介
    将汉字转换为汉语拼音java实现
    C语言学习笔记(四)运算符与表达式(下)
    数组的几种排序算法的实现
    C语言学习笔记(三)运算符与表达式(上)
    java实现二叉树的常见操作
    java解析Internet网页中的内容
    C语言学习笔记(六)分支程序设计
    C语言学习笔记(一)初识C语言
    C语言学习笔记(五)顺序程序设计
  • 原文地址:https://www.cnblogs.com/theodoric008/p/9493543.html
Copyright © 2011-2022 走看看