Posted at

Unity + OpenCVで目の領域を拡大する(IndexがIncrementalじゃないのに対応); How to scale eye region in Unity + OpenCV

More than 1 year has passed since last update.


  • FaceTracker.csに追記

        private void ScalePoints(int startIndex, int endIndex, float scale)

{
int pointNum = endIndex - startIndex + 1;
int[] indexArray = new int[pointNum];
for (int i = startIndex; i <= endIndex; i++)
{
indexArray[i - startIndex] = i;
}
ScalePointsByIndexArray(indexArray, scale);
}

private void ScalePointsByIndexArray(int[] indexArray, float scale)
{
double xSum = 0;
double ySum = 0;
foreach (int i in indexArray)
{
xSum += points[0][i].x;
ySum += points[0][i].y;
}
double xMean = xSum / indexArray.Length;
double yMean = ySum / indexArray.Length;

// scale from mean
foreach (int i in indexArray)
{
double relativeX = points[0][i].x - xMean;
double relativeY = points[0][i].y - yMean;
relativeX *= scale;
relativeY *= scale;
points[0][i].x = relativeX + xMean;
points[0][i].y = relativeY + yMean;
}
}


  • 使いたい所で

            ScalePoints(37, 45, 1.4f); // nose

ScalePoints(48, 59, 1.4f); // mouse

int[] arr = { 27, 68, 28, 69, 29, 70, 30, 71 }; // left eye
ScalePointsByIndexArray(arr, 2.4f); // eye


  • なんでIndexが飛び飛びやねん!と思いながら対応した

  • 想定通り目の領域が拡大されることを確認した