Advertisement
Guest User

GOWNO!

a guest
Jul 2nd, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.54 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include "Detection.h"
  4. #include <stdio.h>
  5. #include "ipp.h"
  6.  
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10.  
  11.  
  12. BOOL APIENTRY DllMain(HANDLE hModule,
  13.     DWORD  ul_reason_for_call,
  14.     LPVOID lpReserved
  15.     )
  16. {
  17.     return TRUE;
  18. }
  19.  
  20. bool Dilate(BYTE* pImg, int nW, int nH)
  21. {
  22.     int nSize = nW*nH;
  23.  
  24.     BYTE* pDilImg = new BYTE[nSize];
  25.     ::ZeroMemory(pDilImg, nSize);
  26.  
  27.     IppiSize ROI;
  28.     ROI.height = nH - 4;
  29.     ROI.width = nW - 4;
  30.  
  31.     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 };
  32.  
  33.     IppiSize MaskSize = { 5, 5 };
  34.     IppiPoint Anchor = { 2, 2 };
  35.     IppStatus S = ippiDilate_8u_C1R(pImg + 2 * nW + 2, nW, pDilImg + 2 * nW + 2, nW, ROI, pMask, MaskSize, Anchor);
  36.  
  37.     int p = 0;
  38.     int q = 0;
  39.     for (int i = 0; i < nSize; i++, q++)
  40.     {
  41.         if (q >= nW)
  42.         {
  43.             q = 0;
  44.             p++;
  45.         }
  46.         if (p > 2 && p < nH - 3 && q > 2 && q < nW - 4)
  47.             pImg[i] = pDilImg[i];
  48.         else
  49.             pImg[i] = 0;
  50.     }
  51.  
  52.     delete[] pDilImg;
  53.  
  54.     return S == ippStsNoErr;
  55. }
  56.  
  57. bool Erode(BYTE* pImg, int nW, int nH)
  58. {
  59.     int nSize = nW*nH;
  60.  
  61.     BYTE* pErodImg = new BYTE[nSize];
  62.     ::ZeroMemory(pErodImg, nSize);
  63.  
  64.     IppiSize ROI;
  65.     ROI.height = nH - 2;
  66.     ROI.width = nW - 2;
  67.  
  68.     IppStatus S = ippiErode3x3_8u_C1R(pImg + nW + 1, nW, pErodImg + nW + 1, nW, ROI);
  69.  
  70.     int p = 0;
  71.     int q = 0;
  72.     for (int i = 0; i < nSize; i++, q++)
  73.     {
  74.         if (q >= nW)
  75.         {
  76.             q = 0;
  77.             p++;
  78.         }
  79.         if (p > 2 && p < nH - 3 && q > 2 && q < nW - 4)
  80.             pImg[i] = pErodImg[i];
  81.         else
  82.             pImg[i] = 0;
  83.     }
  84.  
  85.     delete[] pErodImg;
  86.  
  87.     return S == ippStsNoErr;
  88. }
  89.  
  90.  
  91. /************************************************************************************/
  92. /************************************************************************************/
  93. /************************************************************************************/
  94. /************************************************************************************/
  95. /************************************************************************************/
  96.  
  97. double NormalizationMM(double* MM, int nW, int nH, BYTE* pDst)
  98. {
  99.     double min, max, calc;
  100.     /*********************Mouth Map****************************************************/
  101.     min = MM[0];
  102.     max = MM[0];
  103.  
  104.     for (int i = 0; i<(nW*nH); i++)
  105.     {
  106.         if (min>MM[i]){
  107.             min = MM[i];
  108.         }
  109.  
  110.         if (max<MM[i]){
  111.             max = MM[i];
  112.         }
  113.  
  114.  
  115.     }
  116.  
  117.     for (int i = 0; i<nW*nH; i++)
  118.     {
  119.         pDst[i] = (255 * ((MM[i] - min) / (max - min)));
  120.  
  121.     }
  122.  
  123.     /************************************************************************************/
  124.     Erode(pDst, nW, nH); //Erode and Dilate functions to remove false noise
  125.     Dilate(pDst, nW, nH);
  126.  
  127.     min = pDst[0];
  128.     max = pDst[0];
  129.  
  130.     for (int i = 0; i < nW*nH; i++)
  131.     {
  132.         if (pDst[i] > max){
  133.             max = pDst[i];
  134.         }
  135.         if (pDst[i] < min){
  136.             min = pDst[i];
  137.         }
  138.     }
  139.  
  140.     calc = min + (max - min) / 2;
  141.     for (int i = 0; i <nW*nH; i++)
  142.     {
  143.         if (pDst[i] > calc){
  144.             pDst[i] = 255;
  145.         }
  146.         else{
  147.             pDst[i] = 0;
  148.         }
  149.     }
  150.  
  151.     return true;
  152.  
  153.  
  154. }
  155.  
  156. double NormalizationEM(double* EM, int nW, int nH, BYTE* pDst)
  157. {
  158.     double min, max, calc;
  159.     //nH = nH/2;
  160.     /*********************Eye Map****************************************************/
  161.     min = EM[0];
  162.     max = EM[0];
  163.  
  164.     for (int i = 0; i<(nW*nH); i++)
  165.     {
  166.         if (min>EM[i]){
  167.             min = EM[i];
  168.         }
  169.  
  170.         if (max<EM[i]){
  171.             max = EM[i];
  172.         }
  173.  
  174.  
  175.     }
  176.  
  177.     for (int i = 0; i<nW*nH; i++)
  178.     {
  179.         pDst[i] = (255 * ((EM[i] - min) / (max - min)));
  180.  
  181.     }
  182.  
  183.     /************************************************************************************/
  184.     Erode(pDst, nW, nH); //Erode and Dilate functions to remove false noise
  185.     Dilate(pDst, nW, nH);
  186.  
  187.     min = pDst[0];
  188.     max = pDst[0];
  189.  
  190.     for (int i = 0; i < nW*nH; i++)
  191.     {
  192.         if (pDst[i] > max){
  193.             max = pDst[i];
  194.         }
  195.         if (pDst[i] < min){
  196.             min = pDst[i];
  197.         }
  198.     }
  199.  
  200.     calc = min + (max - min) / 2;
  201.     for (int i = 0; i <nW*nH; i++)
  202.     {
  203.         if (pDst[i] > calc){
  204.             pDst[i] = 255;
  205.         }
  206.         else{
  207.             pDst[i] = 0;
  208.         }
  209.     }
  210.  
  211.     return true;
  212.  
  213.  
  214. }
  215.  
  216. __declspec(dllexport) bool __cdecl MouthMap(BYTE* pY, BYTE* pC_b, BYTE* pC_r, int nW, int nH, BYTE* pDst)
  217. {
  218.     double *MM;
  219.     MM = new double[nW*nH];
  220.     double sum1 = 0, sum2 = 0;
  221.  
  222.     for (int i = 0; i<nW*nH; i++) //average calculation
  223.     {
  224.         sum1 += (double)pC_r[i] * (double)pC_r[i]; //avg(Cr)
  225.         sum2 += (double)pC_r[i] / (double)pC_b[i]; //avg(Cr/Cb)
  226.     }
  227.  
  228.     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)).
  229.  
  230.  
  231.     for (int i = 0; i<nW*nH; i++)
  232.     {
  233.         double Cb = (double)pC_b[i];
  234.         double Cr = (double)pC_r[i];
  235.         MM[i] = (Cr*Cr)*(Cr*Cr - n*(Cr / Cb))*(Cr*Cr - n*(Cr / Cb)); //Mouth Map
  236.     }
  237.  
  238.     NormalizationMM(MM, nW, nH, pDst);                                        //Normalizating function for Mouth Map to achieve final effect
  239.     return true;
  240. }
  241.  
  242. __declspec(dllexport) bool __cdecl EyeMap(BYTE* pY, BYTE* pC_b, BYTE* pC_r, int nW, int nH, BYTE* pDst)
  243. {
  244.     //variables
  245.     double EMc, EMl, Cb, Cr; //where EMc - chrominance eye map, Eml - luminance eye map , Cb, Cr - color channels
  246.     double *EM = new double[nW*nH]; // final eye map
  247.     BYTE *Ydil = new BYTE[nW*nH]; //dilated channel
  248.     BYTE *Yer = new BYTE[nW*nH]; //eroded channel
  249.  
  250.     for (int i = 0; i<nW*nH; i++)
  251.     {
  252.         Cb = pC_b[i];
  253.         Cr = pC_r[i];
  254.         Ydil[i] = pY[i];
  255.         Yer[i] = pY[i];
  256.     }
  257.  
  258.     Erode(Yer, nW, nH);
  259.     Dilate(Ydil, nW, nH);
  260.  
  261.     for (int i = 0; i<nW*nH; i++)
  262.     {
  263.         double EMc = (1.0 / 3.0)*((Cb*Cb) + ((255.0 - Cr)*(255.0 - Cr)) + (Cb / Cr));
  264.         EMl = (Ydil[i]) / (Yer[i] + 1);
  265.         EM[i] = (EMc*EMl);
  266.     }
  267.  
  268.     NormalizationEM(EM, nW, nH, pDst);
  269.     return true;
  270. }
  271. int GetMax(int dArray[], int iSize) {
  272.     int iCurrMax = 0;
  273.     for (int i = 1; i < iSize; ++i) {
  274.         if (dArray[iCurrMax] > dArray[i]) {
  275.             iCurrMax = i;
  276.         }
  277.     }
  278.     return dArray[iCurrMax];
  279. }
  280.  
  281. int GetMin(int dArray[], int iSize) {
  282.     int iCurrMin = 0;
  283.     for (int i = 1; i < iSize; ++i) {
  284.         if (dArray[iCurrMin] < dArray[i]) {
  285.             iCurrMin = i;
  286.         }
  287.     }
  288.     return dArray[iCurrMin];
  289. }
  290. __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)
  291. {
  292.     /*
  293.     mouth position detection.
  294.     */
  295.     int leftXM, rightXM, upYM, downYM;
  296.     int leftt[65 * 75];
  297.     int rightt[65 * 75];
  298.  
  299.     for (int i = 0; i<nW*nH; i++){
  300.         leftt[i] = rightt[i] = 0;
  301.     }
  302.  
  303.     int imp1 = 0, imp2 = 0;//kiedy znajdzie pierwszy piksel
  304.     //zrobic zmienna min i wywalic lefftt
  305.     for (int i = 0; i<nW*nH; i++)
  306.     if (pMouthMap[i] != 0){
  307.         leftt[i] = i / nH; // i % nW
  308.         if (imp1 == 0) {
  309.             upYM = i / nW;
  310.             imp1 = 1;
  311.         }
  312.         //break;
  313.     }
  314.     leftXM = GetMin(leftt, nW*nH);
  315.  
  316.     for (int i = nW*nH - 1; i >= 0; i--){
  317.         if (pMouthMap[i] != 0){
  318.             rightt[i] = i / nH;
  319.             if (imp2 == 0) { downYM = i / nW; imp2 = 1; }
  320.             //break;
  321.         }
  322.     }
  323.     rightXM = GetMax(rightt, nW*nH);
  324.  
  325.  
  326.     nM_X = leftXM + rightXM;
  327.     nM_X /= 2;
  328.  
  329.     nM_Y = upYM + downYM;
  330.     nM_Y /= 2;
  331.  
  332.     /*
  333.     mouth position detection ending.
  334.     */
  335.  
  336.  
  337.     /*
  338.     eyes position detection.
  339.     */
  340.  
  341.     int x1 = 0, x2 = 0, y1 = 0, y2 = 0, counter1 = 0, counter2 = 0;
  342.     //musimy dodac bo jest zjebane i wykrywa usta i JEBANY NOSEK
  343.     for (int i = 0; i< nW * nH / 2 ; i++){
  344.         if (pEyeMap[i] == 255){
  345.             if (i % nW < nW / 2){
  346.                 x1 += i % nW;//biore cala linie i dziele przez counter srednia
  347.                 y1 += i / nW;
  348.                 counter1++;
  349.             } else {
  350.                 x2 += i % nW;
  351.                 y2 += i / nW;
  352.                 counter2++;
  353.             }
  354.         }
  355.        
  356.     }
  357.  
  358.     if (counter1 != 0) nLE_X = x1 / counter1;
  359.     if (counter1 != 0) nLE_Y = y1 / counter1;
  360.  
  361.     if (counter2 != 0) nRE_X = x2 / counter2;
  362.     if (counter2 != 0) nRE_Y = y2 / counter2;
  363.     return true;
  364.     /*
  365.     eyes position detection ending.
  366.     */
  367.  
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement