Exemplo n.º 1
0
        private static void RandomlyCropImage(Matrix <RgbPixel> inputImage, Matrix <ushort> labelImage, TrainingSample crop, Rand rnd)
        {
            var rect = MakeRandomCroppingRectResNet(inputImage, rnd);

            using (var chipDims = new ChipDims(227, 227))
                using (var chipDetails = new ChipDetails(rect, chipDims))
                {
                    // Crop the input image.
                    crop.InputImage = Dlib.ExtractImageChip <RgbPixel>(inputImage, chipDetails, InterpolationTypes.Bilinear);

                    // Crop the labels correspondingly. However, note that here bilinear
                    // interpolation would make absolutely no sense - you wouldn't say that
                    // a bicycle is half-way between an aeroplane and a bird, would you?
                    crop.LabelImage = Dlib.ExtractImageChip <ushort>(labelImage, chipDetails, InterpolationTypes.NearestNeighbor);

                    // Also randomly flip the input image and the labels.
                    if (rnd.GetRandomDouble() > 0.5)
                    {
                        var tmpInput = Dlib.FlipLR(crop.InputImage);
                        var tmpLabel = Dlib.FlipLR(crop.LabelImage);
                        crop.InputImage?.Dispose();
                        crop.LabelImage?.Dispose();
                        crop.InputImage = tmpInput;
                        crop.LabelImage = tmpLabel;
                    }

                    // And then randomly adjust the colors.
                    Dlib.ApplyRandomColorOffset(crop.InputImage, rnd);
                }
        }