예제 #1
0
        public Image2DMatrix GetImageMatrix()
        {
            Image2DMatrix sourceMatrix = _imageLoader.GetImageMatrix();

            if (Quadrilateral != null)
            {
                //Bilinear transformations have 8 Parameter. These parameter can be obtained
                //by solving two linear equation systems. This will be done in the following.
                Quadrilateral targetQuadrilateral = GetTargetQuadrilateral();

                Matrix <double> m = GetmMatrix();
                Vector <double> a = GetaVector(targetQuadrilateral, m);
                Vector <double> b = GetbVector(targetQuadrilateral, m);

                double a0 = a[0];
                double a1 = a[1];
                double a2 = a[2];
                double a3 = a[3];

                double b0 = b[0];
                double b1 = b[1];
                double b2 = b[2];
                double b3 = b[3];

                return(Image2DMatrix.Transform(sourceMatrix, (x, y) =>
                {
                    x = (int)(a0 * x + a1 * y + a2 * x * y + a3);
                    y = (int)(b0 * x + b1 * y + b2 * x * y + b3);
                    return (x, y);
                }));
            }
            return(sourceMatrix);
        }
예제 #2
0
 /// <summary>
 /// Applies a function to every pixel of the image.
 /// </summary>
 /// <param name="sourceMatrix">The image which will provide the original pixel values.</param>
 /// <param name="targetMatrix">The image where the new values will be stored.</param>
 /// <param name="action">This function gets the position of the original pixel and returns a value for it.</param>
 /// /// <returns>The targetMatrix will be returned.</returns>
 public static Image2DMatrix Map(Image2DMatrix sourceMatrix, Image2DMatrix targetMatrix, Func <int, int, ushort> action)
 {
     for (int y = 0; y < sourceMatrix.Height; y++)
     {
         for (int x = 0; x < sourceMatrix.Width; x++)
         {
             targetMatrix[y, x] = action(x, y);
         }
     }
     return(targetMatrix);
 }
        /// <summary>
        /// Executes the image loading stack and applies transformations.
        /// Finally, the image matrix will be converted to a bitmap image.
        /// </summary>
        /// <returns></returns>
        public WriteableBitmap Build()
        {
            SetImageLoader();

            Image2DMatrix        imageMatrix          = _imageLoader.GetImageMatrix();
            TransformationMatrix transformationMatrix = GetTransformationMatrix(imageMatrix);

            imageMatrix = ApplyTransformationMatrix(imageMatrix, transformationMatrix);

            return(MatrixToBitmapImageConverter.GetImage(imageMatrix));
        }
예제 #4
0
        public static WriteableBitmap GetImage(Image2DMatrix imageMatrix)
        {
            if (imageMatrix != null)
            {
                int    height       = imageMatrix.Height;
                int    width        = imageMatrix.Width;
                int    bitsPerPixel = 8 * imageMatrix.BytePerPixel;
                byte[] imageBytes   = imageMatrix.GetBytes();

                WriteableBitmap bitmap = CreateWriteableBitmap(height, width, bitsPerPixel, imageBytes);
                //Must be freezed to share it between threads.
                bitmap.Freeze();

                return(bitmap);
            }
            return(null);
        }
        private Image2DMatrix ApplyTransformationMatrix(Image2DMatrix imageMatrix, TransformationMatrix transformationMatrix)
        {
            if (transformationMatrix != TransformationMatrix.UnitMatrix3x3)
            {
                if (SourceToTargetEnabled)
                {
                    imageMatrix = Image2DMatrix.Transform(imageMatrix, transformationMatrix);
                }
                else
                {
                    //With target to source enabled, the inverted transformation matrix is needed.
                    Image2DMatrix targetMatrix = new Image2DMatrix(TargetImageHeight, TargetImageWidth, imageMatrix.BytePerPixel);
                    imageMatrix = Image2DMatrix.TransformTargetToSource(imageMatrix, targetMatrix, transformationMatrix.Invert2D());
                }
            }

            return(imageMatrix);
        }
        private TransformationMatrix GetTransformationMatrix(Image2DMatrix imageMatrix)
        {
            if (imageMatrix != null)
            {
                //The transformations will be concatenated here. However, it is possible that
                //some combinations of parameters do need lead to correct results, because
                //transformations are not cumulative.
                TransformationMatrix transformationMatrix = TransformationMatrix.
                                                            UnitMatrix3x3.
                                                            Shear2D(Bx, By).
                                                            Scale2D(Sx, Sy).
                                                            Rotate2D(Alpha, imageMatrix.Width / 2, imageMatrix.Height / 2).
                                                            Project2D(SourceQuadrilateral);

                if (!SourceToTargetEnabled)
                {
                    transformationMatrix = transformationMatrix.Shift2D(Dx, Dy);
                }

                return(transformationMatrix);
            }
            return(TransformationMatrix.UnitMatrix3x3);
        }
예제 #7
0
 /// <summary>
 /// Applies a function to every pixel of the image.
 /// </summary>
 /// <param name="sourceMatrix">The image which will provide the original pixel values.</param>
 /// <param name="targetMatrix">The image where the new values will be stored.</param>
 /// <param name="action">This function gets the original pixel value and returns a new one.</param>
 /// <returns>The targetMatrix will be returned.</returns>
 public static Image2DMatrix Map(Image2DMatrix sourceMatrix, Image2DMatrix targetMatrix, Func <ushort, ushort> action)
 {
     return(Map(sourceMatrix, targetMatrix, (x, y) => action(sourceMatrix[y, x])));
 }
예제 #8
0
 /// <summary>
 /// Transforms each pixel position from the source image to a new position.
 /// </summary>
 /// <param name="sourceMatrix">The image which will provide the pixel values.</param>
 /// <param name="transformFunction">This function gets the original position and returns a new position.</param>
 public static Image2DMatrix Transform(Image2DMatrix sourceMatrix, Func <int, int, (int X, int Y)> transformFunction)