示例#1
0
        private void button8_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                VideoProcessig videoProcessig = new VideoProcessig(openFileDialog1.FileName, (map) =>
                {
                    Vector3[,] arr = map.GetRGB();
                    Morf m         = Morf.GenerateKMean(arr, 5);
                    m.RemoveEmptyRegions();
                    for (int i = 0; i < m.regions.Count; i++)
                    {
                        if (m.regions[i].Size < 20000)
                        {
                            m.regions[i].Fill(new Vector3(1, 1, 1), arr);
                        }
                        else
                        {
                            m.regions[i].Fill(m.regions[i].GetAverage(arr), arr);
                        }
                    }

                    return(arr);
                });
                videoProcessig.Show();
            }
        }
示例#2
0
        private void button6_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Vector3[,] val = Procedurs.Medium(openFileDialog1.FileName);
                Morf morf = Morf.GenerateKMean(val, 5);
                morf.RemoveEmptyRegions();
                List <Vector3> avrs = new List <Vector3>();
                for (int i = 0; i < morf.regions.Count; i++)
                {
                    avrs.Add(morf.regions[i].GetAverage(val));
                }
                float[,] temp  = new float[val.GetLength(0), val.GetLength(1)];
                float[,] temp2 = new float[val.GetLength(0), val.GetLength(1)];

                VideoProcessig videoProcessig = new VideoProcessig(openFileDialog1.FileName, (map) =>
                {
                    val.WriteRGB(map);
                    Procedurs.MorfSubtract(morf, avrs, val, temp);
                    temp2.ForEach(() => 0);
                    Procedurs.BlockSum(temp, temp2, 5);
                    temp2.RegMaximum();
                    return(temp2);
                });
                videoProcessig.Show();
            }
        }
示例#3
0
        public static void MorfSubtract(Morf morf, List <Vector3> avrs, Vector3[,] img, float[,] result)
        {
            int width  = img.GetLength(0);
            int height = img.GetLength(1);

            for (int i = 0; i < avrs.Count; i++)
            {
                Vector3    av     = avrs[i];
                MorfRegion region = morf.regions[i];
                region.ForEach(img, result, (v) => (v - av).Magnitude());
            }
        }
示例#4
0
 private void button3_Click(object sender, EventArgs e)
 {
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
         Vector3[,] val = Procedurs.Medium(openFileDialog1.FileName);
         Morf morf = Morf.GenerateKMean(val, 5);
         morf.RemoveEmptyRegions();
         for (int i = 0; i < morf.regions.Count; i++)
         {
             morf.regions[i].Fill(morf.regions[i].GetAverage(val), val);
         }
         var proc = new ImageProcessing(val.GetImage());
     }
 }
示例#5
0
        public static Morf GenerateKMean(float[,] map, uint count)
        {
            int width  = map.GetLength(0);
            int height = map.GetLength(1);

            MorfRegion[] regions = new MorfRegion[count + 1];
            for (int x = 0; x < count + 1; x++)
            {
                regions[x] = new MorfRegion();
            }

            float minX = float.MaxValue;

            float maxX = float.MinValue;

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    minX = Math.Min(minX, map[x, y]);

                    maxX = Math.Max(maxX, map[x, y]);
                }
            }

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int xpos = (int)((map[x, y] - minX) / (maxX - minX) * count);

                    regions[xpos].AddPoint(x, y);
                }
            }

            Morf morf = new Morf();

            for (int x = 0; x < count + 1; x++)
            {
                morf.regions.Add(regions[x]);
            }

            return(morf);
        }
示例#6
0
        private void button12_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Image <Gray, Byte>  last  = null;
                Image <Gray, float> flowX = null;
                Image <Gray, float> flowY = null;
                Image <Gray, float> draw  = null;

                Vector3[,] val = Procedurs.Medium(openFileDialog1.FileName);
                Morf morf = Morf.GenerateKMean(val, 5);
                morf.RemoveEmptyRegions();
                List <Vector3> avrs = new List <Vector3>();
                for (int i = 0; i < morf.regions.Count; i++)
                {
                    avrs.Add(morf.regions[i].GetAverage(val));
                }
                float[,] temp = new float[val.GetLength(0), val.GetLength(1)];

                VideoProcessig videoProcessig = new VideoProcessig(openFileDialog1.FileName, (map) =>
                {
                    val.WriteRGB(map);
                    Procedurs.MorfSubtract(morf, avrs, val, temp);
                    Image <Gray, byte> image = new Image <Gray, byte>(map.Size);
                    temp.RegMaximum();
                    for (int x = 0; x < map.Width; x++)
                    {
                        for (int y = 0; y < map.Height; y++)
                        {
                            Gray g      = image[y, x];
                            g.Intensity = temp[x, y] * 255;
                            image[y, x] = g;
                        }
                    }

                    if (last == null)
                    {
                        last  = image;
                        flowX = new Image <Gray, float>(map.Size);
                        flowY = new Image <Gray, float>(map.Size);
                        draw  = new Image <Gray, float>(map.Size);
                    }
                    else
                    {
                        CvInvoke.CalcOpticalFlowFarneback(last, image, flowX, flowY, 0.5, 3, 10, 3, 5, 1.5, Emgu.CV.CvEnum.OpticalflowFarnebackFlag.Default);
                        last = image;
                    }

                    double max = 0;
                    for (int y = 0; y < map.Height; y++)
                    {
                        for (int x = 0; x < map.Width; x++)
                        {
                            Gray g       = draw[y, x];
                            double value = Math.Abs(flowX[y, x].Intensity) + Math.Abs(flowY[y, x].Intensity);
                            max          = Math.Max(value, max);
                            g.Intensity  = value;
                            draw[y, x]   = g;
                        }
                    }

                    for (int y = 0; y < map.Height; y++)
                    {
                        for (int x = 0; x < map.Width; x++)
                        {
                            Gray g       = draw[y, x];
                            g.Intensity /= max;
                            g.Intensity *= 255;
                            draw[y, x]   = g;
                        }
                    }



                    var result = draw.Bitmap;
                    return(result);
                });


                videoProcessig.Show();
            }
        }
示例#7
0
        private void button7_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Image <Rgb, float> last = null;
                //Image<Gray, float> flowX = null;
                //Image<Gray, float> flowY = null;

                Vector3[,] val = Procedurs.Medium(openFileDialog1.FileName);
                Morf morf = Morf.GenerateKMean(val, 5);
                morf.RemoveEmptyRegions();
                List <Vector3> avrs = new List <Vector3>();
                for (int i = 0; i < morf.regions.Count; i++)
                {
                    avrs.Add(morf.regions[i].GetAverage(val));
                }
                for (int i = 0; i < morf.regions.Count; i++)
                {
                    morf.regions[i].Fill(avrs[i], val);
                }

                Image <Rgb, float> avr_map = new Image <Rgb, float>(val.GetImage());
                ImageProcessing    proc    = new ImageProcessing(avr_map.Bitmap);
                proc.Show();

                float[,] temp   = new float[val.GetLength(0), val.GetLength(1)];
                float[,] temp2  = new float[val.GetLength(0), val.GetLength(1)];
                byte[,] b_temp  = new byte[val.GetLength(0), val.GetLength(1)];
                byte[,] b_temp2 = new byte[val.GetLength(0), val.GetLength(1)];

                LinesTracer      tracer = new LinesTracer();
                List <Rectangle> cars   = new List <Rectangle>();

                VideoProcessig videoProcessig = new VideoProcessig(openFileDialog1.FileName, (map) =>
                {
                    last = new Image <Rgb, float>(map);
                    CvInvoke.AbsDiff(last, avr_map, last);
                    var grey = last.Convert <Gray, float>();

                    grey.
                    CvInvoke.Multiply(grey, new ScalarArray(1), grey);
                    //var binar = grey.ThresholdBinary(new Gray(0.2), new Gray(1));

                    return(grey.Bitmap);

                    //val.WriteRGB(map);
                    //Procedurs.MorfSubtract(morf, avrs, val, temp);
                    //b_temp.ForEach(temp, (b, v) => (byte)(v > 0.2f ? 1 : 0));
                    //b_temp.BinaryThiken(b_temp2);
                    //b_temp2.BinaryThiken(b_temp);
                    //b_temp.BinaryThiken(b_temp2);
                    //b_temp2.BinaryThiken(b_temp);

                    //Image<Gray, Byte> image = new Image<Gray, byte>(map);


                    ////CvInvoke.CalcOpticalFlowFarneback(last,image, flowX, flowY, 0.5, 3, 10, 3, 5, 1.5, Emgu.CV.CvEnum.OpticalflowFarnebackFlag.Default);
                    ////CvInvoke.AccumulateSquare(flowX, flowY);
                    ////CvInvoke.Sobel(flowY, flowX, Emgu.CV.CvEnum.DepthType.Cv32F, 1, 0);

                    //Morf mm = Morf.GenerateBinar(b_temp);

                    //cars.Clear();

                    //using (Graphics gr = Graphics.FromImage(map))
                    //{
                    //    for (int i = 0; i < mm.regions.Count; i++)
                    //    {
                    //        var rect = mm.regions[i].GetSize();
                    //        if (rect.Width * rect.Height > 1000)
                    //        {
                    //            gr.DrawRectangle(Pens.Green, rect);
                    //            cars.Add(rect);
                    //        }
                    //    }
                    //}

                    //tracer.TrackAndWrite(cars, ref map, 0.2f * map.Width);

                    //return map;
                });
                videoProcessig.Show();
            }
        }
示例#8
0
        public static Morf GenerateKMean(Vector3[,] map, uint count)
        {
            int width  = map.GetLength(0);
            int height = map.GetLength(1);

            MorfRegion[,,] regions = new MorfRegion[count + 1, count + 1, count + 1];
            for (int x = 0; x < count + 1; x++)
            {
                for (int y = 0; y < count + 1; y++)
                {
                    for (int z = 0; z < count + 1; z++)
                    {
                        regions[x, y, z] = new MorfRegion();
                    }
                }
            }

            float minX = float.MaxValue;
            float minY = float.MaxValue;
            float minZ = float.MaxValue;

            float maxX = float.MinValue;
            float maxY = float.MinValue;
            float maxZ = float.MinValue;

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    minX = Math.Min(minX, map[x, y].x);
                    minY = Math.Min(minY, map[x, y].y);
                    minZ = Math.Min(minZ, map[x, y].z);

                    maxX = Math.Max(maxX, map[x, y].x);
                    maxY = Math.Max(maxY, map[x, y].y);
                    maxZ = Math.Max(maxZ, map[x, y].z);
                }
            }

            float kX = count / (maxX - minX);
            float kY = count / (maxY - minY);
            float kZ = count / (maxZ - minZ);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Vector3 v    = map[x, y];
                    int     xpos = (int)((v.x - minX) * kX);
                    int     ypos = (int)((v.y - minY) * kY);
                    int     zpos = (int)((v.z - minZ) * kZ);

                    regions[xpos, ypos, zpos].AddPoint(x, y);
                }
            }

            Morf morf = new Morf();

            for (int x = 0; x < count + 1; x++)
            {
                for (int y = 0; y < count + 1; y++)
                {
                    for (int z = 0; z < count + 1; z++)
                    {
                        morf.regions.Add(regions[x, y, z]);
                    }
                }
            }

            return(morf);
        }
示例#9
0
        public static Morf GenerateBinar(byte[,] map)
        {
            List <MorfRegion> regions = new List <MorfRegion>();
            int width  = map.GetLength(0);
            int height = map.GetLength(1);

            int[,] reg_map = new int[width, height];
            if (map[0, 0] == 1)
            {
                MorfRegion morfRegion = new MorfRegion();
                morfRegion.AddPoint(0, 0);
                reg_map[0, 0] = 1;
                regions.Add(morfRegion);
            }
            for (int y = 1; y < height; y++)
            {
                if (map[0, y] == 1)
                {
                    if (reg_map[0, y - 1] != 0)
                    {
                        reg_map[0, y] = reg_map[0, y - 1];
                        regions[reg_map[0, y] - 1].AddPoint(0, y);
                    }
                    else
                    {
                        MorfRegion morfRegion = new MorfRegion();
                        morfRegion.AddPoint(0, y);
                        regions.Add(morfRegion);
                        reg_map[0, y] = regions.Count;
                    }
                }
            }

            for (int x = 1; x < width; x++)
            {
                if (map[x, 0] == 1)
                {
                    if (reg_map[x - 1, 0] != 0)
                    {
                        reg_map[x, 0] = reg_map[x - 1, 0];
                        regions[reg_map[x, 0] - 1].AddPoint(x, 0);
                    }
                    else
                    {
                        MorfRegion morfRegion = new MorfRegion();
                        morfRegion.AddPoint(x, 0);
                        regions.Add(morfRegion);
                        reg_map[x, 0] = regions.Count;
                    }
                }

                for (int y = 1; y < height; y++)
                {
                    if (map[x, y] == 1)
                    {
                        if (reg_map[x, y - 1] != 0)
                        {
                            if (reg_map[x - 1, y] != 0)
                            {
                                if (reg_map[x - 1, y] != reg_map[x, y - 1])
                                {
                                    #region Объединение множеств
                                    reg_map[x, y] = reg_map[x, y - 1];
                                    int old = reg_map[x - 1, y] - 1;
                                    regions[reg_map[x, y] - 1].AddPoint(x, y);
                                    regions[old].Fill <int>(reg_map[x, y], reg_map);
                                    regions[reg_map[x, y] - 1].Union(regions[old]);
                                    regions[old].Clear();
                                    #endregion
                                }
                                else
                                {
                                    reg_map[x, y] = reg_map[x, y - 1];
                                    regions[reg_map[x, y] - 1].AddPoint(x, y);
                                }
                            }
                            else
                            {
                                reg_map[x, y] = reg_map[x, y - 1];
                                regions[reg_map[x, y] - 1].AddPoint(x, y);
                            }
                        }
                        else if (reg_map[x - 1, y] != 0)
                        {
                            reg_map[x, y] = reg_map[x - 1, y];
                            regions[reg_map[x, y] - 1].AddPoint(x, y);
                        }
                        else
                        {
                            MorfRegion morfRegion = new MorfRegion();
                            morfRegion.AddPoint(x, y);
                            regions.Add(morfRegion);
                            reg_map[x, y] = regions.Count;
                        }
                    }
                }
            }

            Morf morf = new Morf();
            morf.regions = regions;
            morf.RemoveEmptyRegions();
            return(morf);
        }