示例#1
0
        public static Bitmap EdgeDetectBitmap(Bitmap bmp, double[,] filterMatX, double[,] filterMatY, int edgelimit, bool grayscale)
        {
            var pixelSource  = TypesAndFiles.BitmapToByteArray(bmp);
            var pixelsResult = new byte[pixelSource.Length];

            if (grayscale)
            {
                pixelSource = TypesAndFiles.GrayscaleByteArray(pixelSource);
            }

            var filterWidth  = filterMatX.GetLength(1);
            var filterOffset = (filterWidth - 1) / 2;

            for (var yy = filterOffset; yy < bmp.Height - filterOffset; yy++)
            {
                for (var xx = filterOffset; xx < bmp.Width - filterOffset; xx++)
                {
                    var gx = 0.0;
                    var gy = 0.0;

                    var byteOffset = (yy * bmp.Width + xx) * 4;

                    // Applying filters
                    for (var fx = -filterOffset; fx <= filterOffset; fx++)
                    {
                        for (var fy = -filterOffset; fy <= filterOffset; fy++)
                        {
                            var calcOffset = byteOffset + (fy + fx * bmp.Width) * 4;

                            gx += (pixelSource[calcOffset + 0]) * filterMatX[fx + filterOffset, fy + filterOffset];

                            gy += (pixelSource[calcOffset + 0]) * filterMatY[fx + filterOffset, fy + filterOffset];
                        }
                    }

                    var grad = DataLimit255(Math.Sqrt(gx * gx + gy * gy));

                    if (grad < edgelimit)
                    {
                        grad = 0;
                    }

                    pixelsResult[byteOffset + 0] = (byte)(grad);
                    pixelsResult[byteOffset + 1] = (byte)(grad);
                    pixelsResult[byteOffset + 2] = (byte)(grad);
                    pixelsResult[byteOffset + 3] = 255;
                }
            }

            var resultBitmap = TypesAndFiles.ByteArrayToBitmap(pixelsResult, bmp.Width, bmp.Height);

            return(resultBitmap);
        }
示例#2
0
        public static Bitmap SquaredImage(Bitmap bmp, int edgeLimit, bool grayscale)
        {
            var shapes  = ShapeRectList(bmp, edgeLimit);
            var bmpData = TypesAndFiles.BitmapToByteArray(bmp);

            if (grayscale)
            {
                bmpData = TypesAndFiles.GrayscaleByteArray(bmpData);
            }
            var bmpSquares = SquareMarker(bmpData, bmp.Width, shapes, 2);

            return(TypesAndFiles.ByteArrayToBitmap(bmpSquares, bmp.Width, bmp.Height));
        }
示例#3
0
        public static Bitmap ShapeComparison(Bitmap subjectBitmap, Bitmap logoBitmap, int edgeLimitSub, int edgeLimitLogo,
                                             double hitRate, bool grayscale)
        {
            var rectListSub  = ShapeRectList(subjectBitmap, edgeLimitSub);
            var rectListLogo = ShapeRectList(logoBitmap, edgeLimitLogo);

            var pixelsSubject = TypesAndFiles.BitmapToByteArray(subjectBitmap);
            var pixelsLogo    = TypesAndFiles.BitmapToByteArray(logoBitmap);
            var pixelResult   = pixelsSubject;

            if (grayscale)
            {
                pixelsSubject = TypesAndFiles.GrayscaleByteArray(pixelsSubject);
                pixelsLogo    = TypesAndFiles.GrayscaleByteArray(pixelsLogo);
            }

            //var compareList = new List<Point>();
            var previousHits = new List <double>();

            //FileReader.ExtractArray(pixelsSubject, rectListSub, subjectBitmap.Width, 0);

            for (var i = 0; i < rectListSub.Count / 2; i++)
            {
                previousHits.Add(0.0);
            }

            // Comparison by subtraction
            for (var j = 0; j < rectListLogo.Count; j += 2)
            {
                for (var i = 0; i < rectListSub.Count; i += 2)
                {
                    var subShapeWidth  = rectListSub[i + 1].X - rectListSub[i].X + 1;
                    var subShapeHeight = rectListSub[i + 1].Y - rectListSub[i].Y + 1;

                    var logoShapeWidth  = rectListLogo[j + 1].X - rectListLogo[j].X + 1;
                    var logoShapeHeight = rectListLogo[j + 1].Y - rectListLogo[j].Y + 1;

                    var newArrSub  = TypesAndFiles.ExtractArray(pixelsSubject, rectListSub, subjectBitmap.Width, i);
                    var newArrLogo = TypesAndFiles.ExtractArray(pixelsLogo, rectListLogo, logoBitmap.Width, j);

                    var rate = 0.0;
                    var b    = 0;

                    if (logoShapeHeight == subShapeHeight && logoShapeWidth == subShapeWidth)
                    {
                        var db = 0.0;
                        var dg = 0.0;
                        var dr = 0.0;

                        for (var yy = 0; yy < logoShapeHeight; yy++)
                        {
                            for (var xx = 0; xx < logoShapeWidth; xx++)
                            {
                                var byteOffset = (yy * logoShapeWidth + xx) * 4;

                                db += Math.Abs(newArrSub[byteOffset + 0] - newArrLogo[byteOffset + 0]);
                                dg += Math.Abs(newArrSub[byteOffset + 1] - newArrLogo[byteOffset + 1]);
                                dr += Math.Abs(newArrSub[byteOffset + 2] - newArrLogo[byteOffset + 2]);
                                b++;
                            }
                        }

                        db   = db / (b * 255);
                        dg   = dg / (b * 255);
                        dr   = dr / (b * 255);
                        rate = 1 - (db + dg + dr) / 3;

                        if (previousHits[i / 2] > rate)
                        {
                            continue;
                        }
                        previousHits[i / 2] = rate;
                    }

                    else
                    {
                        var newArrSubScaled = Transformations.TransScaling(newArrSub, subShapeHeight, subShapeWidth, logoShapeHeight, logoShapeWidth);

                        var db = 0.0;
                        var dg = 0.0;
                        var dr = 0.0;

                        for (var yy = 0; yy < logoShapeHeight; yy++)
                        {
                            for (var xx = 0; xx < logoShapeWidth; xx++)
                            {
                                var byteOffset = (yy * logoShapeWidth + xx) * 4;

                                db += Math.Abs(newArrSubScaled[byteOffset + 0] - newArrLogo[byteOffset + 0]);
                                dg += Math.Abs(newArrSubScaled[byteOffset + 1] - newArrLogo[byteOffset + 1]);
                                dr += Math.Abs(newArrSubScaled[byteOffset + 2] - newArrLogo[byteOffset + 2]);
                                b++;
                            }
                        }

                        db   = db / (b * 255);
                        dg   = dg / (b * 255);
                        dr   = dr / (b * 255);
                        rate = 1 - (db + dg + dr) / 3;

                        if (previousHits[i / 2] > rate)
                        {
                            continue;
                        }
                        previousHits[i / 2] = rate;
                    }
                }
            }

            // Marking every shape wwith a hitrate above hitcap
            for (var j = 0; j < rectListSub.Count; j += 2)
            {
                if (previousHits[j / 2] < hitRate)
                {
                    continue;
                }
                var shapelist = new List <Point> {
                    rectListSub[j], rectListSub[j + 1]
                };
                pixelResult = SquareMarker(pixelResult, subjectBitmap.Width, shapelist, previousHits[j / 2]);
            }

            // Kunne brukt pixelSubject, men god huskeregel å ikke endre data man leser av.

            MainWindow.HitRatesPercent = previousHits;

            var resultBitmap = TypesAndFiles.ByteArrayToBitmap(pixelResult, subjectBitmap.Width, subjectBitmap.Height);

            return(resultBitmap);
        }