Ejemplo n.º 1
0
//IntRange(55, 70);
        private void pictureHandle(Bitmap temp_map2)
        {
            if (temp_map2 != null)
            {
                //剪裁图片
                Bitmap temp_map = temp_map2.Clone(new Rectangle(0, 0, temp_map2.Width, temp_map2.Height), temp_map2.PixelFormat);

                //过滤掉没用的颜色
                int            range  = 10;
                ColorFiltering filter = new ColorFiltering();
                filter.Red              = new IntRange(61 - range, 61 + range);
                filter.Green            = new IntRange(49 - range, 49 + range);
                filter.Blue             = new IntRange(91 - range, 91 + range);
                filter.FillOutsideRange = true;
                filter.ApplyInPlace(temp_map);

                //temp_map2.Dispose();
                temp_map = Grayscale.CommonAlgorithms.BT709.Apply(temp_map);                          //转换成灰度
                temp_map = new BlobsFiltering(5, 5, temp_map.Width, temp_map.Height).Apply(temp_map); //去噪点


                //查找物体
                BlobCounter filter3 = new BlobCounter();
                filter3.ProcessImage(temp_map);
                Rectangle[] rects = filter3.GetObjectsRectangles();

                Rectangle findRec   = new Rectangle();
                bool      isfindRec = false;
                foreach (var item in rects)
                {
                    if (item.Top < 100)
                    {
                        continue;
                    }
                    if (item.Height > 20 || item.Width > 20)
                    {
                        continue;
                    }
                    //if (item.Height < 20) continue;
                    //if (item.Height < 31 || item.Height > 33) continue;
                    //if (item.Width < 16 || item.Width > 18) continue;
                    findRec   = item;
                    isfindRec = true;
                    break;
                }

                Bitmap bt1 = temp_map2.Clone(new Rectangle(new System.Drawing.Point(0, 0), temp_map.Size), PixelFormat.Format24bppRgb);

                if (isfindRec)//找到了
                {
                    //Text = findRec.Width.ToString() + " " + findRec.Height.ToString();
                    //因为只可能出现在上面  所以剪切掉下面部分不再用
                    if (temp_map2.Width > 200 && temp_map2.Height > 300)
                    {
                        try
                        {
                            Bitmap transBT = temp_map2.Clone(new Rectangle(50, 120, temp_map2.Width - 100, temp_map2.Height - 50 - findRec.Top + findRec.Height), temp_map2.PixelFormat);
                            System.Threading.Thread handle2 = new System.Threading.Thread(pictureHandle2);
                            handle2.IsBackground = true;
                            handle2.Start(transBT);
                        }
                        catch (Exception)
                        {
                        }
                    }

                    //Edges filter2 = new Edges();
                    //bt2 = filter2.Apply(bt2);
                    //用来画框
                    BitmapData tm = bt1.LockBits(new Rectangle(0, 0, bt1.Width, bt1.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                    Drawing.Rectangle(tm, findRec, Color.Red);//画框
                    bt1.UnlockBits(tm);

                    try
                    {
                        main_x = findRec.X - 50 + findRec.Width / 2;
                        main_y = findRec.Y - 120 + findRec.Height;
                        main_w = findRec.Width;
                        main_h = findRec.Height;

                        label4.Text = "X:" + findRec.X.ToString() + ",Y:" + findRec.Y.ToString();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }



                //ExhaustiveTemplateMatching templateMatching = new ExhaustiveTemplateMatching(0.7f);//图像查找
                //TemplateMatch[] matchings = templateMatching.ProcessImage(temp_map, find_main);
                ////转换成彩色图片
                //if (matchings.Length > 0)//找到了
                //{



                //}
                try
                {
                    PICA.Image = bt1;
                    temp_map.Dispose();
                }
                catch (Exception)
                {
                }

                //bt1.Dispose();
            }
        }