标签:
**************************svmasm*(没发现能训练asm)********************************
#include "cv.h"
#include "highgui.h"
#include <ml.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
/////////////flandmark-master所需头文件/////////////////////
#include "flandmark_detector.h"
/////////////uricamic-clandmark所需头文件////////////////////////
#include "Flandmark.h"
#include "CFeaturePool.h"
#include "CSparseLBPFeatures.h"
#include "helpers.h"
#include "CTimer.h"
using namespace clandmark;
using namespace cv;
using namespace std;
cimg_library::CImg<unsigned char> * cvImgToCImg(cv::Mat &cvImg)
{
cimg_library::CImg<unsigned char> * result = new cimg_library::CImg<unsigned char>(cvImg.cols, cvImg.rows);
for (int x = 0; x < cvImg.cols; ++x)
for (int y = 0; y < cvImg.rows; ++y)
(*result)(x, y) = cvImg.at<uchar>(y, x);
return result;
}
int main(int argc, char** argv)
{
//Train() ;
//Detect() ;
///////////////////////////////////flandmark-master///////////////////////////////////////
////////////////////////////////////////////////////////////////////////http://cmp.felk.cvut.cz/~uricamic/flandmark/
// // load flandmark model structure and initialize
// FLANDMARK_Model * model = flandmark_init("flandmark_model.dat");
// // load input image
// IplImage *img = cvLoadImage("groupphoto.jpg");
// // convert image to grayscale
// IplImage *img_grayscale = cvCreateImage(cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
// cvCvtColor(img, img_grayscale, CV_BGR2GRAY);
// // bbox with detected face (format: top_left_col top_left_row bottom_right_col bottom_right_row)
// // Load image & detect faces
// // Add your code here
// String face_cascade_name = "haarcascade_frontalface_alt.xml";
// CascadeClassifier face_cascade;
// //-- 1. Load the cascades
// if( !face_cascade.load( "C:\\Users\\zzh\\Desktop\\asm\\uricamic-clandmark-044743d\\data\\"+face_cascade_name ) )
// {
// printf("--(!)Error loading\n");
// return -1;
// };
//std::vector<Rect> faces;
//Mat frame_gray;
//fl_double_t *landmarks;
//Mat frame = imread("groupphoto.jpg");
//cvtColor( frame, frame_gray, CV_BGR2GRAY );
////-- Detect faces
//face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
//int bbox[4];
//for( uint32_t i = 0; i < faces.size(); i++ )
//{
// // Get detected face bounding box
// bbox[0] = faces[i].x;
// bbox[1] = faces[i].y;
// bbox[2] = faces[i].x+faces[i].width;
// bbox[3] = faces[i].y+faces[i].height;
// // detect facial landmarks (output are x, y coordinates of detected landmarks)
// double * landmarks = (double*)malloc(2*model->data.options.M*sizeof(double));
// flandmark_detect(img_grayscale, bbox, model, landmarks);
// for (int i=0;i<8;i++)
// {
// Point cc;
// cc.x=(int)landmarks[2*i];
// cc.y=landmarks[2*i+1];
// cvCircle(img_grayscale, cc, 3, CV_RGB(255,0,0), 2);
// }
//}
// cvNamedWindow("Lines", CV_WINDOW_NORMAL);
// cvShowImage( "Lines", img_grayscale );
// cvWaitKey();
/////////////////////uricamic-clandmark////////////////////////////////////////
/////////////////////////////////////////////////////http://cmp.felk.cvut.cz/~uricamic/clandmark/#snippets_mv_cpp_detector
// Initialize flandmark
char modelList[][1024] = {
"JOINT_MV_AFLW_SPLIT_1_-profile.xml",
"JOINT_MV_AFLW_SPLIT_1_-30deg.xml",
"JOINT_MV_AFLW_SPLIT_1_frontal.xml",
"JOINT_MV_AFLW_SPLIT_1_30deg.xml",
"JOINT_MV_AFLW_SPLIT_1_profile.xml",
};
char views[][20] = {
"-profile",
"-half-profile",
"frontal",
"half-profile",
"profile",
};
const int PHIS = 5;
// In the main function call
string modelDir = "C:\\Users\\zzh\\Desktop\\asm\\joint_mv_models\\"; // path to directory containing flandmark model XML files (assuming the path is given as a first argument)
Flandmark *flandmarkPool[PHIS]; // pool of Flandmark instances
// initialize flandmark instances
for (int i=0; i < PHIS; ++i)
{
string cc=string(modelDir+modelList[i]);
flandmarkPool[i] = Flandmark::getInstanceOf(cc.c_str());
if (!flandmarkPool[i])
{
cerr << "Couldn‘t create instance of flandmark with model " << modelList[i] << endl;
return -1;
}
}
// initialize feature pool
const int * bw_size = flandmarkPool[0]->getBaseWindowSize();
CFeaturePool * featuresPool = new CFeaturePool(bw_size[0], bw_size[1]);
// create Sparse LBP features in the pool
featuresPool->addFeaturesToPool(
new CSparseLBPFeatures(
featuresPool->getWidth(),
featuresPool->getHeight(),
featuresPool->getPyramidLevels(),
featuresPool->getCumulativeWidths()
)
);
// register features pool in flandmark instances
for (int i=0; i < PHIS; ++i)
{
flandmarkPool[i]->setNFfeaturesPool(featuresPool);
}
// Load image & detect faces
// Add your code here
String face_cascade_name = "haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
//-- 1. Load the cascades
if( !face_cascade.load( "C:\\Users\\zzh\\Desktop\\asm\\uricamic-clandmark-044743d\\data\\"+face_cascade_name ) )
{
printf("--(!)Error loading\n");
return -1;
};
std::vector<Rect> faces;
Mat frame_gray;
int bbox[8];
fl_double_t *landmarks;
Mat frame = imread("face.jpg");
cvtColor( frame, frame_gray, CV_BGR2GRAY );
// cvtColor( frame, frame_gray, COLOR_BGR2GRAY ); // <- OpenCV 3.0
//equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
// face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
int cc=0;
for( uint32_t i = 0; i < faces.size(); i++ )
{
// Get detected face bounding box
bbox[0] = faces[i].x;
bbox[1] = faces[i].y;
bbox[2] = faces[i].x+faces[i].width;
bbox[3] = faces[i].y;
bbox[4] = faces[i].x+faces[i].width;
bbox[5] = faces[i].y+faces[i].height;
bbox[6] = faces[i].x;
bbox[7] = faces[i].y+faces[i].height;
fl_double_t scores[PHIS];
fl_double_t maximum = -1000000;
int viewID=-1;
cimg_library::CImg<unsigned char>* frm_gray = cvImgToCImg(frame_gray);
for (int phi=0; phi < PHIS; ++phi)
{
Flandmark *flandmark = flandmarkPool[phi];
flandmark->detect_optimized(frm_gray, bbox);
//flandmark->detect_optimizedFromPool(bbox);
// compute score
scores[phi] = flandmark->getScore();
if (scores[phi] > maximum)
{
maximum = scores[phi];
viewID = phi;
}
}
landmarks = flandmarkPool[viewID]->getLandmarks();
cc=flandmarkPool[viewID]->getLandmarksCount();
//delete frm_gray;
}
//// Detect landmarks
//flandmarkPool->detect_optimized(frm_gray, bbox);
//// Get detected landmarks
//fl_double_t *landmarks = flandmark->getLandmarks();
circle(frame, Point(int(landmarks[0]), int(landmarks[1])), 2, Scalar(255, 0, 0), -1);
for (int i=0; i < 4; i++)
{
circle(frame, Point(int(bbox[2*i]), int(bbox[2*i+1])), 2, Scalar(0, 255, 0), -1);
}
for (int i=2; i < 2*cc; i+=2)
{
circle(frame, Point(int(landmarks[i]), int(landmarks[i+1])), 2, Scalar(0, 0, 255), -1);
}
namedWindow("Lines", CV_WINDOW_NORMAL);
imshow( "Lines", frame );
waitKey();
delete flandmarkPool[0];
delete flandmarkPool[1];
delete flandmarkPool[2];
delete flandmarkPool[3];
delete flandmarkPool[4];
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
********************************stasm**********************************************
//#include "cv.h"
//#include "highgui.h"
//#include <ml.h>
//#include <iostream>
//#include <fstream>
//#include <string>
//#include <vector>
#include <stdio.h>
/////////////////添加stasm文件夹的文件到方案////////////////////////////////
/////////////////添加stasm文件夹的MOD_1文件夹的4个文件到方案/////////////////////
//////////////////添加data文件夹到工程///////////////////////////////
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include "opencv/highgui.h" // needed for imread
#include "stasm/stasm_lib.h"
#include "stasm/stasm_lib_ext.h" // needed for stasm_search_auto_ext
#include "stasm/stasm_landmarks.h"
#pragma warning(disable:4996) // ‘vsprintf‘: This function may be unsafe
using namespace cv;
using namespace std;
///////////////////////////////////////////////////////////
static void Exit(const char* format, ...) // args like printf
{
char s[1024+1];
va_list args;
va_start(args, format);
vsprintf(s, format, args);
va_end(args);
stasm_printf("\n%s\n", s);
exit(1);
}
//在控制台打印出标记点
static void PrintLandmarks(const float* landmarks, const char* msg)
{
stasm_printf("%s:\n", msg);
for (int i = 0; i < stasm_NLANDMARKS; i++)
stasm_printf("%3d: %4.0f %4.0f\n",
i, landmarks[i*2], landmarks[i*2+1]);//点的位置(x,y)坐标
}
//标定出这些点
static void BiaoDing(cv::Mat_<unsigned char> &img,float landmarks[],int nlandmarks=stasm_NLANDMARKS)
{
for(int i=0;i<nlandmarks;i++)
{
img(cvRound(landmarks[i*2+1]),cvRound(landmarks[2*i]))=255;
}
}
//画出标记点,连线形式
static void DrawLandmarks(cv::Mat_<unsigned char>& img,float landmarks[],int nlandmarks = stasm_NLANDMARKS)
{
for (int i = 0; i < nlandmarks-1; i++)
{
const int ix = cvRound(landmarks[i*2]); // this point
const int iy = cvRound(landmarks[i*2+1]);
const int ix1 = cvRound(landmarks[(i+1)*2]); // next point
const int iy1 = cvRound(landmarks[(i+1)*2+1]);
cv::line(img,
cv::Point(ix, iy), cv::Point(ix1, iy1), 255, 1);
}
}
int main(int argc, char** argv)
{
const int multi = 0;//将输入的为char*类型的参数转换为int如输入的为0则输出multi为0
if (multi != 0 && multi != 1) //我们设置为0,因为我们只处理一个人脸
Exit("Usage: test_stasm_lib MULTI MINWIDTH TRACE IMAGE, "
"with MULTI 0 or 1, you have MULTI %s", 0);
int minwidth = 25;
//if (sscanf(argv[2], "%d", &minwidth) != 1 ||//设置检测的人脸的最小宽度,如果没有扫描成功,或者最小的宽度<1或者大于100则退出
// minwidth < 1 || minwidth > 100)
//
//{
//
// Exit("Usage: test_stasm_lib MULTI MINWIDTH TRACE IMAGE with "
//
// "MINWIDTH 1 to 100, you have MINWIDTH %s", argv[2]);
//}
const int trace = 1;//我们想跟踪日志,所以我们设置为1
if (trace < 0 || trace > 1)
Exit("Usage: test_stasm_lib MULTI MINWIDTH TRACE IMAGE, with TRACE 0 or 1");
if (!stasm_init("../data", trace))
Exit("stasm_init failed: %s", stasm_lasterr());
const char* path = "face.jpg"; // image name//第五个参数(输入的四个参数),图像的名字
stasm_printf("Reading %s\n", path);
const cv::Mat_<unsigned char> img(cv::imread(path, CV_LOAD_IMAGE_GRAYSCALE));
if (!img.data) // could not load image?
Exit("Cannot load %s", path);
cv::Mat_<unsigned char> outimg(img.clone());
if (!stasm_open_image((const char*)img.data, img.cols, img.rows,
path, multi != 0, minwidth))
Exit("stasm_open_image failed: %s", stasm_lasterr());
// Test stasm_search_auto.
// The min face size was set in the above stasm_open_image call.
float landmarks[2 * stasm_NLANDMARKS]; // x,y coords
int iface = 0;
while (1)
{
stasm_printf("--- Auto Face %d ---\n", iface);
int foundface;
float estyaw;
if (!stasm_search_auto_ext(&foundface, landmarks, &estyaw))//如果没有成功运行
Exit("stasm_search_auto failed: %s", stasm_lasterr());
if (!foundface)//如果没有找到人脸,或者最后一个人脸结束
{
stasm_printf("No more faces\n");
break; // note break
}
char s[100]; sprintf(s, "\nFinal with auto init (estyaw %.0f)", estyaw);
PrintLandmarks(landmarks, s);//标记人脸
DrawLandmarks(outimg, landmarks);//连线人脸
iface++;//统计找到的人脸的个数。
if (trace)
stasm_printf("\n");
}
imwrite("test_stasm_lib_auto.bmp", outimg);
//下面是测试用的,当设置只寻找一个人脸,且minwidh=25并且找到了人脸,则进入调试状态。
if (multi == 0 && minwidth == 25 && iface)
{
// Test stasm_search_pinned. A human user is not at hand, so gyp by using
// points from the last face found above for our 5 start points
stasm_printf("--- Pinned Face %d ---\n", iface);
float pinned[2 * stasm_NLANDMARKS]; // x,y coords
memset(pinned, 0, sizeof(pinned));//初始化一个pin空间和stasm_NLANDMARK一样大
pinned[L_LEyeOuter*2] = landmarks[L_LEyeOuter*2] + 2;
pinned[L_LEyeOuter*2+1] = landmarks[L_LEyeOuter*2+1];
pinned[L_REyeOuter*2] = landmarks[L_REyeOuter*2] - 2;
pinned[L_REyeOuter*2+1] = landmarks[L_REyeOuter*2+1];
pinned[L_CNoseTip*2] = landmarks[L_CNoseTip*2];
pinned[L_CNoseTip*2+1] = landmarks[L_CNoseTip*2+1];
pinned[L_LMouthCorner*2] = landmarks[L_LMouthCorner*2];
pinned[L_LMouthCorner*2+1] = landmarks[L_LMouthCorner*2+1];
pinned[L_RMouthCorner*2] = landmarks[L_RMouthCorner*2];
pinned[L_RMouthCorner*2+1] = landmarks[L_RMouthCorner*2+1];
memset(landmarks, 0, sizeof(landmarks));//将landmarks重置为0
if (!stasm_search_pinned(landmarks,//利用pinned矫正landmarks
pinned, (const char*)img.data, img.cols, img.rows, path))
Exit("stasm_search_pinned failed: %s", stasm_lasterr());
PrintLandmarks(landmarks, "Final with pinned init");
outimg = img.clone();
DrawLandmarks(outimg, landmarks);
imwrite("test_stasm_lib_pinned.bmp", outimg);
// test stasm_convert_shape,找到了位置之后的处理
float newlandmarks[2 * stasm_NLANDMARKS]; // x,y coords
#if 0
memcpy(newlandmarks, landmarks, 2 * stasm_NLANDMARKS * sizeof(float));
stasm_convert_shape(newlandmarks, 68);
PrintLandmarks(newlandmarks, "stasm77 to xm2vts");
#endif
#if 0
outimg = img.clone();
DrawLandmarks(outimg, newlandmarks, 68);
imwrite("test_stasm_lib_68.bmp", outimg);
#endif
#if 0
memcpy(newlandmarks, landmarks, 2 * stasm_NLANDMARKS * sizeof(float));
stasm_convert_shape(newlandmarks, 76);
PrintLandmarks(newlandmarks, "stasm77 to stasm76");
#endif
#if 0
outimg = img.clone();
DrawLandmarks(outimg, newlandmarks, 76);
imwrite("test_stasm_lib_76.bmp", outimg);
#endif
#if 1
memcpy(newlandmarks, landmarks, 2 * stasm_NLANDMARKS * sizeof(float));
stasm_convert_shape(newlandmarks, 22);
PrintLandmarks(newlandmarks, "stasm77 to stasm22");
outimg = img.clone();
// DrawLandmarks(outimg, newlandmarks, 22);
BiaoDing(outimg,newlandmarks,22);
imwrite("test_stasm_lib_22.bmp_biaoding.bmp", outimg);
memcpy(newlandmarks, landmarks, 2 * stasm_NLANDMARKS * sizeof(float));
stasm_convert_shape(newlandmarks, 20);
PrintLandmarks(newlandmarks, "stasm77 to stasm20");
outimg = img.clone();
//DrawLandmarks(outimg, newlandmarks, 20);
BiaoDing(outimg,newlandmarks,19);
imwrite("test_stasm_lib_20_biaoding.bmp", outimg);
#endif
}
}
标签:
原文地址:http://www.cnblogs.com/zzh123/p/4810987.html