Exemplo n.º 1
0
        protected void BSpline()
        {
            if (pictureBoxPreview.Image == null)
            {
                return;
            }

            int nx = (int)numericUpDownIntervalColumn.Value;
            int ny = (int)numericUpDownIntervalRow.Value;

            int X = pictureBoxPreview.Image.Size.Width;
            int Y = pictureBoxPreview.Image.Size.Height;

            Algorithms.Interpolation interpRed   = null;
            Algorithms.Interpolation interpGreen = null;
            Algorithms.Interpolation interpBlue  = null;

            double[][][] mats = bitmapToMat(paddedRawImage);

            if (radioButtonNearestNeighbor.Checked)
            {
                interpRed   = new Algorithms.NearestNeighborInterpolation(mats[0]);
                interpGreen = new Algorithms.NearestNeighborInterpolation(mats[1]);
                interpBlue  = new Algorithms.NearestNeighborInterpolation(mats[2]);
            }

            if (radioButtonBilinear.Checked)
            {
                interpRed   = new Algorithms.BilinearInterpolation(mats[0]);
                interpGreen = new Algorithms.BilinearInterpolation(mats[1]);
                interpBlue  = new Algorithms.BilinearInterpolation(mats[2]);
            }

            if (radioButtonBicubic.Checked)
            {
                interpRed   = new Algorithms.BicubicInterpolation(mats[0]);
                interpGreen = new Algorithms.BicubicInterpolation(mats[1]);
                interpBlue  = new Algorithms.BicubicInterpolation(mats[2]);
            }

            UInt32[][] mat = new UInt32[Y][];
            for (int k = 0; k < Y; k++)
            {
                mat[k] = new UInt32[X];
            }

            Algorithms.BSplineBase bSplineBase = null;
            int order = (int)numericUpDownBSplineOrder.Value;

            switch (order)
            {
            case 1:
                bSplineBase = new Algorithms.LinearBSplineBase();
                break;

            case 3:
                bSplineBase = new Algorithms.CubicBSplineBase();
                break;
            }

            this.progressBar.Value = 0;

            for (int x = 0; x < X; x++)
            {
                if (x % (X / 10) == 0)
                {
                    this.progressBar.Value = Math.Min(100, progressBar.Value + 10);
                }

                for (int y = 0; y < Y; y++)
                {
                    double currX = x;
                    double currY = y;

                    const double eps          = 1e-2;
                    const int    maxIteration = 10;

                    double disX = 0;
                    double disY = 0;

                    double prevDisX = 0;
                    double prevDisY = 0;

                    for (int count = 0; count < maxIteration; count++)
                    {
                        double u = (currX / nx) - Math.Floor(currX / nx);
                        double v = (currY / ny) - Math.Floor(currY / ny);

                        int i = (int)Math.Floor(currX / nx) - (order / 2);
                        int j = (int)Math.Floor(currY / ny) - (order / 2);

                        disX = 0;
                        disY = 0;

                        for (int l = 0; l <= order; l++)
                        {
                            for (int m = 0; m <= order; m++)
                            {
                                double t1 = bSplineBase.CalcBase(l, u);
                                double t2 = bSplineBase.CalcBase(m, v);

                                double t3 = 0;
                                double t4 = 0;
                                if (i + l >= 0 && i + l < controlPoints.GetLength(1) &&
                                    j + m >= 0 && j + m < controlPoints.GetLength(0))
                                {
                                    t3 = controlPoints[j + m, i + l].getDisplacement().X;
                                    t4 = controlPoints[j + m, i + l].getDisplacement().Y;
                                }

                                disX += t1 * t2 * t3;
                                disY += t1 * t2 * t4;
                            }
                        }

                        currX = Math.Max(0, Math.Min(X - 1, x - disX));
                        currY = Math.Max(0, Math.Min(Y - 1, y - disY));

                        if (Math.Max(Math.Abs(disX - prevDisX), Math.Abs(disY - prevDisY)) < eps)
                        {
                            break;
                        }
                        else
                        {
                            prevDisX = disX;
                            prevDisY = disY;
                        }
                    }

                    double red   = interpRed.FromMatrix((int)currY, (int)currX);
                    double green = interpGreen.FromMatrix((int)currY, (int)currX);
                    double blue  = interpBlue.FromMatrix((int)currY, (int)currX);

                    int r = Math.Max(0, Math.Min(255, (int)red));
                    int g = Math.Max(0, Math.Min(255, (int)green));
                    int b = Math.Max(0, Math.Min(255, (int)blue));

                    mat[y][x] = (UInt32)((0xff << 24) | (r << 16) | (g << 8) | (b << 0));
                }
            }

            pictureBoxPreview.Image = new Bitmap(matToBitmap(mat));

            this.progressBar.Value = 100;
        }
Exemplo n.º 2
0
        protected override void updateImage()
        {
            if (rawImage == null)
            {
                return;
            }

            double rho   = ((double)this.hScrollBarRho.Value / this.hScrollBarRho.Maximum) * 10 * Math.PI;
            double phi   = ((double)this.hScrollBarPhi.Value / this.hScrollBarPhi.Maximum) * Math.PI;
            double theta = ((double)this.hScrollBarTheta.Value / this.hScrollBarTheta.Maximum);

            Bitmap image     = new Bitmap(rawImage);
            int    height    = image.Height;
            int    width     = image.Width;
            double R         = Math.Min(height, width) / 2.0;
            double midHeight = height / 2.0;
            double midWidth  = width / 2.0;

            double[][][] mats = bitmapToMat(image);

            Algorithms.Interpolation interpRed   = null;
            Algorithms.Interpolation interpGreen = null;
            Algorithms.Interpolation interpBlue  = null;

            if (radioButtonNearestNeighbor.Checked)
            {
                interpRed   = new Algorithms.NearestNeighborInterpolation(mats[0]);
                interpGreen = new Algorithms.NearestNeighborInterpolation(mats[1]);
                interpBlue  = new Algorithms.NearestNeighborInterpolation(mats[2]);
            }

            if (radioButtonBilinear.Checked)
            {
                interpRed   = new Algorithms.BilinearInterpolation(mats[0]);
                interpGreen = new Algorithms.BilinearInterpolation(mats[1]);
                interpBlue  = new Algorithms.BilinearInterpolation(mats[2]);
            }

            if (radioButtonBicubic.Checked)
            {
                interpRed   = new Algorithms.BicubicInterpolation(mats[0]);
                interpGreen = new Algorithms.BicubicInterpolation(mats[1]);
                interpBlue  = new Algorithms.BicubicInterpolation(mats[2]);
            }

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    double r     = Math.Sqrt((i - midHeight) * (i - midHeight) + (j - midWidth) * (j - midWidth));
                    double alpha = Math.Atan2(j - midWidth, i - midHeight);

                    alpha -= theta * Math.Sin(r * rho / R + phi);

                    double x = midHeight + r * Math.Cos(alpha);
                    double y = midWidth + r * Math.Sin(alpha);

                    int red   = (int)Math.Round(interpRed.FromMatrix(x, y));
                    int green = (int)Math.Round(interpGreen.FromMatrix(x, y));
                    int blue  = (int)Math.Round(interpBlue.FromMatrix(x, y));

                    red   = Math.Max(0, Math.Min(0xff, red));
                    green = Math.Max(0, Math.Min(0xff - 1, green));
                    blue  = Math.Max(0, Math.Min(0xff - 1, blue));

                    int argb = (0xFF << 24) | (red << 16) | (green << 8) | blue;

                    image.SetPixel(j, i, Color.FromArgb(argb));
                }
            }
            pictureBoxPreview.Image = image;
        }
Exemplo n.º 3
0
        protected override void updateImage()
        {
            if (rawImage == null)
            {
                return;
            }

            double theta     = ((double)this.hScrollBarTheta.Value / this.hScrollBarTheta.Maximum) * 2 * Math.PI;
            Bitmap image     = new Bitmap(rawImage);
            int    height    = image.Height;
            int    width     = image.Width;
            double R         = Math.Min(height, width) / 2.0;
            double midHeight = height / 2.0;
            double midWidth  = width / 2.0;

            double[][][] mats = bitmapToMat(image);

            Algorithms.Interpolation interpRed   = null;
            Algorithms.Interpolation interpGreen = null;
            Algorithms.Interpolation interpBlue  = null;

            if (radioButtonNearestNeighbor.Checked)
            {
                interpRed   = new Algorithms.NearestNeighborInterpolation(mats[0]);
                interpGreen = new Algorithms.NearestNeighborInterpolation(mats[1]);
                interpBlue  = new Algorithms.NearestNeighborInterpolation(mats[2]);
            }

            if (radioButtonBilinear.Checked)
            {
                interpRed   = new Algorithms.BilinearInterpolation(mats[0]);
                interpGreen = new Algorithms.BilinearInterpolation(mats[1]);
                interpBlue  = new Algorithms.BilinearInterpolation(mats[2]);
            }

            if (radioButtonBicubic.Checked)
            {
                interpRed   = new Algorithms.BicubicInterpolation(mats[0]);
                interpGreen = new Algorithms.BicubicInterpolation(mats[1]);
                interpBlue  = new Algorithms.BicubicInterpolation(mats[2]);
            }

            UInt32[][] argb = new UInt32[height][];
            for (int i = 0; i < height; i++)
            {
                argb[i] = new UInt32[width];
            }


            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    double r     = Math.Sqrt((i - midHeight) * (i - midHeight) + (j - midWidth) * (j - midWidth));
                    double alpha = Math.Atan2(j - midWidth, i - midHeight);
                    if (r < R)
                    {
                        alpha -= (theta * (R - r) / R);
                    }
                    double x = midHeight + r * Math.Cos(alpha);
                    double y = midWidth + r * Math.Sin(alpha);

                    int red   = (int)Math.Round(interpRed.FromMatrix(x, y));
                    int green = (int)Math.Round(interpGreen.FromMatrix(x, y));
                    int blue  = (int)Math.Round(interpBlue.FromMatrix(x, y));

                    red   = Math.Max(0, Math.Min(0xff, red));
                    green = Math.Max(0, Math.Min(0xff - 1, green));
                    blue  = Math.Max(0, Math.Min(0xff - 1, blue));

                    argb[i][j] = (UInt32)((0xFF << 24) | (red << 16) | (green << 8) | blue);
                }
            }
            pictureBoxPreview.Image = matToBitmap(argb);
        }