예제 #1
0
 public void DoPaint(ImageMatrix m)
 {
     Console.WriteLine($"在{this.GetType().Name}显示图像");
 }
        private double GetMinutiaeAngle(ImageMatrix matrix, int x, int y, MinutiaType type)
        {
            double angle = 0;

            if (type == MinutiaType.End)
            {
                List <Point> points = new List <Point> {
                    new Point(x, y)
                };
                for (int i = 0; i < 10; i++)
                {
                    List <Point> neighbors = GetNeighboors(matrix, points[points.Count - 1].X, points[points.Count - 1].Y);
                    foreach (var neighbor in neighbors)
                    {
                        if (!points.Contains(neighbor))
                        {
                            points.Add(neighbor);
                        }
                    }
                }
                if (points.Count < 10)
                {
                    return(-1);
                }
                angle = Angle.ComputeAngle(points[points.Count - 1].X - points[0].X,
                                           points[points.Count - 1].Y - points[0].Y);
            }

            if (type == MinutiaType.Bifurcation)
            {
                List <Point> treeNeighboors = GetNeighboors(matrix, x, y);

                if (treeNeighboors.Count < 3)
                {
                    return(double.NaN);
                }

                List <Point> n1 = new List <Point>()
                {
                    new Point(x, y), treeNeighboors[0]
                };
                List <Point> n2 = new List <Point>()
                {
                    new Point(x, y), treeNeighboors[1]
                };
                List <Point> n3 = new List <Point>()
                {
                    new Point(x, y), treeNeighboors[2]
                };

                for (int i = 0; i < 10; i++)
                {
                    List <Point> neighboors1 = GetNeighboors(matrix, n1[n1.Count - 1].X, n1[n1.Count - 1].Y);
                    foreach (var neighbor in neighboors1)
                    {
                        if (!n1.Contains(neighbor) && !treeNeighboors.Contains(neighbor))
                        {
                            n1.Add(neighbor);
                        }
                    }

                    List <Point> neighboors2 = GetNeighboors(matrix, n2[n2.Count - 1].X, n2[n2.Count - 1].Y);
                    foreach (var neighbor in neighboors2)
                    {
                        if (!n2.Contains(neighbor) && !treeNeighboors.Contains(neighbor))
                        {
                            n2.Add(neighbor);
                        }
                    }

                    List <Point> neighboors3 = GetNeighboors(matrix, n3[n3.Count - 1].X, n3[n3.Count - 1].Y);
                    foreach (var neighbor in neighboors3)
                    {
                        if (!n3.Contains(neighbor) && !treeNeighboors.Contains(neighbor))
                        {
                            n3.Add(neighbor);
                        }
                    }
                }

                if (n1.Count < 10 || n2.Count < 10 || n3.Count < 10)
                {
                    return(-1);
                }

                double angleNeighboors1 = Angle.ComputeAngle(n1[n1.Count - 1].X - n1[0].X, n1[n1.Count - 1].Y - n1[0].Y);
                double angleNeighboors2 = Angle.ComputeAngle(n2[n2.Count - 1].X - n2[0].X, n2[n2.Count - 1].Y - n2[0].Y);
                double angleNeighboors3 = Angle.ComputeAngle(n3[n3.Count - 1].X - n3[0].X, n3[n3.Count - 1].Y - n3[0].Y);

                double diff1 = Angle.DifferencePi(angleNeighboors1, angleNeighboors2);
                double diff2 = Angle.DifferencePi(angleNeighboors1, angleNeighboors3);
                double diff3 = Angle.DifferencePi(angleNeighboors2, angleNeighboors3);

                if (diff1 <= diff2 && diff1 <= diff3)
                {
                    angle = angleNeighboors2 + diff1 / 2;
                    if (angle > 2 * Math.PI)
                    {
                        angle -= 2 * Math.PI;
                    }
                }
                else if (diff2 <= diff1 && diff2 <= diff3)
                {
                    angle = angleNeighboors1 + diff2 / 2;
                    if (angle > 2 * Math.PI)
                    {
                        angle -= 2 * Math.PI;
                    }
                }
                else
                {
                    angle = angleNeighboors3 + diff3 / 2;
                    if (angle > 2 * Math.PI)
                    {
                        angle -= 2 * Math.PI;
                    }
                }
            }

            return(angle);
        }
        public static double[] CalculateEqualizedCumulativeHistogram(ImageMatrix image)
        {
            int[] histogram = CalculateHistogram(image);

            return(CalculateEqualizedCumulativeHistogram(histogram));
        }
예제 #4
0
        private void RemoveBadBlocksVariance(OrientationImage oi, ImageMatrix matrix)
        {
            int maxLength = oi.WindowSize / 2;

            int[,] varianceMatrix = new int[oi.Height, oi.Width];
            double max = 0;
            double min = double.MaxValue;

            for (int row = 0; row < oi.Height; row++)
            {
                for (int col = 0; col < oi.Width; col++)
                {
                    int x, y;
                    oi.GetPixelCoordFromBlock(row, col, out x, out y);

                    // Computing Average
                    int sum   = 0;
                    int count = 0;
                    for (int xi = x - maxLength; xi < x + maxLength; xi++)
                    {
                        for (int yi = y - maxLength; yi < y + maxLength; yi++)
                        {
                            if (xi >= 0 && xi < matrix.Width && yi >= 0 && yi < matrix.Height)
                            {
                                sum += matrix[yi, xi];
                                count++;
                            }
                        }
                    }
                    double avg = 1.0 * sum / count;

                    // Computing Variance
                    double sqrSum = 0;
                    for (int xi = x - maxLength; xi < x + maxLength; xi++)
                    {
                        for (int yi = y - maxLength; yi < y + maxLength; yi++)
                        {
                            if (xi >= 0 && xi < matrix.Width && yi >= 0 && yi < matrix.Height)
                            {
                                double diff = matrix[yi, xi] - avg;
                                sqrSum += diff * diff;
                            }
                        }
                    }
                    varianceMatrix[row, col] = Convert.ToInt32(Math.Round(sqrSum / (count - 1)));

                    // Computing de max variance
                    if (varianceMatrix[row, col] > max)
                    {
                        max = varianceMatrix[row, col];
                    }
                    if (varianceMatrix[row, col] < min)
                    {
                        min = varianceMatrix[row, col];
                    }
                }
            }

            for (int row = 0; row < oi.Height; row++)
            {
                for (int col = 0; col < oi.Width; col++)
                {
                    varianceMatrix[row, col] = Convert.ToInt32(Math.Round(254.0 * (varianceMatrix[row, col] - min) / (max - min)));
                }
            }

            const int t = 15;

            for (int row = 0; row < oi.Height; row++)
            {
                for (int col = 0; col < oi.Width; col++)
                {
                    if (!oi.IsNullBlock(row, col) && varianceMatrix[row, col] <= t)
                    {
                        oi[row, col] = OrientationImage.Null;
                    }
                }
            }
        }
예제 #5
0
        private void AddMutliframedImageToCanvas(ImageMatrix imageMtx, ref int i, ref int j, int frames)
        {
            for (int frameIndex = 0; frameIndex < frames; frameIndex++, j++)
            {
                if (j >= Columns)
                {
                    j = 0;
                    i++;
                }

                Console.Out.WriteLine("Adding new MF image at Row: " + i + " Column: " + j);
                System.Windows.Controls.Image image = new System.Windows.Controls.Image();
                var img = imageMtx.GetImage(frameIndex);
                ImageConverter imgConv = new ImageConverter();

                var bytes = (byte[])imgConv.ConvertTo(img, typeof(byte[]));
                MemoryStream ms = new MemoryStream(bytes);
                BitmapImage bmpImg = new BitmapImage();
                bmpImg.BeginInit();
                bmpImg.StreamSource = ms;
                bmpImg.EndInit();
                image.Source = bmpImg;
                image.Width = image.Width * ScaledWidth;
                image.Height = image.Height * ScaledHeight;

                Grid.SetRow(image, i);
                Grid.SetColumn(image, j);
                CanvasGrid.Children.Add(image);
            }
        }
예제 #6
0
 private void bn_reset_Click(object sender, RoutedEventArgs e)
 {
     Game.Reset();
     ImageMatrix.UpdateAll();
 }
예제 #7
0
        private void compareForthTransform(ImageMatrix M, int X, int Y)
        {
            int transitions, greater;

            for (int y = 1; y < Y; y++)
            {
                for (int x = 1; x < X; x++)
                {
                    ImageMatrix.Pixel pixel = M[x, y];
                    if (pixel >= 0)
                    {
                        continue;
                    }
                    greater     = 0;
                    transitions = 0;
                    if (pixel[Direction.TopLeft] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.TopLeft] < 0 && pixel[Direction.Top] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.Top] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.Top] < 0 && pixel[Direction.TopRight] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.TopRight] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.TopRight] < 0 && pixel[Direction.Right] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.Left] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.Right] < 0 && pixel[Direction.BottomRight] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.Right] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.BottomRight] < 0 && pixel[Direction.Bottom] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.BottomLeft] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.Bottom] < 0 && pixel[Direction.BottomLeft] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.Bottom] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.BottomLeft] < 0 && pixel[Direction.Left] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.BottomRight] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.Left] < 0 && pixel[Direction.TopLeft] >= 0)
                    {
                        transitions++;
                    }
                    if (greater == 1 && transitions < 2)
                    {
                        pixel.Luminance *= -1;
                    }
                }
            }
        }
예제 #8
0
        public void SetCanvas(List<NMImage> imageObjects)
        {
            try
            {
                ImageObjects = imageObjects;
                if (Sequential)
                {
                    MessageBox.Show("Setting Sequential");
                    if(imageObjects[0].DicomObj == null)
                        MessageBox.Show("Sequ set canvas null obj");
                    DcmObject = imageObjects[0].DicomObj;
                    DcmImage = new ImageMatrix(imageObjects[0].FilePath);
                }
                else if (MultiFramed)
                {
                    MessageBox.Show("Setting Multiframed");
                    if (imageObjects[0].DicomObj == null)
                        MessageBox.Show("MF set canvas null objs");
                    DcmObjects = new List<DICOMObject>();
                    DcmImages = new List<ImageMatrix>();
                    int i = 0;
                    foreach (NMImage image in imageObjects)
                    {
                        DcmObjects.Add(image.DicomObj);
                        DcmImages.Add(new ImageMatrix(image.FilePath));
                        i++;
                    }
                }

                //Calculate scale factor for cell dimensions
                int imageWidth = 0, imageHeight = 0;
                ScaleLayout(ref imageWidth, ref imageHeight);

                //Add columns with scaled widths
                for (int i = 0; i < Columns; i++)
                {
                    ColumnDefinition colDef = new ColumnDefinition();
                    GridLength width = new GridLength(imageWidth * ScaledWidth);
                    colDef.Width = width;
                    CanvasGrid.ColumnDefinitions.Add(colDef);
                }

                //Add rows with scaled heights
                for (int i = 0; i < Rows; i++)
                {
                    RowDefinition rowDef = new RowDefinition();
                    GridLength height = new GridLength(imageHeight * ScaledHeight);
                    rowDef.Height = height;
                    CanvasGrid.RowDefinitions.Add(rowDef);
                }

                Console.Out.WriteLine("File: " + ImageObjects[0].FilePath);
                //DcmImage = new ImageMatrix(ImageObjects[0].FilePath);
                //DcmImage.Properties.WindowAndLevel.Window = 0;
                //DcmImage.Properties.WindowAndLevel.Window = 0;
            }
            catch (Exception e) { MessageBox.Show("Error setting up multipframed canvas: " + e.ToString()); }
        }
        private ImageMatrix GetBinaryImage(ImageMatrix matrix, OrientationImage orientationImage)
        {
            int[]       filter = new int[] { 1, 2, 5, 7, 5, 2, 1 };
            ImageMatrix newImg = new ImageMatrix(matrix.Width, matrix.Height);

            for (int i = 0; i < matrix.Width; i++)
            {
                for (int j = 0; j < matrix.Height; j++)
                {
                    newImg[j, i] = 255;
                }
            }
            for (int row = 0; row < orientationImage.Height; row++)
            {
                for (int col = 0; col < orientationImage.Width; col++)
                {
                    if (!orientationImage.IsNullBlock(row, col))
                    {
                        int x, y;
                        orientationImage.GetPixelCoordFromBlock(row, col, out x, out y);

                        int maxLength = orientationImage.WindowSize / 2;
                        for (int xi = x - maxLength; xi < x + maxLength; xi++)
                        {
                            for (int yi = y - maxLength; yi < y + maxLength; yi++)
                            {
                                int[] projection = GetProjection(orientationImage, row, col, xi, yi, matrix);

                                int[]     smoothed = new int[orientationImage.WindowSize + 1];
                                const int n        = 7;
                                for (int j = 0; j < projection.Length; j++)
                                {
                                    int idx = 0;
                                    int sum = 0, count = 0;
                                    for (int k = j - n / 2; k <= j + n / 2; k++, idx++)
                                    {
                                        if (k >= 0 && k < projection.Length)
                                        {
                                            sum += projection[k] * filter[idx];
                                            count++;
                                        }
                                    }
                                    smoothed[j] = sum / count;
                                }

                                int center = smoothed.Length / 2;
                                int left;
                                for (left = center - 1; smoothed[left] == smoothed[center] && left > 0; left--)
                                {
                                    ;
                                }

                                int rigth;
                                for (rigth = center + 1; smoothed[rigth] == smoothed[center] && rigth < smoothed.Length - 1; rigth++)
                                {
                                    ;
                                }

                                if (xi >= 0 && xi < matrix.Width && yi >= 0 && yi < matrix.Height)
                                {
                                    newImg[yi, xi] = 255;
                                }

                                if (xi > 0 && xi < matrix.Width - 1 && yi > 0 && yi < matrix.Height - 1 && !(left == 255 && rigth == smoothed.Length - 1))
                                {
                                    if (smoothed[center] < smoothed[left] && smoothed[center] < smoothed[rigth])
                                    {
                                        newImg[yi, xi] = 0;
                                    }
                                    else if (rigth - left == 2 &&
                                             ((smoothed[left] < smoothed[left - 1] &&
                                               smoothed[left] < smoothed[center]) ||
                                              (smoothed[rigth] < smoothed[rigth + 1] &&
                                               smoothed[rigth] < smoothed[center]) ||
                                              (smoothed[center] < smoothed[left - 1] &&
                                               smoothed[center] < smoothed[rigth + 1]) ||
                                              (smoothed[center] < smoothed[left - 1] &&
                                               smoothed[center] < smoothed[rigth]) ||
                                              (smoothed[center] < smoothed[left] &&
                                               smoothed[center] < smoothed[rigth + 1])))
                                    {
                                        newImg[yi, xi] = 0;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(newImg);
        }
        private bool IsContinous(ImageMatrix matrix, int x0, int y0, int x, int y, out int x1, out int y1)
        {
            x1 = -1;
            y1 = -1;
            bool isBlack = false;

            if (matrix[y0, x0] == 0)
            {
                matrix[y0, x0] = 255;
                isBlack        = true;
            }

            int tl = (x > 0 && y > 0) ? matrix[y - 1, x - 1] : 255;
            int tc = (y > 0) ? matrix[y - 1, x] : 255;
            int tr = (x < matrix.Width - 1 && y > 0) ? matrix[y - 1, x + 1] : 255;
            int cl = (x > 0) ? matrix[y, x - 1] : 255;
            int ce = matrix[y, x];
            int cr = (x < matrix.Width - 1) ? matrix[y, x + 1] : 255;
            int bl = (x > 0 && y < matrix.Height - 1) ? matrix[y + 1, x - 1] : 255;
            int bc = (y < matrix.Height - 1) ? matrix[y + 1, x] : 255;
            int br = (x < matrix.Width - 1 && y < matrix.Height - 1) ? matrix[y + 1, x + 1] : 255;

            if (tl == 0 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x - 1;
                y1 = y - 1;
                if (isBlack)
                {
                    matrix[y0, x0] = 0;
                }
                return(true);
            }
            if (tl == 255 && tc == 0 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x;
                y1 = y - 1;
                if (isBlack)
                {
                    matrix[y0, x0] = 0;
                }
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 0 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x + 1;
                y1 = y - 1;
                if (isBlack)
                {
                    matrix[y0, x0] = 0;
                }
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 0 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x + 1;
                y1 = y;
                if (isBlack)
                {
                    matrix[y0, x0] = 0;
                }
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 0
                )
            {
                x1 = x + 1;
                y1 = y + 1;
                if (isBlack)
                {
                    matrix[y0, x0] = 0;
                }
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 0 && br == 255
                )
            {
                x1 = x;
                y1 = y + 1;
                if (isBlack)
                {
                    matrix[y0, x0] = 0;
                }
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 0 && bc == 255 && br == 255
                )
            {
                x1 = x - 1;
                y1 = y + 1;
                if (isBlack)
                {
                    matrix[y0, x0] = 0;
                }
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 0 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x - 1;
                y1 = y;
                if (isBlack)
                {
                    matrix[y0, x0] = 0;
                }
                return(true);
            }
            return(false);
        }
        private bool IsEnd(ImageMatrix matrix, int x, int y, out int x1, out int y1)
        {
            x1 = -1;
            y1 = -1;

            int tl = (x > 0 && y > 0) ? matrix[y - 1, x - 1] : 255;
            int tc = (y > 0) ? matrix[y - 1, x] : 255;
            int tr = (x < matrix.Width - 1 && y > 0) ? matrix[y - 1, x + 1] : 255;
            int cl = (x > 0) ? matrix[y, x - 1] : 255;
            int ce = matrix[y, x];
            int cr = (x < matrix.Width - 1) ? matrix[y, x + 1] : 255;
            int bl = (x > 0 && y < matrix.Height - 1) ? matrix[y + 1, x - 1] : 255;
            int bc = (y < matrix.Height - 1) ? matrix[y + 1, x] : 255;
            int br = (x < matrix.Width - 1 && y < matrix.Height - 1) ? matrix[y + 1, x + 1] : 255;

            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x;
                y1 = y;
                return(true);
            }
            if (tl == 0 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x - 1;
                y1 = y - 1;
                return(true);
            }
            if (tl == 255 && tc == 0 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x;
                y1 = y - 1;
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 0 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x + 1;
                y1 = y - 1;
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 0 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x + 1;
                y1 = y;
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 0
                )
            {
                x1 = x + 1;
                y1 = y + 1;
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 0 && br == 255
                )
            {
                x1 = x;
                y1 = y + 1;
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 255 && ce == 0 && cr == 255 &&
                bl == 0 && bc == 255 && br == 255
                )
            {
                x1 = x - 1;
                y1 = y + 1;
                return(true);
            }
            if (tl == 255 && tc == 255 && tr == 255 &&
                cl == 0 && ce == 0 && cr == 255 &&
                bl == 255 && bc == 255 && br == 255
                )
            {
                x1 = x - 1;
                y1 = y;
                return(true);
            }
            return(false);
        }
        private void RemoveSpikes(ImageMatrix matrix, OrientationImage orientationImage)
        {
            for (int row = 0; row < orientationImage.Height; row++)
            {
                for (int col = 0; col < orientationImage.Width; col++)
                {
                    if (!orientationImage.IsNullBlock(row, col))
                    {
                        double[] cos = new double[3];
                        double[] sin = new double[3];

                        double orthogonalAngle = orientationImage.AngleInRadians(row, col) + Math.PI / 2;
                        cos[0] = Math.Cos(orthogonalAngle);
                        sin[0] = Math.Sin(orthogonalAngle);

                        double orthogonalAngle1 = orthogonalAngle + Math.PI / 12;
                        cos[1] = Math.Cos(orthogonalAngle1);
                        sin[1] = Math.Sin(orthogonalAngle1);

                        double orthogonalAngle2 = orthogonalAngle - Math.PI / 12;
                        cos[2] = Math.Cos(orthogonalAngle2);
                        sin[2] = Math.Sin(orthogonalAngle2);

                        int x, y;
                        orientationImage.GetPixelCoordFromBlock(row, col, out x, out y);

                        int maxLength = orientationImage.WindowSize / 2;
                        for (int xi = x - maxLength; xi < x + maxLength; xi++)
                        {
                            for (int yi = y - maxLength; yi < y + maxLength; yi++)
                            {
                                int  xj         = xi;
                                int  yj         = yi;
                                bool spikeFound = true;
                                while (spikeFound)
                                {
                                    spikeFound = false;
                                    if (xj > 0 && xj < matrix.Width - 1 && yj > 0 && yj < matrix.Height - 1)
                                    {
                                        int tl = matrix[yj - 1, xj - 1];
                                        int tc = matrix[yj - 1, xj];
                                        int tr = matrix[yj - 1, xj + 1];

                                        int le = matrix[yj, xj - 1];
                                        int ce = matrix[yj, xj];
                                        int ri = matrix[yj, xj + 1];

                                        int bl = matrix[yj + 1, xj - 1];
                                        int bc = matrix[yj + 1, xj];
                                        int br = matrix[yj + 1, xj + 1];

                                        if (CouldBeSpike(tl, tc, tr, le, ce, ri, bl, bc, br))
                                        {
                                            for (int i = 0; i < sin.Length && !spikeFound; i++)
                                            {
                                                int xk = Convert.ToInt32(Math.Round(xj - cos[i]));
                                                int yk = Convert.ToInt32(Math.Round(yj - sin[i]));
                                                if (matrix[yk, xk] == 0)
                                                {
                                                    matrix[yj, xj] = 255;
                                                    xj             = xk;
                                                    yj             = yk;
                                                    spikeFound     = true;
                                                }
                                                else
                                                {
                                                    xk = Convert.ToInt32(Math.Round(xj + cos[i]));
                                                    yk = Convert.ToInt32(Math.Round(yj + sin[i]));
                                                    if (matrix[yk, xk] == 0)
                                                    {
                                                        matrix[yj, xj] = 255;
                                                        xj             = xk;
                                                        yj             = yk;
                                                        spikeFound     = true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        private int[] GetProjection(OrientationImage oi, int row, int col, int x, int y, ImageMatrix matrix)
        {
            double angle           = oi.AngleInRadians(row, col);
            double orthogonalAngle = oi.AngleInRadians(row, col) + Math.PI / 2;

            int maxLength = oi.WindowSize / 2;

            int[] projection       = new int[2 * maxLength + 1];
            int[] outlayerCount    = new int[2 * maxLength + 1];
            bool  outlayerFound    = false;
            int   totalSum         = 0;
            int   validPointsCount = 0;

            for (int li = -maxLength, i = 0; li <= maxLength; li++, i++)
            {
                int xi = Convert.ToInt32(x - li * Math.Cos(orthogonalAngle));
                int yi = Convert.ToInt32(y - li * Math.Sin(orthogonalAngle));

                int ySum = 0;
                for (int lj = -maxLength; lj <= maxLength; lj++)
                {
                    int xj = Convert.ToInt32(xi - lj * Math.Cos(angle));
                    int yj = Convert.ToInt32(yi - lj * Math.Sin(angle));
                    if (xj >= 0 && yj >= 0 && xj < matrix.Width && yj < matrix.Height)
                    {
                        ySum += matrix[yj, xj];
                        validPointsCount++;
                    }
                    else
                    {
                        outlayerCount[i]++;
                        outlayerFound = true;
                    }
                }
                projection[i] = ySum;
                totalSum     += ySum;
            }

            if (outlayerFound)
            {
                int avg = totalSum / validPointsCount;
                for (int i = 0; i < projection.Length; i++)
                {
                    projection[i] += avg * outlayerCount[i];
                }
            }

            return(projection);
        }
        private bool RemoveSpikeOnMinutiae(ImageMatrix matrix, Minutia end, Minutia bifurcation)
        {
            if (bifurcation.MinutiaType == MinutiaType.Bifurcation)
            {
                int xBifur = bifurcation.X;
                int yBifur = bifurcation.Y;

                List <Point> treeNeighboors = GetNeighboors(matrix, xBifur, yBifur);

                if (treeNeighboors.Count < 3)
                {
                    return(false);
                }

                List <Point> n1 = new List <Point> {
                    new Point(xBifur, yBifur), treeNeighboors[0]
                };
                List <Point> n2 = new List <Point> {
                    new Point(xBifur, yBifur), treeNeighboors[1]
                };
                List <Point> n3 = new List <Point> {
                    new Point(xBifur, yBifur), treeNeighboors[2]
                };

                int xEnd = end.X;
                int yEnd = end.Y;

                for (int i = 0; i < 15; i++)
                {
                    List <Point> neighboors1 = GetNeighboors(matrix, n1[n1.Count - 1].X, n1[n1.Count - 1].Y);
                    foreach (var neighbor in neighboors1)
                    {
                        if (!n1.Contains(neighbor) && !treeNeighboors.Contains(neighbor))
                        {
                            if (neighbor.X == xEnd &&
                                neighbor.Y == yEnd)
                            {
                                return(true);
                            }
                            else
                            {
                                n1.Add(neighbor);
                            }
                        }
                    }

                    List <Point> neighboors2 = GetNeighboors(matrix, n2[n2.Count - 1].X, n2[n2.Count - 1].Y);
                    foreach (var neighbor in neighboors2)
                    {
                        if (!n2.Contains(neighbor) && !treeNeighboors.Contains(neighbor))
                        {
                            if (neighbor.X == xEnd &&
                                neighbor.Y == yEnd)
                            {
                                return(true);
                            }
                            else
                            {
                                n2.Add(neighbor);
                            }
                        }
                    }

                    List <Point> neighboors3 = GetNeighboors(matrix, n3[n3.Count - 1].X, n3[n3.Count - 1].Y);
                    foreach (var neighbor in neighboors3)
                    {
                        if (!n3.Contains(neighbor) && !treeNeighboors.Contains(neighbor))
                        {
                            if (neighbor.X == xEnd &&
                                neighbor.Y == yEnd)
                            {
                                return(true);
                            }
                            else
                            {
                                n3.Add(neighbor);
                            }
                        }
                    }
                }
            }
            return(false);
        }
예제 #15
0
        private void compareBackTransform(ImageMatrix M, int X, int Y)
        {
            int greater, transitions;

            for (int y = Y - 1; y > 0; y--)
            {
                for (int x = X - 1; x > 0; x--)
                {
                    ImageMatrix.Pixel pixel = M[x, y];

                    if (pixel >= 0)
                    {
                        continue;
                    }
                    greater     = 0;
                    transitions = 0;
                    if (pixel[Direction.TopLeft] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.TopLeft] < 0 && pixel[Direction.Top] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.Top] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.Top] < 0 && pixel[Direction.TopRight] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.TopRight] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.TopRight] < 0 && pixel[Direction.Right] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.Left] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.Right] < 0 && pixel[Direction.BottomRight] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.Right] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.BottomRight] < 0 && pixel[Direction.Bottom] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.BottomLeft] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.Bottom] < 0 && pixel[Direction.BottomLeft] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.Bottom] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.BottomLeft] < 0 && pixel[Direction.Left] >= 0)
                    {
                        transitions++;
                    }
                    if (pixel[Direction.BottomRight] < pixel)
                    {
                        greater = 1;
                    }
                    if (pixel[Direction.Left] < 0 && pixel[Direction.TopLeft] >= 0)
                    {
                        transitions++;
                    }
                    if (greater == 1 && transitions < 2)
                    {
                        pixel.Luminance *= -1;
                    }
                }
            }
        }
        private List <Minutia> GetMinutiaes(ImageMatrix matrix, OrientationImage orientationImage)
        {
            List <Minutia> minutiaes = new List <Minutia>();

            for (int row = 0; row < orientationImage.Height; row++)
            {
                for (int col = 0; col < orientationImage.Width; col++)
                {
                    if (!orientationImage.IsNullBlock(row, col))
                    {
                        int x, y;
                        orientationImage.GetPixelCoordFromBlock(row, col, out x, out y);

                        int maxLength = orientationImage.WindowSize / 2;

                        for (int xi = x - maxLength; xi < x + maxLength; xi++)
                        {
                            for (int yi = y - maxLength; yi < y + maxLength; yi++)
                            {
                                if (xi > 0 && xi < matrix.Width - 1 && yi > 0 && yi < matrix.Height - 1)
                                {
                                    if (matrix[yi, xi] == 0)
                                    {
                                        List <int> values = new List <int>
                                        {
                                            matrix[yi, xi + 1] == 255 ? 0 : 1,
                                            matrix[yi - 1, xi + 1] == 255 ? 0 : 1,
                                            matrix[yi - 1, xi] == 255 ? 0 : 1,
                                            matrix[yi - 1, xi - 1] == 255 ? 0 : 1,
                                            matrix[yi, xi - 1] == 255 ? 0 : 1,
                                            matrix[yi + 1, xi - 1] == 255 ? 0 : 1,
                                            matrix[yi + 1, xi] == 255 ? 0 : 1,
                                            matrix[yi + 1, xi + 1] == 255 ? 0 : 1,
                                        };

                                        int cn = 0;
                                        for (int i = 0; i < values.Count; i++)
                                        {
                                            int idx = i;
                                            if (i == 7)
                                            {
                                                idx = -1;
                                            }
                                            cn += Math.Abs(values[i] - values[idx + 1]);
                                        }
                                        cn = (int)(cn * 0.5);


                                        double angleminu;
                                        // end minutiae
                                        if (cn == 1)
                                        {
                                            angleminu = GetMinutiaeAngle(matrix, xi, yi, MinutiaType.End);
                                            if (angleminu != -1)
                                            {
                                                minutiaes.Add(new Minutia
                                                {
                                                    Angle       = (float)angleminu,
                                                    X           = (short)xi,
                                                    Y           = (short)yi,
                                                    MinutiaType = MinutiaType.End
                                                }
                                                              );
                                            }
                                        }
                                        //bifurcation minutiae
                                        if (cn == 3)
                                        {
                                            angleminu = GetMinutiaeAngle(matrix, xi, yi, MinutiaType.Bifurcation);
                                            if (!double.IsNaN(angleminu) && angleminu != -1)
                                            {
                                                minutiaes.Add(new Minutia
                                                {
                                                    Angle       = (float)angleminu,
                                                    X           = (short)xi,
                                                    Y           = (short)yi,
                                                    MinutiaType = MinutiaType.Bifurcation
                                                }
                                                              );
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            List <Minutia> noInTheBorder = new List <Minutia>();

            for (int i = 0; i < minutiaes.Count; i++)
            {
                // boundary Effects (foreground areas)
                int row, col;
                orientationImage.GetBlockCoordFromPixel(minutiaes[i].X, minutiaes[i].Y, out row, out col);
                if (row >= 1 && col >= 1 && col < orientationImage.Width - 1 && row < orientationImage.Height - 1)
                {
                    if (!
                        (orientationImage.IsNullBlock(row - 1, col) ||
                         orientationImage.IsNullBlock(row + 1, col) ||
                         orientationImage.IsNullBlock(row, col - 1) ||
                         orientationImage.IsNullBlock(row, col + 1) //||
                        )
                        )
                    {
                        noInTheBorder.Add(minutiaes[i]);
                    }
                }
            }

            MtiaEuclideanDistance miEuclideanDistance = new MtiaEuclideanDistance();

            bool[] toErase = new bool[noInTheBorder.Count];
            for (int i = 0; i < noInTheBorder.Count; i++)
            {
                Minutia mA = noInTheBorder[i];
                for (int j = 0; j < noInTheBorder.Count; j++)
                {
                    if (i != j)
                    {
                        Minutia mB = noInTheBorder[j];
                        // different to orientation image
                        int row, col;
                        orientationImage.GetBlockCoordFromPixel(mA.X, mA.Y, out row, out col);
                        double angleOI = orientationImage.AngleInRadians(row, col);
                        if (mA.MinutiaType == MinutiaType.End &&
                            Math.Min(Angle.DifferencePi(mA.Angle, angleOI),
                                     Angle.DifferencePi(mA.Angle, angleOI + Math.PI)) > Math.PI / 6)
                        {
                            toErase[i] = true;
                        }

                        //  near minutiaes elimination
                        if (mA.MinutiaType == mB.MinutiaType &&
                            miEuclideanDistance.Compare(mA, mB) < 5)
                        {
                            toErase[i] = toErase[j] = true;
                        }

                        //  Ridge break elimination (Ratha)
                        if (mA.MinutiaType == MinutiaType.End &&
                            mB.MinutiaType == MinutiaType.End &&
                            mA.Angle == mB.Angle &&
                            miEuclideanDistance.Compare(mA, mB) < 10)
                        {
                            toErase[i] = toErase[j] = true;
                        }

                        // Ridge break elimination (tavo - migue)
                        if (mA.MinutiaType == MinutiaType.End &&
                            mB.MinutiaType == MinutiaType.End &&
                            Angle.DifferencePi(mA.Angle, mB.Angle) < Math.PI / 12 &&
                            miEuclideanDistance.Compare(mA, mB) < 10)
                        {
                            toErase[i] = toErase[j] = true;
                        }

                        // spike elimination
                        if (mA.MinutiaType == MinutiaType.End &&
                            mB.MinutiaType == MinutiaType.Bifurcation &&
                            miEuclideanDistance.Compare(mA, mB) < 15)
                        {
                            if (RemoveSpikeOnMinutiae(matrix, mA, mB))
                            {
                                toErase[i] = true;
                            }
                        }
                    }
                }
            }

            List <Minutia> result = new List <Minutia>();

            for (int i = 0; i < noInTheBorder.Count; i++)
            {
                if (!toErase[i])
                {
                    result.Add(noInTheBorder[i]);
                }
            }

            return(result);
        }
예제 #17
0
 private char CommaOrDot(ImageMatrix matrix)
 {
     return(Math.Abs(matrix.BlacksInColumn(matrix.Width / 2) - matrix.BlacksInRow(matrix.Height / 2)) < 5 ? '.' : ',');
 }
예제 #18
0
        private void AddSingleFrameImageToCanvas(ImageMatrix imageMtx, ref int i, ref int j)
        {
            Console.Out.WriteLine("Adding new SF image at Row: " + i + " Column: " + j);
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            var img = imageMtx.GetImage(0);
            ImageConverter imgConv = new ImageConverter();

            var bytes = (byte[])imgConv.ConvertTo(img, typeof(byte[]));
            MemoryStream ms = new MemoryStream(bytes);
            BitmapImage bmpImg = new BitmapImage();
            bmpImg.BeginInit();
            bmpImg.StreamSource = ms;
            bmpImg.EndInit();
            image.Source = bmpImg;
            image.Width = image.Width * ScaledWidth;
            image.Height = image.Height * ScaledHeight;

            Grid.SetRow(image, i);
            Grid.SetColumn(image, j);
            CanvasGrid.Children.Add(image);
            j++;
        }
예제 #19
0
        public static int[] CalculateCumulativeHistogram(ImageMatrix image)
        {
            int[] histogram = CalculateHistogram(image);

            return(CalculateCumulativeHistogram(histogram));
        }
예제 #20
0
        public void SetCanvas(NMImage image)
        {
            try
            {
                DcmObject = image.DicomObj;

                //Calculate scale factor for cell dimensions
                int imageWidth = 0, imageHeight = 0;
                ScaleLayout(ref imageWidth, ref imageHeight);

                //Add columns with scaled widths
                for (int i = 0; i < Columns; i++)
                {
                    ColumnDefinition colDef = new ColumnDefinition();
                    GridLength width = new GridLength(imageWidth * ScaledWidth);
                    colDef.Width = width;
                    CanvasGrid.ColumnDefinitions.Add(colDef);
                }

                //Add rows with scaled heights
                for (int i = 0; i < Rows; i++)
                {
                    RowDefinition rowDef = new RowDefinition();
                    GridLength height = new GridLength(imageHeight * ScaledHeight);
                    rowDef.Height = height;
                    CanvasGrid.RowDefinitions.Add(rowDef);
                }

                Console.Out.WriteLine(image.FilePath);
                //var owPixel = image.DicomObj.FindFirst(TagHelper.PIXEL_DATA) as OtherWordString;
                //byte[] img = (owPixel.Data_).ToArray();
                DcmImage = new ImageMatrix(image.FilePath);
                Console.Out.WriteLine("LET ME GO!!");
            }
            catch(Exception e)
            {
                MessageBox.Show("Error setting up canvas: " + e.Message + e.StackTrace);
            }
        }
예제 #21
0
파일: TestSuite.cs 프로젝트: Tobraef/OCR
        public void TestTurboMatrix()
        {
            ImageMatrix matrix = new ImageMatrix(new bool[10][]
            {
                new bool[3] {
                    false, false, false
                },
                new bool[3] {
                    false, false, false
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    false, false, false
                },
                new bool[3] {
                    false, false, false
                },
                new bool[3] {
                    false, false, false
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    true, true, true
                }
            });
            var turbo = new TurboMatrix(matrix);

            foreach (var row in turbo.Matrix)
            {
                if (row[0] != 0)
                {
                    Console.WriteLine("TEST_TurboMatrix: First pixel match fail");
                }
                if (row[1] != 2)
                {
                    Console.WriteLine("TEST_TurboMatrix: Wrong number of correct pixels");
                }
                if (row[2] != 2)
                {
                    Console.WriteLine("TEST_TurboMatrix: Wrong number of correct pixels");
                }
                if (row[3] != 3)
                {
                    Console.WriteLine("TEST_TurboMatrix: Wrong number of correct pixels");
                }
                if (row[4] != 3)
                {
                    Console.WriteLine("TEST_TurboMatrix: Wrong number of correct pixels");
                }
            }

            var   sameTurbo = new TurboMatrix(matrix);
            float equality  = turbo.Compare(sameTurbo) * DATA.letterHeight * DATA.letterWidth / 30;

            if (equality < 0.95)
            {
                Console.WriteLine("TEST_TurboMatrix: Same turbo matrix comparison failed. Equality: " + equality.ToString());
            }
            //12233 _ 041122
            var otherMatrix = new ImageMatrix(new bool[10][]
            {
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    false, false, false
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    false, false, false
                },
                new bool[3] {
                    false, false, false
                },
                new bool[3] {
                    true, true, true
                },
                new bool[3] {
                    true, true, true
                }
            });

            equality = turbo.Compare(new TurboMatrix(otherMatrix));
            Console.WriteLine("Equality: {0}", equality.ToString());
            equality /= (float)DATA.letterHeight * DATA.letterWidth / 30;
            if (equality > 0.6 || equality < 0.4)
            {
                //Console.WriteLine("TEST_TurboMatrix: Expected eq of different matrixes 0.5, received: {0}", equality.ToString());
            }
        }