Пример #1
0
        internal static void DrawInnerCircle(ByteImage originalBitmapTbl, Tuple <int, int, int> pupCenter)
        {
            int x = pupCenter.Item1;
            int y = pupCenter.Item2;
            int r = pupCenter.Item3;

            try
            {
                originalBitmapTbl.setPixel(x, y, 255, 255, 0, 0);
                originalBitmapTbl.setPixel(x + 1, y, 255, 255, 0, 0);
                originalBitmapTbl.setPixel(x - 1, y, 255, 255, 0, 0);
                originalBitmapTbl.setPixel(x, y + 1, 255, 255, 0, 0);
                originalBitmapTbl.setPixel(x, y - 1, 255, 255, 0, 0);

                for (int t = 0; t < 360; t++)
                {
                    var a = (int)(x - (r * Math.Cos(t * Math.PI / 180)));
                    var b = (int)(y - (r * Math.Sin(t * Math.PI / 180)));
                    if (a >= 0 && b >= 0 && a < originalBitmapTbl.Width && b < originalBitmapTbl.Height)
                    {
                        try
                        {
                            originalBitmapTbl.setPixel(a, b, 255, 0, 0, 255);
                        }
                        catch (Exception e) { };
                    }
                }
            }
            catch (Exception ex) { }
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
0
        private void performLoadingPictures(BitmapImage btmi, string FileName, int mode)
        {
            if (mode == 0)
            {
                newBmp = (Bitmap)Bitmap.FromFile(FileName);
                var temp = new BitmapImage(new Uri(FileName));
                WnewBmp   = new WriteableBitmap(temp);
                newBmpTbl = new ByteImage(WnewBmp, newBmp);

                originalBitmap    = (Bitmap)Bitmap.FromFile(FileName);
                WoriginalBitmap   = new WriteableBitmap(temp);
                originalBitmapTbl = new ByteImage(WoriginalBitmap, newBmp);

                codeBitmap    = (Bitmap)Bitmap.FromFile(FileName);
                WcodeBitmap   = new WriteableBitmap(temp);
                codeBitmapTbl = new ByteImage(WoriginalBitmap, newBmp);
            }
            else if (mode == 1)
            {
                newBmp2 = (Bitmap)Bitmap.FromFile(FileName);
                var temp2 = new BitmapImage(new Uri(FileName));
                WnewBmp2   = new WriteableBitmap(temp2);
                newBmpTbl2 = new ByteImage(WnewBmp2, newBmp2);

                originalBitmap2    = (Bitmap)Bitmap.FromFile(FileName);
                WoriginalBitmap2   = new WriteableBitmap(temp2);
                originalBitmapTbl2 = new ByteImage(WoriginalBitmap2, newBmp2);

                codeBitmap2    = (Bitmap)Bitmap.FromFile(FileName);
                WcodeBitmap2   = new WriteableBitmap(temp2);
                codeBitmapTbl2 = new ByteImage(WoriginalBitmap2, newBmp2);
            }
        }
Пример #5
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)));
                }
            }
        }
Пример #6
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);
            }
        }
Пример #7
0
        public static byte[] GetSingleLine(ByteImage bitmap, int x, int y, int R, double[] coss, double[] sins, int mode = 0)
        {
            int wid = (int)bitmap.Width;
            int hig = (int)bitmap.Height;

            List <byte> pixels = new List <byte>();

            for (int t = 0; t < 360; t++)
            {
                if ((mode == 1 && isTinCone(t)) || (mode == 2 && isTinSecondCone(t)) || (mode == 3 && isTinBiggestCone(t)) || mode == 0)
                {
                    var a = (int)(x - (R * coss[t]));
                    var b = (int)(y - (R * sins[t]));
                    int a2;
                    int b2;
                    if (t + 1 >= 360)
                    {
                        a2 = (int)(x - (R * coss[0]));
                        b2 = (int)(y - (R * sins[0]));
                    }
                    else
                    {
                        a2 = (int)(x - (R * coss[t + 1]));
                        b2 = (int)(y - (R * sins[t + 1]));
                    }

                    if (Math.Abs(a2 - a) == 0 && Math.Abs(b2 - b) == 0)
                    {
                        pixels.Add(SinglePoints(bitmap, a, b));
                    }
                    else if (Math.Abs(a2 - a) > Math.Abs(b2 - b) && Math.Abs(a2 - a) != 0 && Math.Abs(b2 - b) != 0)
                    {
                        //bardziej poziomo
                        pixels.AddRange(MoreHorizontal(bitmap, a, b, a2, b2));
                    }
                    else if (Math.Abs(a2 - a) <= Math.Abs(b2 - b) && Math.Abs(a2 - a) != 0 && Math.Abs(b2 - b) != 0)
                    {
                        //bardziej pionowo
                        pixels.AddRange(MoreVertical(bitmap, a, b, a2, b2));
                    }
                    else if (Math.Abs(a2 - a) == 0 && Math.Abs(b2 - b) != 0)
                    {
                        //pionowo
                        pixels.AddRange(Vertical(bitmap, a, b, a2, b2));
                    }
                    else if (Math.Abs(a2 - a) != 0 && Math.Abs(b2 - b) == 0)
                    {
                        //poziomo
                        pixels.AddRange(Horizontal(bitmap, a, b, a2, b2));
                    }
                    else
                    {
                        Console.WriteLine("nie weszlo");
                    }
                }
            }
            return(pixels.ToArray());
        }
Пример #8
0
        public static void Gauss(ByteImage btm)
        {
            double a = 2;

            double[,] wages = new double[3, 3] {
                { 1, a, 1 }, { a, a *a, a }, { 1, a, 1 }
            };
            GaussFilter(btm, a, wages);
        }
Пример #9
0
        public static void Contrast(ByteImage btm)
        {
            PrimitiveContrast pc = new PrimitiveContrast(30, 255, 255, 0);

            for (int x = 0; x < btm.Width; x++)
            {
                for (int y = 0; y < btm.Height; y++)
                {
                    btm.setPixel(x, y, pc.GetColor(btm.getPixel(x, y)));
                }
            }
        }
Пример #10
0
        public static void IrisContrast(ByteImage newBmpTbl)
        {
            PrimitiveContrast pc = new PrimitiveContrast(0, 255, 185, 0);

            for (int x = 0; x < newBmpTbl.Width; x++)
            {
                for (int y = 0; y < newBmpTbl.Height; y++)
                {
                    newBmpTbl.setPixel(x, y, pc.GetColor(newBmpTbl.getPixel(x, y)));
                }
            }
        }
Пример #11
0
 public static void GrayScale(ByteImage btm)
 {
     for (int x = 0; x < btm.Width; x++)
     {
         for (int y = 0; y < btm.Height; y++)
         {
             byte[] oldColour;
             oldColour = btm.getPixel(x, y);
             var value = (0.2 * (double)oldColour[1]) + (0.7 * (double)oldColour[2]) + (0.1 * (double)oldColour[3]);
             btm.setPixel(x, y, oldColour[0], (byte)value, (byte)value, (byte)value);
         }
     }
 }
Пример #12
0
 public static void CuttOffIris(ByteImage btm, Tuple <System.Drawing.Point, int> pupil, Tuple <System.Drawing.Point, int> iris)
 {
     for (int x = 0; x < btm.Width; x++)
     {
         for (int y = 0; y < btm.Height; y++)
         {
             if (IsInsideCircle(pupil, x, y) || !IsInsideCircle(iris, x, y))
             {
                 btm.setPixel(x, y, 255, 255, 255, 255);
             }
         }
     }
 }
Пример #13
0
        public ByteImage(ByteImage bitmap)
        {
            Width  = bitmap.Width;
            Height = bitmap.Height;
            int size = Width * Height * 4;

            Pixels = new byte[size];
            Stride = bitmap.Stride;
            for (int x = 0; x < size; x++)
            {
                Pixels[x] = bitmap.Pixels[x];
            }
            Bitmap = new Bitmap(bitmap.Bitmap);
        }
Пример #14
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));
                }
            }
        }
Пример #15
0
 public async Task DrawLines(int mode)
 {
     if (mode == 0)
     {
         await Task.Run(() =>
         {
             var tup       = Helper.DrawLines(newBmpTbl, pupilX, pupilY, pupilR, irisX, irisY, irisR);
             codeBitmapTbl = tup.Item1;
             finalcode1    = tup.Item2;
         });
     }
     else if (mode == 1)
     {
         await Task.Run(() =>
         {
             var tup2       = Helper.DrawLines(newBmpTbl2, pupilX2, pupilY2, pupilR2, irisX2, irisY2, irisR2);
             codeBitmapTbl2 = tup2.Item1;
             finalcode2     = tup2.Item2;
         });
     }
 }
Пример #16
0
        private async void Lines1_Button(object sender, RoutedEventArgs e)
        {
            if ((!(newBmp != null && newBmpTbl != null) && pupilR != 0 && irisR != 0) && !(newBmp2 != null && newBmpTbl2 != null) && pupilR2 != 0 && irisR2 != 0)
            {
                MessageBox.Show("Load image!");
                return;
            }
            BlakWait.Visibility = Visibility.Visible;

            if ((newBmp != null && newBmpTbl != null) && pupilR != 0 && irisR != 0 && !pic1Calculated)
            {
                newBmpTbl = new ByteImage(originalBitmapTbl);
                await RunGreyScale(0);
                await DrawLines(0);

                newBmpTbl = new ByteImage(originalBitmapTbl);
                await CutOffStuff(0);

                img.Source     = newBmpTbl.ToBitmapSource();
                code.Source    = codeBitmapTbl.ToBitmapSource();
                pic1Calculated = true;
            }

            if ((newBmp2 != null && newBmpTbl2 != null) && pupilR2 != 0 && irisR2 != 0 && !pic2Calculated)
            {
                newBmpTbl2 = new ByteImage(originalBitmapTbl2);
                await RunGreyScale(1);
                await DrawLines(1);

                newBmpTbl2 = new ByteImage(originalBitmapTbl2);
                await CutOffStuff(1);

                img2.Source    = newBmpTbl2.ToBitmapSource();
                code2.Source   = codeBitmapTbl2.ToBitmapSource();
                pic2Calculated = true;
            }

            BlakWait.Visibility = Visibility.Collapsed;
            Console.WriteLine("Test.");
        }
Пример #17
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));
                    }
                }
            }
        }
Пример #18
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);
        }
Пример #19
0
        private async Task CutOffIris(int mode)
        {
            if (mode == 0)
            {
VERYBAD:
                pupilX    = 0;
                pupilY    = 0;
                pupilR    = 0;
                irisX     = 0;
                irisY     = 0;
                irisR     = 0;
                newBmpTbl = new ByteImage(originalBitmapTbl);

                await RunGreyScale(0);
                await ThreeColors(0);

                for (int i = 0; i < 2; i++)
                {
                    await RunGaussFilter(0);
                    await RunBlack(0);
                }
                await RunRemoveSingleNoises(0);

                await Task.Run(() =>
                {
                    Tuple <int, int, int> PupCenter = Helper.PupilCenter(borderColor, newBmpTbl);
                    pupilX = PupCenter.Item1;
                    pupilY = PupCenter.Item2;
                    pupilR = PupCenter.Item3;
                });

                //--------------
                newBmpTbl = new ByteImage(originalBitmapTbl);
                await RunContrast(0);
                await RunGreyScale(0);
                await RunContrast(0);
                await RunGaussFilter(0);

                await RunBlack(0);
                await FiveColors(0);

                //await RunFullBlack();

                await RunRemoveSingleNoises(0);

                await Task.Run(() =>
                {
                    Tuple <int, int, int> IrisCenter = Helper.Iris(borderIrisColor, newBmpTbl, pupilX, pupilY, pupilR);
                    irisX = IrisCenter.Item1;
                    irisY = IrisCenter.Item2;
                    irisR = IrisCenter.Item3;
                });

                if (irisR == -1)
                {
                    goto VERYBAD;
                }
                newBmpTbl = new ByteImage(originalBitmapTbl);
                await CutOffStuff(0);

                img.Source = newBmpTbl.ToBitmapSource();
            }
            else if (mode == 1)
            {
VERYBAD2:
                pupilX2    = 0;
                pupilY2    = 0;
                pupilR2    = 0;
                irisX2     = 0;
                irisY2     = 0;
                irisR2     = 0;
                newBmpTbl2 = new ByteImage(originalBitmapTbl2);

                await RunGreyScale(1);
                await ThreeColors(1);

                for (int i = 0; i < 2; i++)
                {
                    await RunGaussFilter(1);
                    await RunBlack(1);
                }
                await RunRemoveSingleNoises(1);

                await Task.Run(() =>
                {
                    Tuple <int, int, int> PupCenter = Helper.PupilCenter(borderColor2, newBmpTbl2);
                    pupilX2 = PupCenter.Item1;
                    pupilY2 = PupCenter.Item2;
                    pupilR2 = PupCenter.Item3;
                });

                //--------------
                newBmpTbl2 = new ByteImage(originalBitmapTbl2);
                await RunContrast(1);
                await RunGreyScale(1);
                await RunContrast(1);
                await RunGaussFilter(1);

                await RunBlack(1);
                await FiveColors(1);

                //await RunFullBlack();

                await RunRemoveSingleNoises(1);

                await Task.Run(() =>
                {
                    Tuple <int, int, int> IrisCenter = Helper.Iris(borderIrisColor2, newBmpTbl2, pupilX2, pupilY2, pupilR2);
                    irisX2 = IrisCenter.Item1;
                    irisY2 = IrisCenter.Item2;
                    irisR2 = IrisCenter.Item3;
                });

                if (irisR2 == -1)
                {
                    goto VERYBAD2;
                }
                newBmpTbl2 = new ByteImage(originalBitmapTbl2);
                await CutOffStuff(1);

                img2.Source = newBmpTbl2.ToBitmapSource();
            }
        }
Пример #20
0
        internal static Tuple <ByteImage, byte[]> DrawLines(ByteImage newBmpTbl, int pupilX, int pupilY, int pupilR, int irisX, int irisY, int irisR)
        {
            byte[] firstLine;
            byte[] secondLine;
            byte[] thirdLine;
            byte[] forthLine;
            byte[] fifthLine;
            byte[] sixthLine;
            byte[] seventhLine;
            byte[] eighthLine;

            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 dist = ((irisR - ((irisR - pupilR) / 4)) - pupilR) / 8;


            firstLine  = GetSingleLine(newBmpTbl, pupilX, pupilY, pupilR + (1 * dist), coss, sins, 1);
            secondLine = GetSingleLine(newBmpTbl, pupilX, pupilY, pupilR + (2 * dist), coss, sins, 1);
            thirdLine  = GetSingleLine(newBmpTbl, pupilX, pupilY, pupilR + (3 * dist), coss, sins, 1);
            forthLine  = GetSingleLine(newBmpTbl, pupilX, pupilY, pupilR + (4 * dist), coss, sins, 1);

            fifthLine = GetSingleLine(newBmpTbl, pupilX, pupilY, pupilR + (5 * dist), coss, sins, 2);
            sixthLine = GetSingleLine(newBmpTbl, pupilX, pupilY, pupilR + (6 * dist), coss, sins, 2);

            seventhLine = GetSingleLine(newBmpTbl, pupilX, pupilY, pupilR + (7 * dist), coss, sins, 3);
            eighthLine  = GetSingleLine(newBmpTbl, pupilX, pupilY, pupilR + (8 * dist), coss, sins, 3);

            List <byte[]> lines = new List <byte[]>();

            lines.Add(GetGradients(firstLine));
            lines.Add(GetGradients(secondLine));
            lines.Add(GetGradients(thirdLine));
            lines.Add(GetGradients(forthLine));
            lines.Add(GetGradients(fifthLine));
            lines.Add(GetGradients(sixthLine));
            lines.Add(GetGradients(seventhLine));
            lines.Add(GetGradients(eighthLine));


            Bitmap btm = new Bitmap(256 * 2, (3 * 8), newBmpTbl.Bitmap.PixelFormat);

            for (int x = 0; x < btm.Width; x++)
            {
                for (int y = 0; y < btm.Height; y++)
                {
                    btm.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                }
            }

            for (int i = 0; i < 8; i++)
            {
                int counter = 0;
                for (int x = 0; x < lines[i].Length - 1; x++)
                {
                    btm.SetPixel(counter, (i * 3), Color.FromArgb(lines[i][x], lines[i][x], lines[i][x]));
                    btm.SetPixel(counter, (i * 3) + 1, Color.FromArgb(lines[i][x], lines[i][x], lines[i][x]));
                    btm.SetPixel(counter, (i * 3) + 2, Color.FromArgb(lines[i][x], lines[i][x], lines[i][x]));

                    btm.SetPixel(counter + 1, (i * 3), Color.FromArgb(lines[i][x], lines[i][x], lines[i][x]));
                    btm.SetPixel(counter + 1, (i * 3) + 1, Color.FromArgb(lines[i][x], lines[i][x], lines[i][x]));
                    btm.SetPixel(counter + 1, (i * 3) + 2, Color.FromArgb(lines[i][x], lines[i][x], lines[i][x]));
                    counter += 2;
                }
                btm.SetPixel(btm.Width - 2, (i * 3), Color.FromArgb(lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1]));
                btm.SetPixel(btm.Width - 2, (i * 3) + 1, Color.FromArgb(lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1]));
                btm.SetPixel(btm.Width - 2, (i * 3) + 2, Color.FromArgb(lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1]));

                btm.SetPixel(btm.Width - 1, (i * 3), Color.FromArgb(lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1]));
                btm.SetPixel(btm.Width - 1, (i * 3) + 1, Color.FromArgb(lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1]));
                btm.SetPixel(btm.Width - 1, (i * 3) + 2, Color.FromArgb(lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1], lines[i][lines[i].Length - 1]));
            }


            System.Windows.Media.Imaging.BitmapSource    bitmapSource    = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(btm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            System.Windows.Media.Imaging.WriteableBitmap writeableBitmap = new System.Windows.Media.Imaging.WriteableBitmap(bitmapSource);

            var         imageForLines = new ByteImage(writeableBitmap, btm);
            List <byte> totalCode     = new List <byte>();

            for (int i = 0; i < 8; i++)
            {
                totalCode.AddRange(lines[i]);
            }
            return(new Tuple <ByteImage, byte[]>(imageForLines, totalCode.ToArray()));
        }
Пример #21
0
        public static Tuple <int, int, int> PupilCenter(int borderColor, ByteImage bitmap)
        {
            int threads = 8;

            ByteImage[]         pictures = new ByteImage[threads];
            ConcurrentBag <int> Xs       = new ConcurrentBag <int>();
            ConcurrentBag <int> Ys       = new ConcurrentBag <int>();
            ConcurrentBag <int> Rs       = new ConcurrentBag <int>();

            for (int i = 0; i < threads; i++)
            {
                pictures[i] = new ByteImage(bitmap);
            }
            int borderValue      = (int)((0.85 * (double)borderColor) / 3);
            int centerX          = (int)bitmap.Width / 2;
            int centerY          = (int)bitmap.Height / 2;
            int R                = 1;
            int verticalMargin   = (int)bitmap.Height / 4;
            int horizontalMargin = (int)bitmap.Width / 4;

            int w = (int)bitmap.Width / threads;

            Parallel.For(0, threads, i =>
                         //for (int i = 0; i < threads; i++)
            {
                int tempX = 0;
                int tempY = 0;
                int tempR = 0;
                for (int x = i * w; x < (i + 1) * w; x++)
                {
                    for (int y = 0; y < pictures[i].Height; y++)
                    {
                        if (x > horizontalMargin && x < pictures[i].Width - horizontalMargin && y > verticalMargin && y < pictures[i].Height - verticalMargin)
                        {
                            if (x == pictures[i].Width / 2 && y == pictures[i].Height / 2)
                            {
                                Console.WriteLine("test");
                            }
                            if (pictures[i].Pixels[pictures[i].getPixelIndex(x, y) + 1] == 0)
                            {
                                int currR       = 0;
                                int[] contrasts = FindContrasts((int)pictures[i].Width / 4, x, y, pictures[i]);
                                for (int c = 0; c < contrasts.Length; c++)
                                {
                                    if (contrasts[c] >= borderValue)
                                    {
                                        currR = c;
                                        break;
                                    }
                                }
                                if (currR > tempR)
                                {
                                    tempX = x;
                                    tempY = y;
                                    tempR = currR;
                                }
                            }
                        }
                    }
                }
                Xs.Add(tempX);
                Ys.Add(tempY);
                Rs.Add(tempR);
            });
            //}

            for (int i = 0; i < threads; i++)
            {
                if (Rs.ElementAt(i) > R)
                {
                    R       = Rs.ElementAt(i);
                    centerX = Xs.ElementAt(i);
                    centerY = Ys.ElementAt(i);
                }
            }

            return(new Tuple <int, int, int>(centerX, centerY, R));
        }
Пример #22
0
        public static Tuple <int, int, int> Iris(int borderColor, ByteImage bitmap, int px, int py, int pr)
        {
            if (px == 0 || py == 0 || pr == 0)
            {
                return(new Tuple <int, int, int>(-1, -1, -1));
            }
            int threads = 8;

            ByteImage[]         pictures = new ByteImage[threads];
            ConcurrentBag <int> Xs       = new ConcurrentBag <int>();
            ConcurrentBag <int> Ys       = new ConcurrentBag <int>();
            ConcurrentBag <int> Rs       = new ConcurrentBag <int>();

            for (int i = 0; i < threads; i++)
            {
                pictures[i] = new ByteImage(bitmap);
            }
            int borderValue = (int)(((double)borderColor / 5) * 3);
            int centerX     = px;
            int centerY     = py;
            int R           = pr;

            int w = 12 / threads;

            Parallel.For(0, threads, i =>
                         //for (int i = 0; i < threads; i++)
            {
                int tempX = 0;
                int tempY = 0;
                int tempR = 0;
                //for (int x = px - (i * w); x < px + ((i + 1) * w); x++)
                //{
                // for (int y = py - 9; y < py + 9; y++)
                // {
                //if (pictures[i].Pixels[pictures[i].getPixelIndex(x, y) + 1] == 0)
                //{
                int currR       = 0;
                tempX           = px;
                tempY           = py;
                int[] contrasts = FindContrasts((int)pictures[i].Width / 4 + pr, px, py, pictures[i], (pr + 10));
                for (int c = 0; c < contrasts.Length; c++)
                {
                    if (contrasts[c] >= borderValue)
                    {
                        currR = c;
                        break;
                    }
                }
                if (currR > tempR)
                {
                    tempR = currR;
                }
                //}
                // }
                // }
                Xs.Add(tempX);
                Ys.Add(tempY);
                Rs.Add(tempR);
            });
            //}

            for (int i = 0; i < threads; i++)
            {
                if (Rs.ElementAt(i) > R)
                {
                    R       = Rs.ElementAt(i);
                    centerX = Xs.ElementAt(i);
                    centerY = Ys.ElementAt(i);
                }
            }
            R = R + pr;
            return(new Tuple <int, int, int>(centerX, centerY, R));
        }
Пример #23
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);
        }