AForge学习笔记(5):AForge.Imaging(上)

来源:互联网 发布:生死狙击刷金币软件 编辑:程序博客网 时间:2024/05/17 22:34

作者:GAO-XIANG

转自:http://blog.csdn.net/xiang__jiangsu/article/details/8131141


本次我们一起来学习AForge中影像处理算法以及使用方法。

ComplexImage:对复杂影像的处理主要包括反向快速傅立叶变换以及正向快速傅立叶变换,使用很简单,如下面示例:

           //读入原始影像
            Bitmap bt=new Bitmap(@"C:\Users\GAOXIANG\Desktop\FLY.jpg");
            Size size = new System.Drawing.Size();
            //将长宽处理为2的幂
            double n1, n2;
            n1 = Math.Log(bt.Width, 2);
            n2 = Math.Log(bt.Height, 2);
            size.Width = (Int32)Math.Pow(2, Convert.ToInt32(n1));
            size.Height = (Int32)Math.Pow(2, Convert.ToInt32(n2));
            Bitmap bt0 = new Bitmap(bt, size.Width, size.Height);
            Rectangle rec = new Rectangle(pictureBox1.Location, bt0.Size);
            //将彩色图转为灰度图
            Bitmap bt1 = bt0.Clone(rec, PixelFormat.Format8bppIndexed);
            pictureBox1.Image = bt1;
            //复杂图像的原图像需要满足两个条件:灰度图(像素深度:8bbp),长宽必须是2的幂
            //傅立叶正变换
            ComplexImage img1 = ComplexImage.FromBitmap(bt1);
            img1.ForwardFourierTransform();
            Bitmap bt2 = img1.ToBitmap();
            pictureBox2.Image = img1.ToBitmap();
            //傅立叶逆变换
            ComplexImage img2 = ComplexImage.FromBitmap(bt1);
            img2.BackwardFourierTransform();
            pictureBox3.Image = img2.ToBitmap();

结果:


ExhaustiveBlockMatching:穷举块匹配算法,通过某种算法获取初始图像的相关点,利用相关点与匹配图像匹配,若匹配点满足阈值条件,则作为匹配点保留。AForge中的程序实现如下:

            // 通过Susan算法确定原始图像相关点,第一参数:像素阈值,第二参数:几何阈值
            SusanCornersDetector scd = new SusanCornersDetector(30, 18);
            List<IntPoint> points = scd.ProcessImage(sourceImage);//获取相关点
            // 创建块匹配算法实例,第一参数:块大小,第二参数:搜索半径
            ExhaustiveBlockMatching bm = new ExhaustiveBlockMatching(8, 12);
            // 搜索匹配点
            List<BlockMatch> matches = bm.ProcessImage(sourceImage, points, searchImage);
            // 保存图像属性,用于之后的图像对象绘制(点、线绘制)
            BitmapData data = sourceImage.LockBits(
                new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                ImageLockMode.ReadWrite, sourceImage.PixelFormat);
            foreach (BlockMatch match in matches)
            {
                //显示图像中匹配原始点
                Drawing.FillRectangle(data,
                    new Rectangle(match.SourcePoint.X - 1, match.SourcePoint.Y - 1, 3, 3),
                    Color.Yellow);
                Drawing.Line(data, match.SourcePoint, match.MatchPoint, Color.Red);
                // 判断相似度
                if (match.Similarity > 0.98f)
                {
                    // 对高相似的目标进行处理
                }
            }
            //解锁
            sourceImage.UnlockBits(data);

运行结果:


ExhaustiveTemplateMatching:基于模版的图像匹配,所谓模版即是值将在原始图像中搜索的区域模块,在AForge中的程序实现如下:

           bt1 = new Bitmap(@"C:\Users\GAOXIANG\Desktop\111.jpg");
            bt2 = new Bitmap(@"C:\Users\GAOXIANG\Desktop\222.jpg");
            bt3 = new Bitmap(@"C:\Users\GAOXIANG\Desktop\333.jpg");
            //创建模版匹配实例,0.98f确定匹配相似性阈值
            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.98f);
            //基于一定的相似性阈值获得匹配块
            TemplateMatch[] matchings = tm.ProcessImage(bt1, bt2);
            BitmapData data = bt1.LockBits(
                new Rectangle(0, 0, bt1.Width, bt1.Height),
                ImageLockMode.ReadWrite, bt1.PixelFormat);
            foreach (TemplateMatch m in matchings)
            {
                Drawing.Rectangle(data, m.Rectangle, Color.Red);
            }
            bt1.UnlockBits(data);
            // 对第二模版进行匹配,过程相同
            ExhaustiveTemplateMatching tm1 = new ExhaustiveTemplateMatching(0.94f);
            TemplateMatch[] matchings1 = tm1.ProcessImage(bt1, bt3);
            BitmapData data1 = bt1.LockBits(
                new Rectangle(0, 0, bt1.Width, bt1.Height),
                ImageLockMode.ReadWrite, bt1.PixelFormat);
            foreach (TemplateMatch m in matchings1)
            {
                Drawing.Rectangle(data1, m.Rectangle, Color.Red);
            }
            bt1.UnlockBits(data1);

运行结果:


HorizontalIntensityStatistics:用于图像水平分量的灰度值统计,使用如下:

            bt1 = new Bitmap(@"C:\Users\GAOXIANG\Desktop\1.jpg");
            pictureBox1.Image = bt1;
            //获取图像并转为8bpp深度
            Rectangle rec = new Rectangle(new Point(0,0),bt1.Size);
            bt2 = bt1.Clone(rec,PixelFormat.Format8bppIndexed);
            //获取图像水平分量
            AForge.Imaging.HorizontalIntensityStatistics his=new AForge.Imaging.HorizontalIntensityStatistics(bt2);
            AForge.Math.Histogram histogram = his.Gray;
            histogram1.Values = histogram.Values;

运行结果:


HoughCircleTransformation:通过霍夫变换对圆进行检测,示例程序如下:

            Bitmap bt = new Bitmap(@"C:\Users\GAOXIANG\Desktop\1.bmp");
            //由于只能处理变换8位深度的图像,因此进行图像格式转换
            Bitmap bt1 = bt.Clone(new Rectangle(new Point(0,0),bt.Size),System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            AForge.Imaging.HoughCircleTransformation circleTransform = new AForge.Imaging.HoughCircleTransformation(35);
            // 进行基于霍夫算法的圆探测
            circleTransform.ProcessImage(bt1);
            Bitmap houghCirlceImage = circleTransform.ToBitmap();
            AForge.Imaging.HoughLineTransformation lineTransform = new AForge.Imaging.HoughLineTransformation();
            // 进行霍夫线探测
            lineTransform.ProcessImage(bt1);
            Bitmap houghlineImage = lineTransform.ToBitmap();
            pictureBox1.Image = bt;
            pictureBox2.Image = houghCirlceImage;
            pictureBox3.Image = houghlineImage;

运行结果:


1 0
原创粉丝点击