Example #1
0
        private void btnScan_Click(object sender, EventArgs e)
        {
            // reset image
            pbScreen.Image = sourceImage;

            var masks = new ArrayList();

            for (var i = 0; i < tcMasks.TabPages.Count; ++i)
            {
                PictureBox pb = (PictureBox)tcMasks.TabPages[i].Controls[0];
                var mask = new MaskItem();
                mask.Image = bitmapConvertor.RoundImage(new Bitmap(pb.Image));
                mask.CalculateColors();
                mask.Name = i.ToString();
                masks.Add(mask);
            }

            var detected = new ArrayList();

            detectionService.Detect(
                bitmapConvertor.ImageToMatrix(new Bitmap(pbScreen.Image)),
                masks,
                detected
            );

            using (Graphics gr = Graphics.FromImage(pbScreen.Image))
            {
                Pen borderPen = new Pen(Color.Red, 1);

                foreach (DetectedItem item in detected)
                {
                    gr.DrawImage(
                        item.Mask.Image,
                        new Point(item.X, item.Y)
                    );
                    gr.DrawRectangle(borderPen, item.Rect);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Поиск изображений на карте
        /// </summary>
        /// <param name="map">Карта</param>
        /// <param name="masks">Искомые изображения</param>
        /// <param name="detected">Результат поиска</param>
        /// <param name="startX"></param>
        /// <param name="startY"></param>
        public bool TestBlock(int[,] map, MaskItem mask, ArrayList detected, int startX, int startY)
        {
            var similarity = 0;

            // similarity / (size.X * size.Y) >= 0.8
            // similarity >= 0.8 * size.X * size.Y

            var minSimilarityX = 0.4 * mask.Width;

            for (int y = 0; y < mask.Height; ++y)
            {
                // Первый блок строки
                for (int x = 0; x < mask.Width; ++x)
                {
                    if (map[startX + x, startY + y] == mask.Map[x, y])
                    {
                        similarity++;
                    }
                }

                if (similarity < Math.Max(2, minSimilarityX * y))
                {
                    return false;
                }
            }

            return similarity >= minSimilarityX * mask.Height;
        }