天天看點

AForge學習筆記(5):AForge.Imaging(上)

作者: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();

結果:

AForge學習筆記(5):AForge.Imaging(上)

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);

運作結果:

AForge學習筆記(5):AForge.Imaging(上)

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);

運作結果:

AForge學習筆記(5):AForge.Imaging(上)

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;

運作結果:

AForge學習筆記(5):AForge.Imaging(上)

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;

運作結果:

AForge學習筆記(5):AForge.Imaging(上)