欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

乱七八糟

程序员文章站 2024-03-25 08:14:22
...
// MathFuncDll.cpp: 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"  //预编译头
#include "face_lift.h" 


#define PI 3.1415926
#define DETECT_BUFFER_SIZE 0x20000


Mat LocalTranslationWarp(Mat &img, int startX, int startY, int endX, int endY, float radius)//平移
{
	double ddradius = float(radius * radius);
	Mat copyImg;
	copyMakeBorder(img, copyImg, 0, 1, 0, 1, BORDER_REPLICATE);
	double ddmc = (endX - startX) * (endX - startX) + (endY - startY) * (endY - startY);
	int H = img.rows;
	int W = img.cols;
	int C = 3;
	for (int i = 0; i < W; i++)
	{
		for (int j = 0; j < H; j++)
		{
			if (fabs(i - startX) > radius and fabs(j - startY) > radius)
			{
				continue;
			}
			double distance = (i - startX) * (i - startX) + (j - startY) * (j - startY);
			if (distance < ddradius)
			{
				double ratio = (ddradius - distance) / (ddradius - distance + ddmc);
				ratio = ratio * ratio;
				double UX = i - ratio * (endX - startX);
				double UY = j - ratio * (endY - startY);
				//Vec3b pix = copyImg.ptr<Vec3b>(i)[j];
				//双线性插值法(或者内点法)//
				int x1 = UX;
				int x2 = x1 + 1;
				int y1 = UY;
				int y2 = y1 + 1;
				float a0 = float(img.ptr<Vec3b>(y1)[x1][0]) * (float(x2) - UX)*(float(y2) - UY);
				float a1 = float(img.ptr<Vec3b>(y1)[x1][1]) * (float(x2) - UX)*(float(y2) - UY);
				float a2 = float(img.ptr<Vec3b>(y1)[x1][2]) * (float(x2) - UX)*(float(y2) - UY);

				float b0 = float(img.ptr<Vec3b>(y1)[x2][0]) * (UX - float(x1))*(float(y2) - UY);
				float b1 = float(img.ptr<Vec3b>(y1)[x2][1]) * (UX - float(x1))*(float(y2) - UY);
				float b2 = float(img.ptr<Vec3b>(y1)[x2][2]) * (UX - float(x1))*(float(y2) - UY);

				float c0 = float(img.ptr<Vec3b>(y2)[x1][0]) * (float(x2) - UX)*(UY - float(y1));
				float c1 = float(img.ptr<Vec3b>(y2)[x1][1]) * (float(x2) - UX)*(UY - float(y1));
				float c2 = float(img.ptr<Vec3b>(y2)[x1][2]) * (float(x2) - UX)*(UY - float(y1));

				float d0 = float(img.ptr<Vec3b>(y2)[x2][0]) * (UX - float(x1))*(UY - float(y1));
				float d1 = float(img.ptr<Vec3b>(y2)[x2][1]) * (UX - float(x1))*(UY - float(y1));
				float d2 = float(img.ptr<Vec3b>(y2)[x2][2]) * (UX - float(x1))*(UY - float(y1));
				copyImg.ptr<Vec3b>(j)[i][0] = int(a0 + b0 + c0 + d0);
				copyImg.ptr<Vec3b>(j)[i][1] = int(a1 + b1 + c1 + d1);
				copyImg.ptr<Vec3b>(j)[i][2] = int(a2 + b2 + c2 + d2);
				//cout << copyImg.ptr<Vec3b>(j)[i] << endl;
			}
		}
	}

	return copyImg;
}



Mat LocalTranslationWarp1(Mat &img, int startX, int startY, int endX, int endY, float radius)//大眼
{
	double ddradius = float(radius * radius);
	Mat copyImg;
	copyMakeBorder(img, copyImg, 0, 1, 0, 1, BORDER_REPLICATE);
	double ddmc = (endX - startX) * (endX - startX) + (endY - startY) * (endY - startY);
	int H = img.rows;
	int W = img.cols;
	int C = 3;
	for (int i = 0; i < W; i++)
	{
		for (int j = 0; j < H; j++)
		{
			if (fabs(i - startX) > radius and fabs(j - startY) > radius)
			{
				continue;
			}
			double distance = (i - startX) * (i - startX) + (j - startY) * (j - startY);
			if (distance < ddradius)
			{
				double ratio = (ddradius - distance) / (ddradius - distance + ddmc);
				ratio = ratio * ratio;
				double UX = i - ratio * (endX - startX);
				double UY = j - ratio * (endY - startY);
				//Vec3b pix = copyImg.ptr<Vec3b>(i)[j];
				//双线性插值法(或者内点法)//
				int x1 = UX;
				int x2 = x1 + 1;
				int y1 = UY;
				int y2 = y1 + 1;
				float a0 = float(img.ptr<Vec3b>(y1)[x1][0]) * (float(x2) - UX)*(float(y2) - UY);
				float a1 = float(img.ptr<Vec3b>(y1)[x1][1]) * (float(x2) - UX)*(float(y2) - UY);
				float a2 = float(img.ptr<Vec3b>(y1)[x1][2]) * (float(x2) - UX)*(float(y2) - UY);

				float b0 = float(img.ptr<Vec3b>(y1)[x2][0]) * (UX - float(x1))*(float(y2) - UY);
				float b1 = float(img.ptr<Vec3b>(y1)[x2][1]) * (UX - float(x1))*(float(y2) - UY);
				float b2 = float(img.ptr<Vec3b>(y1)[x2][2]) * (UX - float(x1))*(float(y2) - UY);

				float c0 = float(img.ptr<Vec3b>(y2)[x1][0]) * (float(x2) - UX)*(UY - float(y1));
				float c1 = float(img.ptr<Vec3b>(y2)[x1][1]) * (float(x2) - UX)*(UY - float(y1));
				float c2 = float(img.ptr<Vec3b>(y2)[x1][2]) * (float(x2) - UX)*(UY - float(y1));

				float d0 = float(img.ptr<Vec3b>(y2)[x2][0]) * (UX - float(x1))*(UY - float(y1));
				float d1 = float(img.ptr<Vec3b>(y2)[x2][1]) * (UX - float(x1))*(UY - float(y1));
				float d2 = float(img.ptr<Vec3b>(y2)[x2][2]) * (UX - float(x1))*(UY - float(y1));
				copyImg.ptr<Vec3b>(j)[i][0] = int(a0 + b0 + c0 + d0);
				copyImg.ptr<Vec3b>(j)[i][1] = int(a1 + b1 + c1 + d1);
				copyImg.ptr<Vec3b>(j)[i][2] = int(a2 + b2 + c2 + d2);
				//cout << copyImg.ptr<Vec3b>(j)[i] << endl;
			}
		}
	}

	return copyImg;
}



//对人像的人脸进行鼻子变小处理。
double  __stdcall face_lift(double a, const char*  path)
{
	unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
	Mat src = imread(path, IMREAD_ANYCOLOR | IMREAD_IGNORE_ORIENTATION);
	Mat gray;
	cvtColor(src, gray, COLOR_BGR2GRAY);
	auto pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
	Mat m_roi_1;
	int i = 0;
	short * p = ((short*)(pResults + 1)) + 142 * i;
	int x = p[0];
	int y = p[1];
	int w = p[2];
	int h = p[3];
	int neighbors = p[4];
	int angle = p[5];
	if (1)
	{
		for (int j = 0; j < 68; j++)
		{
			//cv::circle(src, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0));
			//cout << p[6 + 2 * j] <<"--"<< p[6 + 2 * j + 1] << endl;
		}
	}
	cout << p[6 + 2 * 30] << endl;
	cout << p[6 + 2 * 30 + 1] << endl;
	float r_left = sqrt((p[6 + 2 * 3] - p[6 + 2 * 5])*(p[6 + 2 * 3] - p[6 + 2 * 5]) + (p[6 + 2 * 3 + 1] - p[6 + 2 * 5 + 1])*(p[6 + 2 * 3 + 1] - p[6 + 2 * 5 + 1]));
	float r_right = sqrt((p[6 + 2 * 13] - p[6 + 2 * 15])*(p[6 + 2 * 13] - p[6 + 2 * 15]) + (p[6 + 2 * 13 + 1] - p[6 + 2 * 15 + 1])*(p[6 + 2 * 13 + 1] - p[6 + 2 * 15 + 1]));
	Mat data = LocalTranslationWarp(src, p[6 + 2 * 3], p[6 + 2 * 3 + 1], p[6 + 2 * 30], p[6 + 2 * 30 + 1], r_left);
	Mat data1 = LocalTranslationWarp(data, p[6 + 2 * 13], p[6 + 2 * 13 + 1], p[6 + 2 * 30], p[6 + 2 * 30 + 1], r_right);
	fstream _file;
	 _file.open("shoulian.jpg", ios::in);
     if (!_file)
	 {
		
	 }
	 else
	{
		 remove("shoulian.jpg");
	 }
	imwrite("shoulian.jpg", data1);
	/*cout << r_left << "====" << r_right << endl;
	namedWindow("enhanced", 0);
	resizeWindow("enhanced", 640, 480);
	imshow("enhanced", data1);
	waitKey(0);*/
	return 1;
}




double  __stdcall dayan(double a, const char*  path)
{
	unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
	Mat src = imread(path, IMREAD_ANYCOLOR | IMREAD_IGNORE_ORIENTATION);
	Mat gray;
	cvtColor(src, gray, COLOR_BGR2GRAY);
	auto pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
	Mat m_roi_1;
	int i = 0;
	short * p = ((short*)(pResults + 1)) + 142 * i;
	int x = p[0];
	int y = p[1];
	int w = p[2];
	int h = p[3];
	int neighbors = p[4];
	int angle = p[5];
	if (1)
	{
		for (int j = 0; j < 68; j++)
		{
			//cv::circle(src, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0));
			//cout << p[6 + 2 * j] <<"--"<< p[6 + 2 * j + 1] << endl;
		}
	}
	cout << p[6 + 2 * 30] << endl;
	cout << p[6 + 2 * 30 + 1] << endl;
	float r_left = sqrt((p[6 + 2 * 3] - p[6 + 2 * 5])*(p[6 + 2 * 3] - p[6 + 2 * 5]) + (p[6 + 2 * 3 + 1] - p[6 + 2 * 5 + 1])*(p[6 + 2 * 3 + 1] - p[6 + 2 * 5 + 1]));
	float r_right = sqrt((p[6 + 2 * 13] - p[6 + 2 * 15])*(p[6 + 2 * 13] - p[6 + 2 * 15]) + (p[6 + 2 * 13 + 1] - p[6 + 2 * 15 + 1])*(p[6 + 2 * 13 + 1] - p[6 + 2 * 15 + 1]));
	Mat data = LocalTranslationWarp(src, p[6 + 2 * 3], p[6 + 2 * 3 + 1], p[6 + 2 * 30], p[6 + 2 * 30 + 1], r_left);
	Mat data1 = LocalTranslationWarp(data, p[6 + 2 * 13], p[6 + 2 * 13 + 1], p[6 + 2 * 30], p[6 + 2 * 30 + 1], r_right);
	fstream _file;
	_file.open("dayan.jpg", ios::in);
	if (!_file)
	{

	}
	else
	{
		remove("dayan.jpg");
	}
	imwrite("dayan.jpg", data1);
	/*cout << r_left << "====" << r_right << endl;
	namedWindow("enhanced", 0);
	resizeWindow("enhanced", 640, 480);
	imshow("enhanced", data1);
	waitKey(0);*/
	return 1;
}


double  __stdcall Little_nose(double a, const char*  path)
{
	unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
	Mat src = imread(path, IMREAD_ANYCOLOR | IMREAD_IGNORE_ORIENTATION);
	Mat gray;
	cvtColor(src, gray, COLOR_BGR2GRAY);
	auto pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
	Mat m_roi_1;
	int i = 0;
	short * p = ((short*)(pResults + 1)) + 142 * i;
	int x = p[0];
	int y = p[1];
	int w = p[2];
	int h = p[3];
	int neighbors = p[4];
	int angle = p[5];
	if (1)
	{
		for (int j = 0; j < 68; j++)
		{
			//cv::circle(src, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0));
			//cout << p[6 + 2 * j] <<"--"<< p[6 + 2 * j + 1] << endl;
		}
	}
	int left_landmark = 31;
	int left_landmark_down = 30;
	int right_landmark = 35;
	int right_landmark_down = 30;
	int endPt = 28;

	float r_left = sqrt((p[6 + 2 * left_landmark] - p[6 + 2 * left_landmark_down])*(p[6 + 2 * left_landmark] - p[6 + 2 * left_landmark_down]) + (p[6 + 2 * left_landmark + 1] - p[6 + 2 * left_landmark_down + 1])*(p[6 + 2 * left_landmark + 1] - p[6 + 2 * left_landmark_down + 1]));
	float r_right = sqrt((p[6 + 2 * right_landmark] - p[6 + 2 * right_landmark_down])*(p[6 + 2 * right_landmark] - p[6 + 2 * right_landmark_down]) + (p[6 + 2 * right_landmark + 1] - p[6 + 2 * right_landmark_down + 1])*(p[6 + 2 * right_landmark + 1] - p[6 + 2 * right_landmark_down + 1]));
	Mat data = LocalTranslationWarp(src, p[6 + 2 * left_landmark], p[6 + 2 * left_landmark + 1], p[6 + 2 * endPt], p[6 + 2 * endPt + 1], 1.5*r_left);
	Mat data1 = LocalTranslationWarp(data, p[6 + 2 * right_landmark], p[6 + 2 * right_landmark + 1], p[6 + 2 * endPt], p[6 + 2 * endPt + 1], 1.5 * r_right);
	fstream _file;
	_file.open("suibi.jpg", ios::in);
	if (!_file)
	{

	}
	else
	{
		remove("suibi.jpg");
	}
	imwrite("suibi.jpg", data1);
	/*cout << r_left << "====" << r_right << endl;
	namedWindow("enhanced", 0);
	resizeWindow("enhanced", 640, 480);
	imshow("enhanced", data1);
	waitKey(0);*/
	return 1;
}
double  __stdcall Cone_face(double a, const char*  path)
{
	unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
	Mat src = imread(path, IMREAD_ANYCOLOR | IMREAD_IGNORE_ORIENTATION);
	Mat gray;
	cvtColor(src, gray, COLOR_BGR2GRAY);
	auto pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
	Mat m_roi_1;
	int i = 0;
	short * p = ((short*)(pResults + 1)) + 142 * i;
	int x = p[0];
	int y = p[1];
	int w = p[2];
	int h = p[3];
	int neighbors = p[4];
	int angle = p[5];
	if (1)
	{
		for (int j = 0; j < 68; j++)
		{
			//cv::circle(src, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0));
			//cout << p[6 + 2 * j] <<"--"<< p[6 + 2 * j + 1] << endl;
		}
	}
	int left_landmark = 7;
	int left_landmark_down = 8;
	int right_landmark = 9;
	int right_landmark_down = 8;
	int right_landmark1 = 8;
	int endPt = 7;
	int endPt1 = 9;
	int endPt2 = 8;

	float r_left = sqrt((p[6 + 2 * left_landmark] - p[6 + 2 * left_landmark_down])*(p[6 + 2 * left_landmark] - p[6 + 2 * left_landmark_down]) + (p[6 + 2 * left_landmark + 1] - p[6 + 2 * left_landmark_down + 1])*(p[6 + 2 * left_landmark + 1] - p[6 + 2 * left_landmark_down + 1]));
	float r_right = sqrt((p[6 + 2 * right_landmark] - p[6 + 2 * right_landmark_down])*(p[6 + 2 * right_landmark] - p[6 + 2 * right_landmark_down]) + (p[6 + 2 * right_landmark + 1] - p[6 + 2 * right_landmark_down + 1])*(p[6 + 2 * right_landmark + 1] - p[6 + 2 * right_landmark_down + 1]));
	/*Mat data = LocalTranslationWarp(src, p[6 + 2 * left_landmark], p[6 + 2 * left_landmark + 1], p[6 + 2 * endPt], p[6 + 2 * endPt + 1] + 10, 1 * r_left);
	Mat data1 = LocalTranslationWarp(data, p[6 + 2 * right_landmark], p[6 + 2 * right_landmark + 1], p[6 + 2 * endPt1], p[6 + 2 * endPt1 + 1] + 10, 1 * r_right);*/
	Mat data1 = LocalTranslationWarp(src, p[6 + 2 * right_landmark1], p[6 + 2 * right_landmark1 + 1], p[6 + 2 * endPt2], p[6 + 2 * endPt2 + 1] + 10, 2 * r_right);
	fstream _file;
	_file.open("Coneface.jpg", ios::in);
	if (!_file)
	{

	}
	else
	{
		remove("Coneface.jpg");
	}
	imwrite("Coneface.jpg", data1);
	return 1;
}


string Trim(string& str)
{
	//str.find_first_not_of(" \t\r\n"),在字符串str中从索引0开始,返回首次不匹配"\t\r\n"的位置
	str.erase(0, str.find_first_not_of(" \t\r\n"));
	str.erase(str.find_last_not_of(" \t\r\n") + 1);
	return str;
}

template <class Type>
Type stringToNum(const string& str)
{
	istringstream iss(str);
	Type num;
	iss >> num;
	return num;
}
void bubble(float *a, int n) /*定义两个参数:数组首地址与数组大小*/
{
	int i, j;
	float temp;

	for (i = 0; i < n - 1; i++)

		for (j = i + 1; j < n; j++) /*注意循环的上下限*/

			if (a[i] > a[j]) {

				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
}
double  __stdcall bi(double a, const char*  path)
{
	
	ifstream fin("E:/PbModels/result/data1.csv"); //打开文件流操作
	string line;
	float balance[1000];
	int i = 0;
	while (getline(fin, line))   //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
	{		
		istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
		vector<string> fields; //声明一个字符串向量
		string field;
		while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
		{
			fields.push_back(field); //将刚刚读取的字符串添加到向量fields中
		}
		string name = Trim(fields[0]); //清除掉向量fields中第一个元素的无效字符,并赋值给变量name	
		float a = stringToNum<float>(name);
		string age = Trim(fields[1]); //清除掉向量fields中第二个元素的无效字符,并赋值给变量age

		balance[i] = a;
		i = i + 1;
	}
	float *p = new  float[i];
	int j = 0;
	for (int i = 0; i <= 1000; i++)
	{

		if (balance[i] == (double)0)
		{
			break;
		}
		p[j] = balance[i];
		cout << "j=" << j << "值=" << p[j] << endl;
		j = j + 1;
	}
	bubble(p, i);
	return p[0];
}
double  __stdcall aad(double a, const char*  path)
{

	ifstream fin("E:/PbModels/result/data1.csv"); //打开文件流操作
	string line;
	float balance[1000];
	int i = 0;
	while (getline(fin, line))   //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
	{
		istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
		vector<string> fields; //声明一个字符串向量
		string field;
		while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
		{
			fields.push_back(field); //将刚刚读取的字符串添加到向量fields中
		}
		string name = Trim(fields[0]); //清除掉向量fields中第一个元素的无效字符,并赋值给变量name	
		float a = stringToNum<float>(name);
		string age = Trim(fields[1]); //清除掉向量fields中第二个元素的无效字符,并赋值给变量age

		balance[i] = a;
		i = i + 1;
	}
	float *p = new  float[i];
	int j = 0;
	for (int i = 0; i <= 1000; i++)
	{
		if (balance[i] == (double)0)
		{
			break;
		}
		p[j] = balance[i];
		cout << "j=" << j << "值=" << p[j] << endl;
		j = j + 1;

	}
	bubble(p, i);
	int z;
	for (z = 0; z <= 1000; z++)
	{
		if (p[z] < a)
		{

		}
		else
		{
			break;
		}
	}
	double rate = double(z / j);
	double z1 = z;
	double j1 = j;
	return (double)(z1 / j1);
}
double  __stdcall sum(double a, const char*  path)
{

	// 读文件
	ifstream fin("D://导出数据.csv", ios::in);
	string line;
	vector<vector<string>> strArray;
	double i = 0, j = 0;
	while (getline(fin, line))
	{
		istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
		vector<string> fields; //声明一个字符串向量
		string field;
		while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
		{
			fields.push_back(field); //将刚刚读取的字符串添加到向量fields中
		}
		string name = Trim(fields[0]); //清除掉向量fields中第一个元素的无效字符,并赋值给变量name	
		string age = Trim(fields[1]); //清除掉向量fields中第二个元素的无效字符,并赋值给变量age
		cout << name << "------" << age << endl;
		if (Trim(fields[1]) == "合格" and Trim(fields[2]) == "合格")

		{
			i = i + 1;
		}
		j = j + 1;
	}
	double z = i / (j - 1);	
	return z;
}