예제 #1
0
 private static void Rotate(MagickImage output, UnperspectiveRotation rotation)
 {
     if (rotation != UnperspectiveRotation.None)
     {
         output.Rotate((int)rotation);
     }
 }
예제 #2
0
        /// <summary>
        /// Automatically remove pespective distortion from an image.
        /// </summary>
        /// <param name="input">The image to execute the script on.</param>
        /// <returns>The resulting image.</returns>
        public MagickImage Execute(MagickImage input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            CheckSettings();

            MagickGeometry inputDimensions = new MagickGeometry(input.Width, input.Height);
            MagickColor    backgroundColor = GetBorderColor(input);
            int            borderSize      = (int)((3 * Blur) + 5);

            var output = input.Clone();

            MagickGeometry trimmedDimensions = TrimImage(output, backgroundColor, borderSize);

            PadImage(output, borderSize);

            int xOffset;

            using (MagickImage paddedMask = CreatePaddedMask(output, out xOffset))
            {
                double      maxRad  = 0.5 * Hypot(output.Width, output.Height);
                MagickImage depolar = CreateDepolar(paddedMask, maxRad);

                ushort[] pixels = GetGrayChannel(depolar);

                List <PixelValue> maxList = GetPeaks(pixels, depolar);

                PointD[] corners = GetCorners(paddedMask, maxList, maxRad, xOffset);

                UnperspectiveRotation rotation = GetRotation(corners, output);

                Distort(output, corners, inputDimensions, trimmedDimensions, backgroundColor);
                Rotate(output, rotation);

                return(output);
            }
        }
예제 #3
0
        private static void GetRotation_Geometry10x10_ReturnsCorrectRotation(int x, int y, UnperspectiveRotation expectedRotation)
        {
            var script = new UnperspectiveScript();

            Type       type   = script.GetType();
            MethodInfo method = type.GetMethod("GetRotation", BindingFlags.Instance | BindingFlags.NonPublic);

            using (MagickImage image = new MagickImage(MagickColors.Fuchsia, 10, 10))
            {
                var result = method.Invoke(script, new object[] { new PointD[] { new PointD(x, y) }, image });
                Assert.AreEqual(expectedRotation, result);
            }
        }