public FaceColors[,] GetFace(FaceColors face)
    {
        FaceColors[,] retValue = new FaceColors[_size, _size];

        for (int i = 0; i < _size; i++)
        {
            for (int j = 0; j < _size; j++)
            {
                retValue[i, j] = _faces[(int)face, i, j];
            }
        }

        return(retValue);
    }
    private List <FaceColors> GetNeighbors(FaceColors face)
    {
        switch (face)
        {
        case FaceColors.White:
            return(new List <FaceColors> {
                FaceColors.Orange, FaceColors.Blue, FaceColors.Red, FaceColors.Green
            });

        case FaceColors.Red:
            return(new List <FaceColors> {
                FaceColors.White, FaceColors.Blue, FaceColors.Yellow, FaceColors.Green
            });

        case FaceColors.Blue:
            return(new List <FaceColors> {
                FaceColors.White, FaceColors.Orange, FaceColors.Yellow, FaceColors.Red
            });

        case FaceColors.Orange:
            return(new List <FaceColors> {
                FaceColors.White, FaceColors.Green, FaceColors.Yellow, FaceColors.Blue
            });

        case FaceColors.Green:
            return(new List <FaceColors> {
                FaceColors.White, FaceColors.Red, FaceColors.Yellow, FaceColors.Yellow
            });

        case FaceColors.Yellow:
        default:
            return(new List <FaceColors> {
                FaceColors.Red, FaceColors.Blue, FaceColors.Orange, FaceColors.Green
            });
        }
    }
    public void RotateFace(FaceColors face, int slice)
    {
        FaceColors[,] tempFace = new FaceColors[_size, _size];

        for (int i = 0; i < _size; i++)
        {
            for (int j = 0; j < _size; j++)
            {
                tempFace[_size - 1 - i, _size - 1 - j] = _faces[(int)face, i, j];
            }
        }

        for (int i = 0; i < _size; i++)
        {
            for (int j = 0; j < _size; j++)
            {
                _faces[(int)face, i, j] = tempFace[i, j];
            }
        }

        List <FaceColors> neighbors         = GetNeighbors(face);
        List <Vector3Int> neighborLocations = new List <Vector3Int>();

        foreach (FaceColors neighbor in neighbors)
        {
            List <FaceColors> neighborNeighbors = GetNeighbors(neighbor);
            int ourIndex = neighborNeighbors.IndexOf(face);
            switch (ourIndex)
            {
            case 0:
                neighborLocations.Add(new Vector3Int(-1, 0, -1));
                break;

            case 1:
                neighborLocations.Add(new Vector3Int(_size - 1, -1, -1));
                break;

            case 2:
                neighborLocations.Add(new Vector3Int(-1, _size - 1, 1));
                break;

            default:
                neighborLocations.Add(new Vector3Int(0, -1, 1));
                break;
            }
        }

        for (int i = 0; i < _size; i++)
        {
            FaceColors[]      tempColors    = new FaceColors[4];
            List <Vector2Int> faceLocations = new List <Vector2Int>();
            for (int j = 0; j < neighbors.Count; j++)
            {
                Vector3Int neighborLocation = neighborLocations[i];
                Vector2Int faceLocation     = new Vector2Int();
                if (neighborLocation.x == -1)
                {
                    if (neighborLocation.z == 1)
                    {
                        faceLocation.x = i;
                    }
                    else
                    {
                        faceLocation.x = _size - 1 - i;
                    }

                    faceLocation.y = neighborLocation.y;
                }
                else
                {
                    if (neighborLocation.z == 1)
                    {
                        faceLocation.y = i;
                    }
                    else
                    {
                        faceLocation.x = _size - 1 - i;
                    }
                    faceLocation.x = neighborLocation.x;
                }
                faceLocations.Add(faceLocation);
            }
            Vector2Int lastFaceLocation = faceLocations[faceLocations.Count - 1];
            FaceColors lastFaceColor    = _faces[(int)neighbors[neighbors.Count - 1], lastFaceLocation.x, lastFaceLocation.y];
            for (int j = 0; j < faceLocations.Count; j++)
            {
                Vector2Int faceLocation  = faceLocations[j];
                FaceColors tempFaceColor = _faces[(int)neighbors[j], faceLocation.x, faceLocation.y];

                _faces[(int)neighbors[j], faceLocation.x, faceLocation.y] = lastFaceColor;
                lastFaceColor = tempFaceColor;
            }
        }
    }
Пример #4
0
        public static FaceColors Extract(Mat face, Guid jobId)
        {
            var faceId = Guid.NewGuid();

            var dir = new DirectoryInfo($"Results\\{jobId}\\Extracted\\Unique\\{faceId}");

            if (!dir.Exists)
            {
                dir.Create();
            }

            var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(face);

            bitmap.Save($"Results\\{jobId}\\Extracted\\Unique\\{faceId}\\face.png", ImageFormat.Png);
            bitmap.Save($"Results\\{jobId}\\Extracted\\Unique\\{faceId}.png", ImageFormat.Png);

            var result = new FaceColors();

            var subWidth  = face.Width / Config.FaceDimension;
            var subHeight = face.Height / Config.FaceDimension;

            var centerX = subWidth / 2;
            var centerY = subHeight / 2;

            for (int i = 0; i < Config.FaceDimension; i++)
            {
                for (int j = 0; j < Config.FaceDimension; j++)
                {
                    var sub = new Mat(face, new Rect(i * subWidth, j * subHeight, subWidth, subHeight));

                    //var colors = new List<Color>();
                    //for (int k = centerX - 20; k < centerX + 20; k++)
                    //    for (int p = centerY - 20; p < centerY + 20; p++)
                    //    {
                    //        var intensity = sub.At<Vec3b>(k, p);

                    //        byte blue = intensity.Item0;
                    //        byte green = intensity.Item1;
                    //        byte red = intensity.Item2;

                    //        var color1 = Color.FromArgb(red, green, blue);
                    //        color1 = ClosestColorRgb(color1);
                    //        colors.Add(color1);
                    //    }

                    //var color = colors.GroupBy(s => s)
                    //         .OrderByDescending(s => s.Count())
                    //         .First().Key;

                    var intensity = sub.At <Vec3b>(centerX, centerY);

                    byte blue  = intensity.Item0;
                    byte green = intensity.Item1;
                    byte red   = intensity.Item2;

                    var color = Color.FromArgb(red, green, blue);

                    var c1 = ClosestColorRgb(color);
                    var c2 = ClosestColorHue(color);
                    var c3 = ClosestColorHsb(color);

                    var list = new List <Color> {
                        c1, c2, c3
                    };
                    var max = list.GroupBy(s => s)
                              .OrderByDescending(s => s.Count())
                              .First().Key;

                    var newColor = FileColorExtractor.GetColor(FileColorExtractor.GetColor(color.ToHexString()));

                    //if (max == Color.Orange && newColor != Color.Orange)
                    //    newColor = Color.Orange;

                    result.SetColor(i.ToString() + "-" + j.ToString(), newColor);

                    bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(sub);
                    //bitmap.Save($"Results\\{jobId}\\Extracted\\Unique\\{faceId}\\sub-{i}-{j}({newColor.Name}).png", ImageFormat.Png);
                    bitmap.Save($"Results\\{jobId}\\Extracted\\Unique\\{faceId}\\sub-{i}-{j}({newColor.Name})({c1.Name}-{c2.Name}-{c3.Name})({color.R}-{color.G}-{color.B}).png", ImageFormat.Png);
                }
            }

            return(result);
        }