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

转载于:https://www.cnblogs.com/RegressionWorldLine/p/4871517.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值