Ejemplo n.º 1
0
        public static void GaussFilter(ByteImage btm, double a, double[,] wages)
        {
            ByteImage tempPict = new ByteImage(btm);

            for (int x = 0; x < tempPict.Width; x++)
            {
                for (int y = 0; y < tempPict.Height; y++)
                {
                    var    currPix = tempPict.Pixels[tempPict.getPixelIndex(x, y)];
                    double sumR    = 0.0;
                    double sumG    = 0.0;
                    double sumB    = 0.0;
                    double dividor = 0.0;
                    for (int x2 = -1; x2 < 2; x2++)
                    {
                        for (int y2 = -1; y2 < 2; y2++)
                        {
                            if (x + x2 >= 0 && y + y2 >= 0 && x + x2 < tempPict.Width && y + y2 < tempPict.Height)
                            {
                                double currWage = wages[x2 + 1, y2 + 1];
                                var    xx       = x + x2;
                                var    yy       = y + y2;
                                sumR    += tempPict.Pixels[tempPict.getPixelIndex(xx, yy) + 1] * currWage;
                                sumG    += tempPict.Pixels[tempPict.getPixelIndex(xx, yy) + 2] * currWage;
                                sumB    += tempPict.Pixels[tempPict.getPixelIndex(xx, yy) + 3] * currWage;
                                dividor += currWage;
                            }
                        }
                    }
                    btm.setPixel(x, y, currPix, (byte)FromInterval((int)(sumR / dividor)), (byte)FromInterval((int)(sumG / dividor)), (byte)FromInterval((int)(sumB / dividor)));
                }
            }
        }
Ejemplo n.º 2
0
        public static List <byte> MoreHorizontal(ByteImage bitmap, int a, int b, int a2, int b2)
        {
            var list = new List <byte>();

            for (int i = 0; i < Math.Abs(a2 - a); i++)
            {
                int nX = a + i;
                int nY = (int)(((b2 - b) / (a2 - a) * nX) + (b2 - ((b2 - b) / (a2 - a) * a2)));

                int sum     = 0;
                int counter = 0;
                for (int x2 = -2; x2 < 3; x2++)
                {
                    for (int y2 = -2; y2 < 3; y2++)
                    {
                        if (nX + x2 >= 0 && nY + y2 >= 0 && nX + x2 < bitmap.Width && nY + y2 < bitmap.Height)
                        {
                            sum += bitmap.Pixels[bitmap.getPixelIndex(nX + x2, nY + y2) + 1];
                            counter++;
                        }
                    }
                }
                if (counter > 0)
                {
                    list.Add((byte)(FromInterval(sum / counter)));
                }
                else
                {
                    list.Add(0);
                }
            }
            return(list);
        }
Ejemplo n.º 3
0
        private static List <byte> Vertical(ByteImage bitmap, int a, int b, int a2, int b2)
        {
            List <byte> list = new List <byte>();

            for (int i = -Math.Abs(b2 - b) / 2; i < Math.Abs(b2 - b) / 2; i++)
            {
                int sum     = 0;
                int counter = 0;
                for (int x2 = -2; x2 < 3; x2++)
                {
                    for (int y2 = -2; y2 < 3; y2++)
                    {
                        if (a + x2 >= 0 && b + y2 + i >= 0 && a + x2 < bitmap.Width && b + y2 + i < bitmap.Height)
                        {
                            sum += bitmap.Pixels[bitmap.getPixelIndex(a + x2, b + y2 + i) + 1];
                            counter++;
                        }
                    }
                }
                if (counter > 0)
                {
                    list.Add((byte)(FromInterval(sum / counter)));
                }
                else
                {
                    list.Add(0);
                }
            }
            return(list);
        }
Ejemplo n.º 4
0
        public static byte SinglePoints(ByteImage bitmap, int a, int b)
        {
            int sum     = 0;
            int counter = 0;

            for (int x2 = -2; x2 < 3; x2++)
            {
                for (int y2 = -2; y2 < 3; y2++)
                {
                    if (a + x2 >= 0 && b + y2 >= 0 && a + x2 < bitmap.Width && b + y2 < bitmap.Height)
                    {
                        sum += bitmap.Pixels[bitmap.getPixelIndex(a + x2, b + y2) + 1];
                        counter++;
                    }
                }
            }
            if (counter > 0)
            {
                return((byte)(FromInterval(sum / counter)));
            }
            else
            {
                return(255);
            }
        }
Ejemplo n.º 5
0
        public static void RemoveSingleNoises(ByteImage btm)
        {
            ByteImage tempPict = new ByteImage(btm);
            int       counter  = 0;

            for (int x = 0; x < btm.Width; x++)
            {
                for (int y = 0; y < btm.Height; y++)
                {
                    var oldColour = btm.getPixel(x, y);
                    if (oldColour[1] == 0)
                    {
                        bool isAlone = true;

                        for (int x2 = -1; x2 <= 1; x2++)
                        {
                            for (int y2 = -1; y2 <= 1; y2++)
                            {
                                if (x + x2 >= 0 && x + x2 < btm.Width && y + y2 >= 0 && y + y2 < btm.Height)
                                {
                                    var col = btm.Pixels[btm.getPixelIndex(x + x2, y + y2) + 1];
                                    if (col == 0)
                                    {
                                        isAlone = false;
                                        break;
                                    }
                                }
                            }
                        }

                        if (isAlone)
                        {
                            tempPict.setPixel(x, y, 255, 0, 0, 0);
                        }
                    }
                }
            }
            Console.WriteLine(counter + " pixels removed");
            for (int x = 0; x < btm.Width; x++)
            {
                for (int y = 0; y < btm.Height; y++)
                {
                    btm.setPixel(x, y, tempPict.getPixel(x, y));
                }
            }
        }
Ejemplo n.º 6
0
        public static void RunFullBlack(ByteImage newBmpTbl)
        {
            int bigCounter = 3;

            while (bigCounter > 2)
            {
                ByteImage tempPict = new ByteImage(newBmpTbl);
                bigCounter = 0;
                for (int x = 0; x < newBmpTbl.Width; x++)
                {
                    for (int y = 0; y < newBmpTbl.Height; y++)
                    {
                        int counter = 0;
                        for (int x2 = -1; x2 < 2; x2++)
                        {
                            for (int y2 = -1; y2 < 2; y2++)
                            {
                                if (newBmpTbl.Pixels[newBmpTbl.getPixelIndex(x, y) + 1] != 0 && x + x2 >= 0 && y + y2 >= 0 && x + x2 < newBmpTbl.Width && y + y2 < newBmpTbl.Height && newBmpTbl.Pixels[newBmpTbl.getPixelIndex(x + x2, y + y2) + 1] <= (255 / 6))
                                {
                                    counter++;
                                }
                            }
                        }
                        if (counter >= 5)
                        {
                            tempPict.setPixel(x, y, 255, 0, 0, 0);
                            bigCounter++;
                        }
                    }
                }
                Console.WriteLine("Blacked " + bigCounter + " pixels.");
                for (int x = 0; x < newBmpTbl.Width; x++)
                {
                    for (int y = 0; y < newBmpTbl.Height; y++)
                    {
                        newBmpTbl.setPixel(x, y, tempPict.getPixel(x, y));
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public static int[] FindContrasts(int maxR, int x, int y, ByteImage bitmap, int minR = 0)
        {
            double[] coss = new double[360];
            double[] sins = new double[360];
            for (int t = 0; t < 360; t++)
            {
                coss[t] = Math.Cos(t * Math.PI / 180);
                sins[t] = Math.Sin(t * Math.PI / 180);
            }
            int wid = (int)bitmap.Width;
            int hig = (int)bitmap.Height;

            int[] cList = new int[maxR];
            for (int r = 0; r < maxR; r++)
            {
                int sum     = 0;
                int counter = 0;
                for (int t = 0; t < 360; t++)
                {
                    var a = (int)(x - ((r + minR) * coss[t]));
                    var b = (int)(y - ((r + minR) * sins[t]));
                    if (a >= 0 && b >= 0 && a < wid && b < hig)
                    {
                        sum += bitmap.Pixels[bitmap.getPixelIndex(a, b) + 1];
                        counter++;
                    }
                }
                if (counter > 0)
                {
                    cList[r] = sum / counter;
                }
                else
                {
                    cList[r] = 0;
                }
            }

            return(cList);
        }
Ejemplo n.º 8
0
        public static int ThreeColors(ByteImage newBmpTbl)
        {
            int borderColor = 255;
            int threads     = 8;

            ByteImage[]         pictures = new ByteImage[threads];
            ConcurrentBag <int> mins     = new ConcurrentBag <int>();
            ConcurrentBag <int> maxs     = new ConcurrentBag <int>();
            ConcurrentBag <int> sums     = new ConcurrentBag <int>();
            ConcurrentBag <int> counters = new ConcurrentBag <int>();

            for (int i = 0; i < threads; i++)
            {
                pictures[i] = new ByteImage(newBmpTbl);
            }


            Parallel.For(0, threads, i =>
                         //for (int i = 0; i < threads; i++)
            {
                int min     = 255;
                int max     = 0;
                int totSum  = 0;
                int counter = 0;
                int w       = (int)pictures[i].Width / threads;

                for (int x = i * w; x < (i + 1) * w; x++)
                {
                    for (int y = 0; y < pictures[i].Height; y++)
                    {
                        var col = pictures[i].getPixel(x, y)[1];
                        if (col > max)
                        {
                            max = col;
                        }
                        if (col < min)
                        {
                            min = col;
                        }
                        if (col <= 215)
                        {
                            totSum += col;
                            counter++;
                        }
                    }
                }
                mins.Add(min);
                maxs.Add(max);
                sums.Add(totSum);
                counters.Add(counter);
            });
            //}

            int fmin  = mins.Min();
            int fmax  = maxs.Max();
            int fmid  = sums.Sum() / counters.Sum();
            int fmid1 = (fmid + fmin) / 2;
            int fmid2 = (fmax + fmid) / 2;

            borderColor = fmid2;

            for (int x = 0; x < newBmpTbl.Width; x++)
            {
                for (int y = 0; y < newBmpTbl.Height; y++)
                {
                    var col = newBmpTbl.Pixels[newBmpTbl.getPixelIndex(x, y) + 1];

                    int distMin  = Math.Abs(col - fmin);
                    int distMid1 = Math.Abs(col - fmid1);
                    int distMid2 = Math.Abs(col - fmid2);
                    int distMax  = Math.Abs(col - fmax);

                    int minV = Math.Min(distMin, Math.Min(distMid1, Math.Min(distMid2, distMax)));
                    if (minV == distMin)
                    {
                        newBmpTbl.setPixel(x, y, 255, 0, 0, 0);
                    }
                    else if (minV == distMid1)
                    {
                        newBmpTbl.setPixel(x, y, 255, (byte)fmid2, (byte)fmid2, (byte)fmid2);
                    }
                    else if (minV == distMid2)
                    {
                        newBmpTbl.setPixel(x, y, 255, (byte)fmid2, (byte)fmid2, (byte)fmid2);
                    }
                    else
                    {
                        newBmpTbl.setPixel(x, y, 255, 255, 255, 255);
                    }
                }
            }
            return(borderColor);
        }