// SeetaFacesTest.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"#include <iostream>
#include <cstdint>
#include <fstream>
#include <string>
#include <cmath>#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur#include "face_detection.h"
#include "face_alignment.h"using namespace cv;
using namespace std;int main()
{// Initialize face detection modelseeta::FaceDetection detector("seeta_fd_frontal_v1.0.bin");detector.SetMinFaceSize(80);detector.SetScoreThresh(2.f);detector.SetImagePyramidScaleFactor(0.8f);detector.SetWindowStep(4, 4);// Initialize face alignment model seeta::FaceAlignment point_detector("seeta_fa_v1.1.bin");//load imageconst char* picturename = "10.jpg";cv::Mat img_grayscale = cv::imread(picturename, 0);cv::Mat img_color = cv::imread(picturename, 1);if (img_grayscale.empty()){return 0;}seeta::ImageData img_data;img_data.data = img_grayscale.data;img_data.width = img_grayscale.cols;img_data.height = img_grayscale.rows;img_data.num_channels = 1;// Detect facesstd::vector<seeta::FaceInfo> faces = detector.Detect(img_data);int32_t face_num = static_cast<int32_t>(faces.size());if (face_num == 0){return 0;}// Detect 5 facial landmarksseeta::FacialLandmark points[5];point_detector.PointDetectLandmarks(img_data, faces[0], points);Rect face_rect;face_rect.x = faces[0].bbox.x;face_rect.y = faces[0].bbox.y;face_rect.width = faces[0].bbox.width;face_rect.height = faces[0].bbox.height;cv::Mat img_rect(img_grayscale, face_rect);  //截取脸部cv::namedWindow("img_grayscale", CV_WINDOW_NORMAL);cv::imshow("img_grayscale", img_rect);//-------计算角度--------int x1 = points[0].x;int y1 = points[0].y;int x2 = points[1].x;int y2 = points[1].y;cout << "0: " << x1 << ", " << y1 << endl;cout << "1: " << x2 << ", " << y2 << endl;double dX = (double)(x2 - x1);double dY = (double)(y2 - y1);float nAngle = (float)(atan(dY / dX)*180/3.1415926);cout << "nAngle:" << nAngle << endl;RotatedRect rrect(cvPoint(points[2].x, points[2].y), Size(face_rect.width, face_rect.height), nAngle);Point2f vertices[4];rrect.points(vertices);for (int i = 0; i < 4; i++){line(img_color, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 150));}// Visualize the resultscv::rectangle(img_color, face_rect, Scalar(150, 255, 0), 5, 8, 0);for (int i = 0; i < 5; i++){cout << i << ": " << points[i].x << ", " << points[i].y << endl;cv::circle(img_color, cvPoint(points[i].x, points[i].y), 10, CV_RGB(0, 255, 0), CV_FILLED);}cv::namedWindow("result", CV_WINDOW_NORMAL);cv::resizeWindow("result", 484, 644);cv::imshow("result", img_color);cv::waitKey(0);cv::imwrite("result.jpg", img_color);return 0;
}

// SeetaFacesTest.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"#include <iostream>
#include <cstdint>
#include <fstream>
#include <string>
#include <cmath>#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur#include "face_detection.h"
#include "face_alignment.h"using namespace cv;
using namespace std;//逆时针旋转图像degree角度(原尺寸)
void rotateImage(const cv::Mat src, const cv::Mat& img_rotate, double degree)
{//旋转中心为图像中心  Point2f center;center.x = float(src.cols / 2.0 + 0.5);center.y = float(src.rows / 2.0 + 0.5);int length = 0;length = sqrt(src.cols*src.cols + src.rows*src.rows);//计算二维旋转的仿射变换矩阵Mat M = getRotationMatrix2D(center, degree, 1);warpAffine(src, img_rotate, M, Size(length, length));//仿射变换
}int main()
{// Initialize face detection modelseeta::FaceDetection detector("seeta_fd_frontal_v1.0.bin");detector.SetMinFaceSize(80);detector.SetScoreThresh(2.f);detector.SetImagePyramidScaleFactor(0.8f);detector.SetWindowStep(4, 4);// Initialize face alignment model seeta::FaceAlignment point_detector("seeta_fa_v1.1.bin");//load imageconst char* picturename = "10.jpg";cv::Mat img_grayscale = cv::imread(picturename, 0);cv::Mat img_color = cv::imread(picturename, 1);if (img_grayscale.empty()){return 0;}seeta::ImageData img_data;img_data.data = img_grayscale.data;img_data.width = img_grayscale.cols;img_data.height = img_grayscale.rows;img_data.num_channels = 1;// Detect facesstd::vector<seeta::FaceInfo> faces = detector.Detect(img_data);int32_t face_num = static_cast<int32_t>(faces.size());if (face_num == 0){return 0;}// Detect 5 facial landmarksseeta::FacialLandmark points[5];point_detector.PointDetectLandmarks(img_data, faces[0], points);//Rect face_rect;//face_rect.x = faces[0].bbox.x;//face_rect.y = faces[0].bbox.y;//face_rect.width = faces[0].bbox.width;//face_rect.height = faces[0].bbox.height;//---------------------Rect face_rect;int tempx0 = points[0].x < points[3].x ? points[0].x : points[3].x;int tempx1 = points[1].x > points[4].x ? points[1].x : points[4].x;int nfaceW = (int)((tempx1 - tempx0)*2.2);int tempy0 = points[0].y > points[1].y ? points[0].y : points[1].y;int tempy1 = points[3].y > points[4].y ? points[3].y : points[4].y;int nfaceH = (int)((tempy1 - tempy0)*2.2);//int FaceWidth = (nfaceW > nfaceH ? nfaceW : nfaceH) * 1.3;int x0 = points[2].x - nfaceW / 2;int y0 = points[2].y - nfaceW / 2;int w0 = nfaceW;int h0 = nfaceW;face_rect.x = x0;face_rect.y = y0;face_rect.width = w0;face_rect.height = h0;cv::Mat img_rect(img_color, face_rect);  //截取脸部cv::circle(img_rect, cvPoint(img_rect.cols / 2, img_rect.rows / 2), 10, CV_RGB(0, 255, 0), CV_FILLED);cv::namedWindow("img_rect", CV_WINDOW_NORMAL);cv::imshow("img_rect", img_rect);cout << "img_rect : " << img_rect.cols << ", " << img_rect.rows << endl;//-------计算角度--------int x1 = points[0].x;int y1 = points[0].y;int x2 = points[1].x;int y2 = points[1].y;cout << "0: " << x1 << ", " << y1 << endl;cout << "1: " << x2 << ", " << y2 << endl;double dX = (double)(x2 - x1);double dY = (double)(y2 - y1);float Angle = (float)(atan(dY / dX)*180/3.1415926);cout << "nAngle:" << Angle << endl;//--------------------------------int length = 0;length = sqrt(img_rect.cols*img_rect.cols + img_rect.rows*img_rect.rows);cv::Mat tempImg(length, length, img_rect.type());//临时图像,大小和输出图像一样大int ROI_x = length / 2 - img_rect.cols / 2;//ROI矩形左上角的x坐标  int ROI_y = length / 2 - img_rect.rows / 2;//ROI矩形左上角的y坐标  Rect ROIRect(ROI_x, ROI_y, img_rect.cols, img_rect.rows);//ROI矩形  Mat tempImgROI2(tempImg, ROIRect);//tempImg的中间部分img_rect.copyTo(tempImgROI2);//将原图复制到tempImg的中心cv::namedWindow("tempImg", CV_WINDOW_NORMAL);cv::imshow("tempImg", tempImg);cv::Mat dst_image;Point2f center(float(length / 2.0), float(length / 2.0));//旋转中心Mat M = getRotationMatrix2D(center, (double)Angle, 1);//计算旋转的仿射变换矩阵warpAffine(tempImg, dst_image, M, Size(length, length));//仿射变换cout << "dst_image : " << dst_image.cols << ", " << dst_image.rows << endl;cv::circle(dst_image, cvPoint(dst_image.cols / 2, dst_image.rows / 2), 10, CV_RGB(150, 255, 0), CV_FILLED);cv::namedWindow("dst_image", CV_WINDOW_NORMAL);cv::imshow("dst_image", dst_image);//-----------------------int ndstfaceW = (int)((float)nfaceW/2.2*1.5);Rect dst_rect;dst_rect.x = dst_image.cols / 2 - ndstfaceW / 2;dst_rect.y = dst_image.rows / 2 - ndstfaceW / 2;dst_rect.width = ndstfaceW;dst_rect.height = ndstfaceW;cv::Mat img_dst_face(dst_image, dst_rect);  //截取脸部cv::namedWindow("img_dst_face", CV_WINDOW_NORMAL);cv::imshow("img_dst_face", img_dst_face);//-------------------RotatedRect rrect(cvPoint(points[2].x, points[2].y), Size(face_rect.width, face_rect.height), Angle);Point2f vertices[4];rrect.points(vertices);for (int i = 0; i < 4; i++){line(img_color, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 150));}// Visualize the resultscv::rectangle(img_color, face_rect, Scalar(150, 255, 0), 5, 8, 0);for (int i = 0; i < 5; i++){cout << i << ": " << points[i].x << ", " << points[i].y << endl;cv::circle(img_color, cvPoint(points[i].x, points[i].y), 10, CV_RGB(0, 255, 0), CV_FILLED);}cv::namedWindow("result", CV_WINDOW_NORMAL);cv::resizeWindow("result", 484, 644);cv::imshow("result", img_color);cv::waitKey(0);cv::imwrite("result.jpg", img_color);return 0;
}

【OpenCV】人脸旋转角度计算相关推荐

  1. opencv 2d人脸姿态计算

    opencv 2d人脸姿态计算 可以的: # -*- coding: utf-8 -*- # 测试使用opencv中的函数solvepnp import cv2 import numpy as np ...

  2. OpenCV与图像处理学习十七——OpenCV人脸检测(含代码)

    OpenCV与图像处理学习十七--OpenCV人脸检测(含代码) 一.人脸识别概要 1.1 人脸检测 1.2 人脸对齐(Face Alignment) 1.3 人脸特征提取(Face Feature ...

  3. Python+OpenCV人脸识别签到考勤系统(新手入门)

    Python+OpenCV人脸识别签到考勤系统(新手入门) 前言 项目效果图 项目需要的环境 编译器 辅助开发QT-designer 项目配置 代码部分 核心代码 项目目录结构 后记 正式版改进 项目 ...

  4. Python+opencv 人脸识别

    python+opencv人脸检测+识别示例及原理解析 一.开发环境搭建 二.图片人脸检测 2.1 文件准备与编程 2.2 注意事项 三.视频人脸识别 3.1文件准备与编程 3.2 注意事项 四.人脸 ...

  5. 【opencv人脸识别1】从图片中检测人脸

    [opencv人脸识别一]从图片中检测人脸 本系列主要讲述利用opencv实现人脸识别的相关知识,并给出实际代码.且循序渐进,由基础到复杂,从最基本的图片检测人脸到视频检测.识别人脸,再到较大型人脸数 ...

  6. 基于opencv人脸识别

    基于opencv平台实现人脸识别.mac os建议使用pycharm ce 编译器使用Xcode 第一步建立代码运行的环境 打开pycharm ce 终端或者mac 终端 输入pip install ...

  7. OpenCV 人脸识别 源代码

    请直接查看原文 OpenCV 人脸识别 源代码 https://hotdog29.com/?p=553 在 2019年7月6日 上张贴 由 hotdog发表回复 opencv 人脸识别 在本教程中,您 ...

  8. 由6,14以及68点人脸关键点计算头部姿态

    前言 关于头部姿态估计理论部分的内容,网络上包括我所列的参考文献中都有大量概述,我不再重复.这里直入主题,如何通过图像中2D人脸关键点计算出头部姿态角,具体就是计算出俯仰角(pitch),偏航角(ya ...

  9. OpenCV人脸识别(1)原理介绍

    前言 本系列博客学习如何使用OpenCV来执行面部识别. 为了构建人脸识别系统,我们首先进行人脸检测,使用深度学习从每个人脸提取人脸特征,在提取到特征上训练人脸识别模型,然后用OpenCV识别图像和视 ...

最新文章

  1. linux统计日志,Linux一些常使用的统计日志 方法
  2. mybatis集成 Invalid bound statement (not found)
  3. SQL Server读写分离之发布订阅
  4. 基于javaSpringboot+mybatis+layui的装修验收管理系统设计和实现
  5. Video.js实现rtmp视频播放
  6. CSU 1573 最多的数字
  7. 大数据之-Hadoop之HDFS的API操作_文件IO流_上传_案例---大数据之hadoop工作笔记0063
  8. js能订阅mq吗_ActiveMQ+MQTT实现客户端订阅推送模式(一)订阅者
  9. ubuntu scrt
  10. Unity游戏开发团队包括哪些角色?
  11. 点云sift matlab,点云配准SIFT算法
  12. mysql reads sql data_mysql – CREATE FUNCTION错误“此函数没有确定,否SQL或READS SQL DATA”...
  13. C# CRC16 CCITT XModem
  14. ios中嵌套h5做的app,长按图片默认会有放大效果;如何禁止
  15. linux mtr 安装,遇到网络问题?别慌!MTR来帮您
  16. 计算机dvd打不开,我的电脑DVD打不开。怎么办?
  17. 大前端 - react - 服务端渲染 - Next.js
  18. [CTO札记]人人网的‘Connect(人人连接)’很有想法
  19. EV录屏软件操作方法
  20. 笔记本“电源已接通,未充电”的解决办法

热门文章

  1. 利用win10镜像文件安装.net framework 3.5 简单快速
  2. php小程序地图处理,微信小程序地图 map
  3. 赵小楼《天道》《遥远的救世主》深度解析(118)女人的客观就那么难么?不难。难的是不想客观的人。
  4. 实体链接在OPPO小布助手和OGraph的实践应用
  5. Simulink选择器模块(Selector)
  6. java 判断是否是罗马_如何只使用正则表达式匹配有效的罗马数字?
  7. html5script计时器,javascript实现秒表计时器的制作方法
  8. k8s对外攻击面总结
  9. 前端知识合集【重中之重】,我只看这一篇!
  10. win10管理员权限启动cmd