Example #1
0
        public override Bitmap Thin(MainWindow win, Bitmap b, bool stop, int stopValue, bool save)
        {
            int[] A0 = new int[] { 3, 6, 7, 12, 14, 15, 24, 28, 30, 31, 48, 56, 60,
                                   62, 63, 96, 112, 120, 124, 126, 127, 129, 131, 135,
                                   143, 159, 191, 192, 193, 195, 199, 207, 223, 224,
                                   225, 227, 231, 239, 240, 241, 243, 247, 248, 249,
                                   251, 252, 253, 254 };
            int[] A1 = new int[] { 7, 14, 28, 56, 112, 131, 193, 224 };
            int[] A2 = new int[] { 7, 14, 15, 28, 30, 56, 60, 112, 120, 131, 135,
                                   193, 195, 224, 225, 240 };
            int[] A3 = new int[] { 7, 14, 15, 28, 30, 31, 56, 60, 62, 112, 120,
                                   124, 131, 135, 143, 193, 195, 199, 224, 225, 227,
                                   240, 241, 248 };
            int[] A4 = new int[] { 7, 14, 15, 28, 30, 31, 56, 60, 62, 63, 112, 120,
                                   124, 126, 131, 135, 143, 159, 193, 195, 199, 207,
                                   224, 225, 227, 231, 240, 241, 243, 248, 249, 252 };
            int[] A5 = new int[] { 7, 14, 15, 28, 30, 31, 56, 60, 62, 63, 112, 120,
                                   124, 126, 131, 135, 143, 159, 191, 193, 195, 199,
                                   207, 224, 225, 227, 231, 239, 240, 241, 243, 248,
                                   249, 251, 252, 254 };
            int[] A1px = new int[] { 3, 6, 7, 12, 14, 15, 24, 28, 30, 31, 48, 56,
                                     60, 62, 63, 96, 112, 120, 124, 126, 127, 129, 131,
                                     135, 143, 159, 191, 192, 193, 195, 199, 207, 223,
                                     224, 225, 227, 231, 239, 240, 241, 243, 247, 248,
                                     249, 251, 252, 253, 254 };

            if (stop)
            {
                System.Threading.Thread.Sleep(stopValue);
            }
            Image saveImage = b;

            if (save)
            {
                saveImage.Save("K3M" + SaveValue.ToString() + ".png", ImageFormat.Png);
                SaveValue++;
            }
            int  weight;
            bool change;
            List <(int, int)> marked  = new List <(int, int)>();
            List <(int, int)> changed = new List <(int, int)>();

            do
            {
                change = false;
                for (int i = 0; i < b.Width; i++) //Phase 0 - Mark
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        if (b.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
                        {
                            weight = CalculateWeight(i, j, b);
                            if (A0.Contains(weight))
                            {
                                marked.Add((i, j));
                            }
                        }
                    }
                }
                foreach ((int, int)p in marked)  //Phase 1 - Delete 3
                {
                    weight = CalculateWeight(p.Item1, p.Item2, b);
                    if (A1.Contains(weight))
                    {
                        b.SetPixel(p.Item1, p.Item2, Color.White);
                        changed.Add(p);
                        change = true;
                    }
                }
                foreach ((int, int)p in changed)
                {
                    marked.Remove(p);
                }
                changed.Clear();
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("K3M" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
                foreach ((int, int)p in marked)  //Phase 2 - Delete 3/4
                {
                    weight = CalculateWeight(p.Item1, p.Item2, b);
                    if (A2.Contains(weight))
                    {
                        b.SetPixel(p.Item1, p.Item2, Color.White);
                        changed.Add(p);
                        change = true;
                    }
                }
                foreach ((int, int)p in changed)
                {
                    marked.Remove(p);
                }
                changed.Clear();
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("K3M" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
                foreach ((int, int)p in marked)  //Phase 3 - Delete 3/4/5
                {
                    weight = CalculateWeight(p.Item1, p.Item2, b);
                    if (A3.Contains(weight))
                    {
                        b.SetPixel(p.Item1, p.Item2, Color.White);
                        changed.Add(p);
                        change = true;
                    }
                }
                foreach ((int, int)p in changed)
                {
                    marked.Remove(p);
                }
                changed.Clear();
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("K3M" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
                foreach ((int, int)p in marked)  //Phase 4 - Delete 3/4/5/6
                {
                    weight = CalculateWeight(p.Item1, p.Item2, b);
                    if (A4.Contains(weight))
                    {
                        b.SetPixel(p.Item1, p.Item2, Color.White);
                        changed.Add(p);
                        change = true;
                    }
                }
                foreach ((int, int)p in changed)
                {
                    marked.Remove(p);
                }
                changed.Clear();
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("K3M" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
                foreach ((int, int)p in marked)  //Phase 5 - Delete 3/4/5/6/7
                {
                    weight = CalculateWeight(p.Item1, p.Item2, b);
                    if (A5.Contains(weight))
                    {
                        b.SetPixel(p.Item1, p.Item2, Color.White);
                        changed.Add(p);
                        change = true;
                    }
                }
                foreach ((int, int)p in changed)
                {
                    marked.Remove(p);
                }
                changed.Clear();
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("K3M" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
                marked.Clear();               //Phase 6 - Unmark
            } while (change);
            for (int i = 0; i < b.Width; i++) //1 pixel Phase
            {
                for (int j = 0; j < b.Height; j++)
                {
                    weight = CalculateWeight(i, j, b);
                    if (A1px.Contains(weight))
                    {
                        b.SetPixel(i, j, Color.White);
                    }
                }
            }
            if (save)
            {
                saveImage = b;
                saveImage.Save("K3M" + SaveValue.ToString() + ".png", ImageFormat.Png);
            }
            return(b);
        }
Example #2
0
        public override Bitmap Thin(MainWindow win, Bitmap b, bool stop, int stopValue, bool save)
        {
            int[] A = new int[] { 3, 5, 7, 12, 13, 14, 15, 20,
                                  21, 22, 23, 28, 29, 30, 31, 48,
                                  52, 53, 54, 55, 56, 60, 61, 62,
                                  63, 65, 67, 69, 71, 77, 79, 80,
                                  81, 83, 84, 85, 86, 87, 88, 89,
                                  91, 92, 93, 94, 95, 97, 99, 101,
                                  103, 109, 111, 112, 113, 115, 116, 117,
                                  118, 119, 120, 121, 123, 124, 125, 126,
                                  127, 131, 133, 135, 141, 143, 149, 151,
                                  157, 159, 181, 183, 189, 191, 192, 193,
                                  195, 197, 199, 205, 207, 208, 209, 211,
                                  212, 213, 214, 215, 216, 217, 219, 220,
                                  221, 222, 223, 224, 225, 227, 229, 231,
                                  237, 239, 240, 241, 243, 244, 245, 246,
                                  247, 248, 249, 251, 252, 253, 254, 255 };

            if (stop)
            {
                System.Threading.Thread.Sleep(stopValue);
            }
            Image saveImage = b;

            if (save)
            {
                saveImage.Save("KMM" + SaveValue.ToString() + ".png", ImageFormat.Png);
                SaveValue++;
            }
            int[,] pixels        = new int[b.Width, b.Height];
            int[,] pixelsWeights = new int[b.Width, b.Height];
            for (int i = 0; i < b.Width; i++)
            {
                for (int j = 0; j < b.Height; j++)
                {
                    if (b.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
                    {
                        pixels[i, j] = 1;
                    }
                    else
                    {
                        pixels[i, j] = 0;
                    }
                }
            }
            bool change = false;

            do
            {
                change = false;
                for (int i = 0; i < b.Width; i++) //mark '2's
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        if (pixels[i, j] == 1)
                        {
                            if (i > 0 && pixels[i - 1, j] == 0)
                            {
                                pixels[i, j] = 2;
                            }
                            else if (j > 0 && pixels[i, j - 1] == 0)
                            {
                                pixels[i, j] = 2;
                            }
                            else if (i < b.Width - 1 && pixels[i + 1, j] == 0)
                            {
                                pixels[i, j] = 2;
                            }
                            else if (j < b.Height - 1 && pixels[i, j + 1] == 0)
                            {
                                pixels[i, j] = 2;
                            }
                        }
                    }
                }
                for (int i = 0; i < b.Width; i++) //mark '3's
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        if (pixels[i, j] == 1)
                        {
                            if (i > 0 && j > 0 && pixels[i - 1, j - 1] == 0)
                            {
                                pixels[i, j] = 3;
                            }
                            else if (i < b.Width - 1 && j > 0 && pixels[i + 1, j - 1] == 0)
                            {
                                pixels[i, j] = 3;
                            }
                            else if (i < b.Width - 1 && j < b.Height - 1 && pixels[i + 1, j + 1] == 0)
                            {
                                pixels[i, j] = 3;
                            }
                            else if (i > 0 && j < b.Height - 1 && pixels[i - 1, j + 1] == 0)
                            {
                                pixels[i, j] = 3;
                            }
                        }
                    }
                }
                for (int i = 0; i < b.Width; i++) //calculate weight
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        if (pixels[i, j] != 0)
                        {
                            pixelsWeights[i, j] = CalculateWeight(i, j, b);
                        }
                    }
                }
                for (int i = 0; i < b.Width; i++) //delete '4's
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        if (pixels[i, j] == 4)
                        {
                            if (A.Contains(pixelsWeights[i, j]))
                            {
                                pixels[i, j] = 0;
                                b.SetPixel(i, j, Color.White);
                                change = true;
                            }
                        }
                    }
                }
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("KMM" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
                for (int i = 0; i < b.Width; i++) //delete not needed '2's
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        if (pixels[i, j] == 2)
                        {
                            if (A.Contains(CalculateWeight(i, j, b)))
                            {
                                pixels[i, j] = 0;
                                b.SetPixel(i, j, Color.White);
                                change = true;
                            }
                            else
                            {
                                pixels[i, j] = 1;
                            }
                        }
                    }
                }
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("KMM" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
                for (int i = 0; i < b.Width; i++) //delete not needed '3's
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        if (pixels[i, j] == 3)
                        {
                            if (A.Contains(CalculateWeight(i, j, b)))
                            {
                                pixels[i, j] = 0;
                                b.SetPixel(i, j, Color.White);
                                change = true;
                            }
                            else
                            {
                                pixels[i, j] = 1;
                            }
                        }
                    }
                }
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("KMM" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
            } while (change);
            return(b);
        }
Example #3
0
        public override Bitmap Thin(MainWindow win, Bitmap b, bool stop, int stopValue, bool save)
        {
            if (stop)
            {
                System.Threading.Thread.Sleep(stopValue);
            }
            Image saveImage = b;

            if (save)
            {
                saveImage.Save("ZhangAndWang" + SaveValue.ToString() + ".png", ImageFormat.Png);
                SaveValue++;
            }
            List <(int, int)> zwdeletable = new List <(int, int)>();
            bool d;
            int  bp, cp;

            do
            {
                zwdeletable.Clear();
                for (int i = 0; i < b.Width; i++)
                {
                    for (int j = 0; j < b.Height; j++)
                    {
                        //1)
                        if (b.GetPixel(i, j).ToArgb() != Color.Black.ToArgb())
                        {
                            continue;
                        }

                        //2)
                        bp = 0;
                        if (i + 1 < b.Width && b.GetPixel(i + 1, j).ToArgb() == Color.Black.ToArgb())
                        {
                            bp++;
                        }
                        if (j - 1 > 0 && i + 1 < b.Width && b.GetPixel(i + 1, j - 1).ToArgb() == Color.Black.ToArgb())
                        {
                            bp++;
                        }
                        if (j - 1 > 0 && b.GetPixel(i, j - 1).ToArgb() == Color.Black.ToArgb())
                        {
                            bp++;
                        }
                        if (i - 1 > 0 && j - 1 > 0 && b.GetPixel(i - 1, j - 1).ToArgb() == Color.Black.ToArgb())
                        {
                            bp++;
                        }
                        if (i - 1 > 0 && b.GetPixel(i - 1, j).ToArgb() == Color.Black.ToArgb())
                        {
                            bp++;
                        }
                        if (i - 1 > 0 && j + 1 < b.Height && b.GetPixel(i - 1, j + 1).ToArgb() == Color.Black.ToArgb())
                        {
                            bp++;
                        }
                        if (j + 1 < b.Height && b.GetPixel(i, j + 1).ToArgb() == Color.Black.ToArgb())
                        {
                            bp++;
                        }
                        if (i + 1 < b.Width && j + 1 < b.Height && b.GetPixel(i + 1, j + 1).ToArgb() == Color.Black.ToArgb())
                        {
                            bp++;
                        }
                        if (bp < 2 || bp > 6)
                        {
                            continue;
                        }

                        //3)
                        cp = 0;
                        if (i + 1 < b.Width && b.GetPixel(i + 1, j).ToArgb() == Color.White.ToArgb())
                        {
                            if (j - 1 > 0 && i + 1 < b.Width && b.GetPixel(i + 1, j - 1).ToArgb() == Color.Black.ToArgb())
                            {
                                cp++;
                            }
                        }
                        if (j - 1 > 0 && i + 1 < b.Width && b.GetPixel(i + 1, j - 1).ToArgb() == Color.White.ToArgb())
                        {
                            if (j - 1 > 0 && b.GetPixel(i, j - 1).ToArgb() == Color.Black.ToArgb())
                            {
                                cp++;
                            }
                        }
                        if (j - 1 > 0 && b.GetPixel(i, j - 1).ToArgb() == Color.White.ToArgb())
                        {
                            if (i - 1 > 0 && j - 1 > 0 && b.GetPixel(i - 1, j - 1).ToArgb() == Color.Black.ToArgb())
                            {
                                cp++;
                            }
                        }
                        if (i - 1 > 0 && j - 1 > 0 && b.GetPixel(i - 1, j - 1).ToArgb() == Color.White.ToArgb())
                        {
                            if (i - 1 > 0 && b.GetPixel(i - 1, j).ToArgb() == Color.Black.ToArgb())
                            {
                                cp++;
                            }
                        }
                        if (i - 1 > 0 && b.GetPixel(i - 1, j).ToArgb() == Color.White.ToArgb())
                        {
                            if (i - 1 > 0 && j + 1 < b.Height && b.GetPixel(i - 1, j + 1).ToArgb() == Color.Black.ToArgb())
                            {
                                cp++;
                            }
                        }
                        if (i - 1 > 0 && j + 1 < b.Height && b.GetPixel(i - 1, j + 1).ToArgb() == Color.White.ToArgb())
                        {
                            if (j + 1 < b.Height && b.GetPixel(i, j + 1).ToArgb() == Color.Black.ToArgb())
                            {
                                cp++;
                            }
                        }
                        if (j + 1 < b.Height && b.GetPixel(i, j + 1).ToArgb() == Color.White.ToArgb())
                        {
                            if (i + 1 < b.Width && j + 1 < b.Height && b.GetPixel(i + 1, j + 1).ToArgb() == Color.Black.ToArgb())
                            {
                                cp++;
                            }
                        }
                        if (i + 1 < b.Width && j + 1 < b.Height && b.GetPixel(i + 1, j + 1).ToArgb() == Color.White.ToArgb())
                        {
                            if (i + 1 < b.Width && b.GetPixel(i + 1, j).ToArgb() == Color.Black.ToArgb())
                            {
                                cp++;
                            }
                        }
                        if (cp != 1)
                        {
                            continue;
                        }

                        //4)
                        d = true;
                        if (i + 1 >= b.Width || (i + 1 < b.Width && b.GetPixel(i + 1, j).ToArgb() == Color.White.ToArgb()))
                        {
                            if (j - 1 < 0 || (j - 1 > 0 && b.GetPixel(i, j - 1).ToArgb() == Color.White.ToArgb()))
                            {
                                if (i - 1 < 0 || (i - 1 > 0 && b.GetPixel(i - 1, j).ToArgb() == Color.White.ToArgb()))
                                {
                                    d = false;
                                }
                            }
                        }
                        if (d)
                        {
                            if (j - 2 > 0 && b.GetPixel(i, j - 2).ToArgb() == Color.Black.ToArgb())
                            {
                                d = false;
                            }
                        }
                        if (d)
                        {
                            continue;
                        }

                        //5)
                        d = true;
                        if (i + 1 >= b.Width || (i + 1 < b.Width && b.GetPixel(i + 1, j).ToArgb() == Color.White.ToArgb()))
                        {
                            if (j - 1 < 0 || (j - 1 > 0 && b.GetPixel(i, j - 1).ToArgb() == Color.White.ToArgb()))
                            {
                                if (i + 1 >= b.Width || (i + 1 < b.Width && b.GetPixel(i + 1, j).ToArgb() == Color.White.ToArgb()))
                                {
                                    d = false;
                                }
                            }
                        }
                        if (d)
                        {
                            if (i + 2 < b.Width && b.GetPixel(i + 2, j).ToArgb() == Color.Black.ToArgb())
                            {
                                d = false;
                            }
                        }
                        if (d)
                        {
                            continue;
                        }

                        zwdeletable.Add((i, j));
                    }
                }
                foreach ((int, int)p in zwdeletable)
                {
                    b.SetPixel(p.Item1, p.Item2, Color.White);
                }
                if (stop)
                {
                    win.UpdateImage(b);
                    System.Threading.Thread.Sleep(stopValue);
                }
                if (save)
                {
                    saveImage = b;
                    saveImage.Save("ZhangAndWang" + SaveValue.ToString() + ".png", ImageFormat.Png);
                    SaveValue++;
                }
            } while (zwdeletable.Count != 0);
            return(b);
        }