• Emgu下SIFT算法的使用


    opencv自带sift算法的函数,在Emgu下可以这样使用:

    using Emgu.CV;
    using Emgu.CV.Structure;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace CVsift
    {
    	public partial class Form1 : Form
    	{
    		public Form1()
    		{
    			InitializeComponent();
    		}
    		string fName;
    		string inifname = Application.StartupPath;
    		private void button1_Click(object sender, EventArgs e)
    		{
    			OpenFileDialog openFileDialog = new OpenFileDialog();
    			openFileDialog.Filter = "bmp|*.bmp|jpg|*.jpg";
    			openFileDialog.RestoreDirectory = true;
    			openFileDialog.FilterIndex = 1;
    			openFileDialog.InitialDirectory = inifname;
    			if (openFileDialog.ShowDialog() == DialogResult.OK)
    			{
    				fName = openFileDialog.FileName;
    				inifname = fName.Replace(openFileDialog.SafeFileName,"");
                    Image<Gray, Byte> originPic = new Image<Gray, byte>(fName);
                    Image<Bgr, Byte> originBgrPic = new Image<Bgr, byte>(fName);
                    Bitmap bitmapGrayPic = originPic.ToBitmap();
                    Bitmap bitmapPicChanged = new Bitmap(bitmapGrayPic.Width, bitmapGrayPic.Height);
                    for (int i = 0; i < bitmapPicChanged.Height; i++)
                    {
                        for (int j = 0; j < bitmapPicChanged.Width; j++)
                        {
                            bitmapPicChanged.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                        }
                    }
                    //局部平均
                    Emgu.CV.Features2D.SIFTDetector sift = new Emgu.CV.Features2D.SIFTDetector
                        (0//特征点数目
                         , 5//octave层数
                         , 0.05//约束阈值
                         , 10//边界阈值
                         , 1.6//sigma
                        );
                    Emgu.CV.Features2D.ImageFeature<float>[] siftPoint = sift.DetectFeatures(originPic, null);
                    Emgu.CV.Util.VectorOfKeyPoint vectorOfPoint = new Emgu.CV.Util.VectorOfKeyPoint();
                    vectorOfPoint.Clear();
                    MKeyPoint[] mKeyPoint = new MKeyPoint[siftPoint.GetLength(0)];
                    for (int i = 0; i < siftPoint.GetLength(0); i++)
                    {
                        mKeyPoint[i].Point = siftPoint[i].KeyPoint.Point;
                    }
                    vectorOfPoint.Push(mKeyPoint);
                    for (int i = 0; i < siftPoint.GetLength(0); i++)
                    {
                        bitmapPicChanged.SetPixel(Convert.ToInt32(siftPoint[i].KeyPoint.Point.X), Convert.ToInt32(siftPoint[i].KeyPoint.Point.Y), Color.FromArgb(0, 0, 0));
                    }
                    string siftname = fName;
                    siftname = fName.Remove(fName.Length - 4);
                    siftname = siftname + "_basicSIFT.bmp";
                    bitmapPicChanged.Save(siftname);
                    Bgr c = new Bgr(Color.FromArgb(255, 0, 0));
                    Image<Bgr, Byte> siftPic = Emgu.CV.Features2D.Features2DToolbox.DrawKeypoints(originBgrPic, vectorOfPoint, c, Emgu.CV.Features2D.Features2DToolbox.KeypointDrawType.DEFAULT);
                    siftname = fName;
                    siftname = fName.Remove(fName.Length - 4);
                    siftname = siftname + "_SIFT.bmp";
                    siftPic.Save(siftname);
                    siftname = fName;
                    siftname = fName.Remove(fName.Length - 4);
                    siftname = siftname + "_SIFT.txt";
                    FileStream fs = new FileStream(siftname, FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs);
                    for (int i = 0; i < siftPoint.GetLength(0); i++)
                    {
                        sw.Write(Convert.ToInt32(siftPoint[i].KeyPoint.Point.X));
                        sw.Write(' ');
                        sw.Write(Convert.ToInt32(siftPoint[i].KeyPoint.Point.Y));
                        sw.WriteLine();
                    }
                    sw.Close();
                    fs.Close();
                    pictureBox1.Image = siftPic.ToBitmap();
    			}			
    		}
    	}
    }
    
    
  • 相关阅读:
    【转】HashMap、TreeMap、Hashtable、HashSet和ConcurrentHashMap区别
    【转】ArrayList循环遍历并删除元素的常见陷阱
    【转】Java内存管理:深入Java内存区域
    【转】java-String中的 intern()
    Jenkins + Ant + Git + Tomcat自动化部署
    Java的四种内部类
    java中的匿名内部类总结
    【转】如何提高意志力&如何坚持每天学习
    【转】前端工程筹建NodeJs+gulp+bower
    转 旧衣服不要扔,竟然还能这样改造,美翻了!
  • 原文地址:https://www.cnblogs.com/RegressionWorldLine/p/4871517.html
Copyright © 2020-2023  润新知