Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "Detection.h"
- #include <stdio.h>
- #include "ipp.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- BOOL APIENTRY DllMain(HANDLE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- return TRUE;
- }
- bool Dilate(BYTE* pImg, int nW, int nH)
- {
- int nSize = nW*nH;
- BYTE* pDilImg = new BYTE[nSize];
- ::ZeroMemory(pDilImg, nSize);
- IppiSize ROI;
- ROI.height = nH - 4;
- ROI.width = nW - 4;
- const Ipp8u pMask[25] = { 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0 };
- IppiSize MaskSize = { 5, 5 };
- IppiPoint Anchor = { 2, 2 };
- IppStatus S = ippiDilate_8u_C1R(pImg + 2 * nW + 2, nW, pDilImg + 2 * nW + 2, nW, ROI, pMask, MaskSize, Anchor);
- int p = 0;
- int q = 0;
- for (int i = 0; i < nSize; i++, q++)
- {
- if (q >= nW)
- {
- q = 0;
- p++;
- }
- if (p > 2 && p < nH - 3 && q > 2 && q < nW - 4)
- pImg[i] = pDilImg[i];
- else
- pImg[i] = 0;
- }
- delete[] pDilImg;
- return S == ippStsNoErr;
- }
- bool Erode(BYTE* pImg, int nW, int nH)
- {
- int nSize = nW*nH;
- BYTE* pErodImg = new BYTE[nSize];
- ::ZeroMemory(pErodImg, nSize);
- IppiSize ROI;
- ROI.height = nH - 2;
- ROI.width = nW - 2;
- IppStatus S = ippiErode3x3_8u_C1R(pImg + nW + 1, nW, pErodImg + nW + 1, nW, ROI);
- int p = 0;
- int q = 0;
- for (int i = 0; i < nSize; i++, q++)
- {
- if (q >= nW)
- {
- q = 0;
- p++;
- }
- if (p > 2 && p < nH - 3 && q > 2 && q < nW - 4)
- pImg[i] = pErodImg[i];
- else
- pImg[i] = 0;
- }
- delete[] pErodImg;
- return S == ippStsNoErr;
- }
- /************************************************************************************/
- /************************************************************************************/
- /************************************************************************************/
- /************************************************************************************/
- /************************************************************************************/
- double NormalizationMM(double* MM, int nW, int nH, BYTE* pDst)
- {
- double min, max, calc;
- /*********************Mouth Map****************************************************/
- min = MM[0];
- max = MM[0];
- for (int i = 0; i<(nW*nH); i++)
- {
- if (min>MM[i]){
- min = MM[i];
- }
- if (max<MM[i]){
- max = MM[i];
- }
- }
- for (int i = 0; i<nW*nH; i++)
- {
- pDst[i] = (255 * ((MM[i] - min) / (max - min)));
- }
- /************************************************************************************/
- Erode(pDst, nW, nH); //Erode and Dilate functions to remove false noise
- Dilate(pDst, nW, nH);
- min = pDst[0];
- max = pDst[0];
- for (int i = 0; i < nW*nH; i++)
- {
- if (pDst[i] > max){
- max = pDst[i];
- }
- if (pDst[i] < min){
- min = pDst[i];
- }
- }
- calc = min + (max - min) / 2;
- for (int i = 0; i <nW*nH; i++)
- {
- if (pDst[i] > calc){
- pDst[i] = 255;
- }
- else{
- pDst[i] = 0;
- }
- }
- return true;
- }
- double NormalizationEM(double* EM, int nW, int nH, BYTE* pDst)
- {
- double min, max, calc;
- //nH = nH/2;
- /*********************Eye Map****************************************************/
- min = EM[0];
- max = EM[0];
- for (int i = 0; i<(nW*nH); i++)
- {
- if (min>EM[i]){
- min = EM[i];
- }
- if (max<EM[i]){
- max = EM[i];
- }
- }
- for (int i = 0; i<nW*nH; i++)
- {
- pDst[i] = (255 * ((EM[i] - min) / (max - min)));
- }
- /************************************************************************************/
- Erode(pDst, nW, nH); //Erode and Dilate functions to remove false noise
- Dilate(pDst, nW, nH);
- min = pDst[0];
- max = pDst[0];
- for (int i = 0; i < nW*nH; i++)
- {
- if (pDst[i] > max){
- max = pDst[i];
- }
- if (pDst[i] < min){
- min = pDst[i];
- }
- }
- calc = min + (max - min) / 2;
- for (int i = 0; i <nW*nH; i++)
- {
- if (pDst[i] > calc){
- pDst[i] = 255;
- }
- else{
- pDst[i] = 0;
- }
- }
- return true;
- }
- __declspec(dllexport) bool __cdecl MouthMap(BYTE* pY, BYTE* pC_b, BYTE* pC_r, int nW, int nH, BYTE* pDst)
- {
- double *MM;
- MM = new double[nW*nH];
- double sum1 = 0, sum2 = 0;
- for (int i = 0; i<nW*nH; i++) //average calculation
- {
- sum1 += (double)pC_r[i] * (double)pC_r[i]; //avg(Cr)
- sum2 += (double)pC_r[i] / (double)pC_b[i]; //avg(Cr/Cb)
- }
- double n = 0.95*((sum1) / (nW*nH)) / ((sum2) / (nW*nH)); //5th equation, the ratio of average Cr^2 value (avg(Cr)) to the average Cr/Cb value (avg(Cr/Cb)).
- for (int i = 0; i<nW*nH; i++)
- {
- double Cb = (double)pC_b[i];
- double Cr = (double)pC_r[i];
- MM[i] = (Cr*Cr)*(Cr*Cr - n*(Cr / Cb))*(Cr*Cr - n*(Cr / Cb)); //Mouth Map
- }
- NormalizationMM(MM, nW, nH, pDst); //Normalizating function for Mouth Map to achieve final effect
- return true;
- }
- __declspec(dllexport) bool __cdecl EyeMap(BYTE* pY, BYTE* pC_b, BYTE* pC_r, int nW, int nH, BYTE* pDst)
- {
- //variables
- double EMc, EMl, Cb, Cr; //where EMc - chrominance eye map, Eml - luminance eye map , Cb, Cr - color channels
- double *EM = new double[nW*nH]; // final eye map
- BYTE *Ydil = new BYTE[nW*nH]; //dilated channel
- BYTE *Yer = new BYTE[nW*nH]; //eroded channel
- for (int i = 0; i<nW*nH; i++)
- {
- Cb = pC_b[i];
- Cr = pC_r[i];
- Ydil[i] = pY[i];
- Yer[i] = pY[i];
- }
- Erode(Yer, nW, nH);
- Dilate(Ydil, nW, nH);
- for (int i = 0; i<nW*nH; i++)
- {
- double EMc = (1.0 / 3.0)*((Cb*Cb) + ((255.0 - Cr)*(255.0 - Cr)) + (Cb / Cr));
- EMl = (Ydil[i]) / (Yer[i] + 1);
- EM[i] = (EMc*EMl);
- }
- NormalizationEM(EM, nW, nH, pDst);
- return true;
- }
- int GetMax(int dArray[], int iSize) {
- int iCurrMax = 0;
- for (int i = 1; i < iSize; ++i) {
- if (dArray[iCurrMax] > dArray[i]) {
- iCurrMax = i;
- }
- }
- return dArray[iCurrMax];
- }
- int GetMin(int dArray[], int iSize) {
- int iCurrMin = 0;
- for (int i = 1; i < iSize; ++i) {
- if (dArray[iCurrMin] < dArray[i]) {
- iCurrMin = i;
- }
- }
- return dArray[iCurrMin];
- }
- __declspec(dllexport) bool __cdecl Detect(BYTE* pEyeMap, BYTE* pMouthMap, int nW, int nH, int& nLE_X, int& nLE_Y, int& nRE_X, int& nRE_Y, int& nM_X, int& nM_Y)
- {
- /*
- mouth position detection.
- */
- int leftXM, rightXM, upYM, downYM;
- int leftt[65 * 75];
- int rightt[65 * 75];
- for (int i = 0; i<nW*nH; i++){
- leftt[i] = rightt[i] = 0;
- }
- int imp1 = 0, imp2 = 0;//kiedy znajdzie pierwszy piksel
- //zrobic zmienna min i wywalic lefftt
- for (int i = 0; i<nW*nH; i++)
- if (pMouthMap[i] != 0){
- leftt[i] = i / nH; // i % nW
- if (imp1 == 0) {
- upYM = i / nW;
- imp1 = 1;
- }
- //break;
- }
- leftXM = GetMin(leftt, nW*nH);
- for (int i = nW*nH - 1; i >= 0; i--){
- if (pMouthMap[i] != 0){
- rightt[i] = i / nH;
- if (imp2 == 0) { downYM = i / nW; imp2 = 1; }
- //break;
- }
- }
- rightXM = GetMax(rightt, nW*nH);
- nM_X = leftXM + rightXM;
- nM_X /= 2;
- nM_Y = upYM + downYM;
- nM_Y /= 2;
- /*
- mouth position detection ending.
- */
- /*
- eyes position detection.
- */
- int x1 = 0, x2 = 0, y1 = 0, y2 = 0, counter1 = 0, counter2 = 0;
- //musimy dodac bo jest zjebane i wykrywa usta i JEBANY NOSEK
- for (int i = 0; i< nW * nH / 2 ; i++){
- if (pEyeMap[i] == 255){
- if (i % nW < nW / 2){
- x1 += i % nW;//biore cala linie i dziele przez counter srednia
- y1 += i / nW;
- counter1++;
- } else {
- x2 += i % nW;
- y2 += i / nW;
- counter2++;
- }
- }
- }
- if (counter1 != 0) nLE_X = x1 / counter1;
- if (counter1 != 0) nLE_Y = y1 / counter1;
- if (counter2 != 0) nRE_X = x2 / counter2;
- if (counter2 != 0) nRE_Y = y2 / counter2;
- return true;
- /*
- eyes position detection ending.
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement