Exemplo n.º 1
0
        private void AddRectanglePixelsInmatrix(ImagePixel[,] pixels, ImageRectangle rectangle, int xRectanglePosition, int yRectanglePosition)
        {
            ImagePixel[,] rectanglePixels = rectangle.PixelsMatrix;

            for (int m = 0, i = xRectanglePosition * rectangle.M; m < rectangle.M; m++, i++)
            {
                for (int n = 0, j = yRectanglePosition * rectangle.N; n < rectangle.N; n++, j++)
                {
                    pixels[i, j] = rectanglePixels[m, n];
                }
            }
        }
Exemplo n.º 2
0
        private ImageRectangle[] SplitIntoRectangles(Bitmap bitmap)
        {
            int numberOfRectangels = (bitmap.Height * bitmap.Width) / (n * m);

            ImageRectangle[] pixelsRectangels = new ImageRectangle[numberOfRectangels];

            int xRectanglePosition = 0;
            int yRectanglePosition = 0;

            for (int r = 0; r < numberOfRectangels; r++)
            {
                ImageRectangle rectangle = new ImageRectangle(n, m);

                rectangle.PixelsMatrix = GetRectanglePixelMatrix(xRectanglePosition, yRectanglePosition, bitmap);

                if (xRectanglePosition >= (bitmap.Width / m) - 1)
                {
                    xRectanglePosition = 0;
                    yRectanglePosition++;
                }
                else
                {
                    xRectanglePosition++;
                }

                pixelsRectangels[r] = rectangle;
            }

            return pixelsRectangels;
        }
Exemplo n.º 3
0
        private void TeachNeuralNetwork(ImageRectangle[] rectangles, BackgroundWorker worker, DoWorkEventArgs doWorkEvent, bool test)
        {
            int N = n * m * 3;

            RectangularMatrix weightMatrix = new RectangularMatrix(N, p);

            Random rand = new Random();
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < p; j++)
                {
                    weightMatrix[i, j] = rand.NextDouble() * 0.1;
                }
            }

            RectangularMatrix secondWeightMatrix = weightMatrix.Transpose();

            double totalError = e + 1;
            int totalIterationNumber = 0;

            state = new CurrentState();

            RowVector[] vectors = new RowVector[rectangles.Length];

            for (int i = 0; i < rectangles.Length; i++)
            {
                vectors[i] = new RowVector(((ImageRectangle)rectangles.GetValue(i)).GetVector());
            }

            while (totalError > e && totalIterationNumber < iterationNumber)
            {
                totalError = 0;

                for (int i = 0; i < rectangles.Length; i++)
                {
                    RowVector xVector = vectors[i];

                    RowVector yVector = xVector * weightMatrix;
                    RowVector xSecondVector = yVector * secondWeightMatrix;
                    RowVector deltaXVector = xSecondVector - xVector;

                    weightMatrix = weightMatrix - a * xVector.Transpose() * deltaXVector * secondWeightMatrix.Transpose();
                    secondWeightMatrix = secondWeightMatrix - a * yVector.Transpose() * deltaXVector;
                }

                for (int i = 0; i < rectangles.Length; i++)
                {

                    RowVector xVector = vectors[i];

                    RowVector yVector = xVector * weightMatrix;
                    RowVector xSecondVector = yVector * secondWeightMatrix;
                    RowVector deltaXVector = xSecondVector - xVector;

                    for (int j = 0; j < deltaXVector.ColumnCount; j++)
                    {
                        totalError += deltaXVector[0, j] * deltaXVector[0, j];
                    }
                }

                totalIterationNumber++;

                state.CurentError = totalError;
                state.IterationNumber = totalIterationNumber;

                if (!test)
                {
                    worker.ReportProgress(0, state);

                    if (worker.CancellationPending)
                    {
                        doWorkEvent.Cancel = true;
                        break;
                    }
                }
            }

            for (int i = 0; i < rectangles.Length; i++)
            {
                RowVector xVector = vectors[i];

                RowVector yVector = xVector * weightMatrix;
                RowVector xSecondVector = yVector * secondWeightMatrix;

                ((ImageRectangle)rectangles.GetValue(i)).SetPixelMatrixFromVector(xSecondVector.ToArray());
            }

            state.Compressing = CalculateCompressing(rectangles.Length, N);
            state.FirstWeightMatrix = GetMatrixString(weightMatrix);
            state.SecondWeightMatrix = GetMatrixString(secondWeightMatrix);
        }
Exemplo n.º 4
0
        private ImagePixel[,] AssembleRectanglesToPixelMatrix(ImageRectangle[] pixelsRectangels, int height, int width)
        {
            ImagePixel[,] pixels = new ImagePixel[width, height];

            int xRectanglePosition = 0;
            int yRectanglePosition = 0;

            for (int p = 0; p < pixelsRectangels.Length; p++)
            {
                ImageRectangle rectangle = pixelsRectangels[p];

                AddRectanglePixelsInmatrix(pixels, rectangle, xRectanglePosition, yRectanglePosition);

                if (xRectanglePosition >= (width / rectangle.M) - 1)
                {
                    xRectanglePosition = 0;
                    yRectanglePosition++;
                }
                else
                {
                    xRectanglePosition++;
                }
            }

            return pixels;
        }