
作者 | Trent1985
来源 | CSDN博客



/*************************************************
Copyright: Copyright Trent.
Author: Trent
Date: 2015-03-09
Description:MTCNN.
**************************************************/
static char* model_path = "C:/Users/Administrator/Desktop/mtcnn/001_TestDemo/TestDemo/TestDemo_C/models";
MTCNN* mtcnn;
int FD_Initialize()
{
mtcnn = new MTCNN(model_path);
return 0;
};
int FD_Process(unsigned char* srcData, int width, int height, int stride, int faceInfos[15])
{
unsigned char* data = (unsigned char*)malloc(sizeof(unsigned char) * height * width * 3);
unsigned char* pSrc = srcData;
unsigned char* pData = data;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
pData[0] = pSrc[0];
pData[1] = pSrc[1];
pData[2] = pSrc[2];
pData += 3;
pSrc += 4;
}
}
ncnn::Mat ncnn_img = ncnn::Mat::from_pixels(data, ncnn::Mat::PIXEL_RGB, width, height);
std::vector<Bbox> finalBbox;
mtcnn->detect(ncnn_img, finalBbox);
if(finalBbox.size() > 0)
{
faceInfos[0] = 1;
faceInfos[1] = finalBbox[0].x1;
faceInfos[2] = finalBbox[0].y1;
faceInfos[3] = finalBbox[0].x2 - finalBbox[0].x1;
faceInfos[4] = finalBbox[0].y2 - finalBbox[0].y1;
faceInfos[5] = finalBbox[0].ppoint[0];
faceInfos[6] = finalBbox[0].ppoint[5];
faceInfos[7] = finalBbox[0].ppoint[1];
faceInfos[8] = finalBbox[0].ppoint[6];
faceInfos[9] = finalBbox[0].ppoint[2];
faceInfos[10] = finalBbox[0].ppoint[7];
faceInfos[11] = finalBbox[0].ppoint[3];
faceInfos[12] = finalBbox[0].ppoint[8];
faceInfos[13] = finalBbox[0].ppoint[4];
faceInfos[14] = finalBbox[0].ppoint[9];
}
free(data);
return 0;
};
void FD_Unitialize()
{
if(model_path != NULL)
free(model_path);
delete(mtcnn);
}
A为左眼中心点
B为右眼中心点
C为嘴巴水平中心点



void GetTexTransMatrix(float x1, float y1, float x2, float y2, float x3, float y3,float tx1, float ty1, float tx2, float ty2, float tx3, float ty3, float*texMatrix)
{
float detA;
detA = tx1*ty2 + tx2*ty3 + tx3*ty1 - tx3*ty2 - tx1*ty3 - tx2*ty1;
float A11, A12, A13, A21, A22, A23, A31, A32, A33;
A11 = ty2 - ty3;
A21 = -(ty1 - ty3);
A31 = ty1 - ty2;
A12 = -(tx2 - tx3);
A22 = tx1 - tx3;
A32 = -(tx1 - tx2);
A13 = tx2*ty3 - tx3*ty2;
A23 = -(tx1*ty3 - tx3*ty1);
A33 = tx1*ty2 - tx2*ty1;
texMatrix[0] = (x1*A11 + x2*A21 + x3*A31) / detA;
texMatrix[1] = (x1*A12 + x2*A22 + x3*A32) / detA;
texMatrix[2] = (x1*A13 + x2*A23 + x3*A33) / detA;
texMatrix[3] = (y1*A11 + y2*A21 + y3*A31) / detA;
texMatrix[4] = (y1*A12 + y2*A22 + y3*A32) / detA;
texMatrix[5] = (y1*A13 + y2*A23 + y3*A33) / detA;

void GetTexTransMatrix(float x1, float y1, float x2, float y2, float x3, float y3,float tx1, float ty1, float tx2, float ty2, float tx3, float ty3, float*texMatrix)
{
float detA;
detA = tx1*ty2 + tx2*ty3 + tx3*ty1 - tx3*ty2 - tx1*ty3 - tx2*ty1;
float A11, A12, A13, A21, A22, A23, A31, A32, A33;
A11 = ty2 - ty3;
A21 = -(ty1 - ty3);
A31 = ty1 - ty2;
A12 = -(tx2 - tx3);
A22 = tx1 - tx3;
A32 = -(tx1 - tx2);
A13 = tx2*ty3 - tx3*ty2;
A23 = -(tx1*ty3 - tx3*ty1);
A33 = tx1*ty2 - tx2*ty1;
texMatrix[0] = (x1*A11 + x2*A21 + x3*A31) / detA;
texMatrix[1] = (x1*A12 + x2*A22 + x3*A32) / detA;
texMatrix[2] = (x1*A13 + x2*A23 + x3*A33) / detA;
texMatrix[3] = (y1*A11 + y2*A21 + y3*A31) / detA;
texMatrix[4] = (y1*A12 + y2*A22 + y3*A32) / detA;
texMatrix[5] = (y1*A13 + y2*A23 + y3*A33) / detA;
}
int Trent_Sticker(unsigned char* srcData, int width, int height, int stride, unsigned char* mask, int maskWidth, int maskHeight, int maskStride, int srcFacePoints[6], int maskFacePoints[6], int ratio)
{
int ret = 0;
float H[6];
GetTexTransMatrix(maskFacePoints[0], maskFacePoints[1], maskFacePoints[2], maskFacePoints[3], maskFacePoints[4], maskFacePoints[5], srcFacePoints[0], srcFacePoints[1], srcFacePoints[2], srcFacePoints[3], srcFacePoints[4], srcFacePoints[5], H);
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
float x = (float)i;
float y = (float)j;
float tx = 0;
float ty = 0;
tx = (int)((H[0] * (x)+H[1] * (y)+H[2]) + 0.5);
ty = (int)((H[3] * (x)+H[4] * (y)+H[5]) + 0.5);
tx = CLIP3(tx, 0, maskWidth - 1);
ty = CLIP3(ty, 0, maskHeight - 1);
int mb = mask[(int)tx * 4 + (int)ty * maskStride];
int mg = mask[(int)tx * 4 + (int)ty * maskStride + 1];
int mr = mask[(int)tx * 4 + (int)ty * maskStride + 2];
int alpha = mask[(int)tx * 4 + (int)ty * maskStride + 3];
int b = srcData[i * 4 + j * stride];
int g = srcData[i * 4 + j * stride + 1];
int r = srcData[i * 4 + j * stride + 2];
srcData[(int)i * 4 + (int)j * stride] = CLIP3((b * (255 - alpha) + mb * alpha) / 255, 0, 255);
srcData[(int)i * 4 + (int)j * stride + 1] = CLIP3((g * (255 - alpha) + mg * alpha) / 255, 0, 255);
srcData[(int)i * 4 + (int)j * stride + 2] = CLIP3((r * (255 - alpha) + mr * alpha) / 255, 0, 255);
}
}
return ret;
};

扫码查看原文
▼▼▼
(*本文为AI科技大本营原创文章,转载请微信联系 1092722531)
◆
精彩推荐
◆
2019 中国大数据技术大会(BDTC)再度来袭!豪华主席阵容及百位技术专家齐聚,15 场精选专题技术和行业论坛,超强干货+技术剖析+行业实践立体解读,深入解析热门技术在行业中的实践落地。6.6 折票限时特惠(立减1400元),学生票仅 599 元!