Vous êtes sur la page 1sur 10

Design tampilan sbagai berikut :

txtImageScene btnImageScene

txtImageToFind btnImageToFind
ckDrawMatchingLines

btnPerformSURFOrGetImageToTrack
ckDrawKeyPoint
s

ibResult
Source Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.CV.Util;
using System.Diagnostics;

namespace SURF_EMGUCV
{
public partial class Form1 : Form
{

Image<Bgr, Byte> imgSceneColor = null;


Image<Bgr, Byte> imgToFindColor = null;

Image<Bgr, Byte> imgCopyOfImageToFindWidthBorder = null;

bool blnImageSceneLoaded = false;


bool blnImageToFindLoaded = false;

static Image<Bgr, Byte> imgResult = null;

Bgr bgrKeyPointColor = new Bgr(Color.Blue);


Bgr bgrMatchingLinesColor = new Bgr(Color.Green);
Bgr bgrFoundImageColor = new Bgr(Color.Red);

Stopwatch stopwatch = new Stopwatch();


public Form1()
{
InitializeComponent();
}

private void btnImageScene_Click(object sender, EventArgs e)


{
DialogResult dialogResult = ofdImageScene.ShowDialog();

if ((dialogResult == DialogResult.OK) || (dialogResult == DialogResult.Yes))


txtImageScene.Text = ofdImageScene.FileName;
else
return;

try
{
imgSceneColor = new Image<Bgr, Byte>(txtImageScene.Text);
//'Image(Of Bgr, Byte) My_Image = New Image(Of Bgr, Byte)(txtImageScene.Text)
//'imgSceneColor = imgSceneColor.ToBitmap();
}
catch (Exception ex)
{
this.Text = ex.Message;
return;
}

blnImageSceneLoaded = true;
if (blnImageToFindLoaded == false)
ibResult.Image = imgSceneColor;
else
ibResult.Image = imgSceneColor.ConcateHorizontal(imgCopyOfImageToFindWidthBorder);
}

private void btnImageToFind_Click(object sender, EventArgs e)


{
DialogResult dialogResult = ofdImageToFind.ShowDialog();
if ((dialogResult == DialogResult.OK) || (dialogResult == DialogResult.Yes))
txtImageToFind.Text = ofdImageToFind.FileName;
else
return;

try
{
imgToFindColor = new Image<Bgr, Byte>(txtImageToFind.Text);
//'Image(Of Bgr, Byte) My_Image = New Image(Of Bgr, Byte)(txtImageScene.Text)
//'imgSceneColor = imgSceneColor.ToBitmap();
}
catch (Exception ex)
{
this.Text = ex.Message;
return;
}

blnImageToFindLoaded = true;

imgCopyOfImageToFindWidthBorder = imgToFindColor.Copy();
imgCopyOfImageToFindWidthBorder.Draw(new Rectangle(1, 1, imgCopyOfImageToFindWidthBorder.Width
- 3, imgCopyOfImageToFindWidthBorder.Height - 3), bgrFoundImageColor, 2);

if (blnImageSceneLoaded == true)
ibResult.Image = imgSceneColor.ConcateHorizontal(imgCopyOfImageToFindWidthBorder);
else
ibResult.Image = imgCopyOfImageToFindWidthBorder;

private void btnPerformSURFOrGetImageToTrack_Click(object sender, EventArgs e)


{

if ((txtImageToFind.Text != string.Empty) && (txtImageScene.Text != String.Empty))


PerformSURFDetectionAndUpdateGUI(new Object(), new EventArgs());
else
this.Text = "choose image files first, then choose Perform SURF Detection button";

public void PerformSURFDetectionAndUpdateGUI(object sender, EventArgs arg)


{
if ((blnImageSceneLoaded == false | blnImageToFindLoaded == false | imgSceneColor == null |
imgToFindColor == null))
{
this.Text = "either or both image are not loaded or null, please choose both images before
performing SURF";
return;
}

this.Text = "processing, please wait . . .";


Application.DoEvents();
stopwatch.Restart();

//if we get here, we both color imagesare good, we can begin SURF detection stuff . . .
SURFDetector surfDetector = new SURFDetector(500, false);
Image<Gray, byte> imgSceneGray = null;
Image<Gray, byte> imgToFindGray = null;

VectorOfKeyPoint vkpSceneKeyPoints = default(VectorOfKeyPoint);


VectorOfKeyPoint vkpToFindKeyPoints = default(VectorOfKeyPoint);

Matrix<float> mtxSceneDescriptors = default(Matrix<float>);


Matrix<float> mtxToFindDescriptors = default(Matrix<float>);

Matrix<int> mtxMatchIndices = default(Matrix<int>);


Matrix<float> mtxDistance = default(Matrix<float>);
Matrix<byte> mtxMask = default(Matrix<byte>);

BruteForceMatcher<float> bruteForceMatcher = default(BruteForceMatcher<float>);


HomographyMatrix homographyMatrix = null;

int intKNumNearestNeighbors = 2;
double dblUniquenessThreshold = 0.8;

int intNumNonZeroElements = 0;

double dblScaleIncrement = 1.5;


int intRotationBins = 20;

double dblRansacReprojectionThreshold = 2.0;


Rectangle rectImageToFind = default(Rectangle);
PointF[] ptfPointsF = null;
Point[] ptPoints = null;

imgSceneGray = imgSceneColor.Convert<Gray, byte>();


imgToFindGray = imgToFindColor.Convert<Gray, byte>();

vkpSceneKeyPoints = surfDetector.DetectKeyPointsRaw(imgSceneGray, null);


mtxSceneDescriptors = surfDetector.ComputeDescriptorsRaw(imgSceneGray, null,
vkpSceneKeyPoints);

vkpToFindKeyPoints = surfDetector.DetectKeyPointsRaw(imgToFindGray, null);


mtxToFindDescriptors = surfDetector.ComputeDescriptorsRaw(imgToFindGray, null,
vkpToFindKeyPoints);

bruteForceMatcher = new BruteForceMatcher<float>(DistanceType.L2);


bruteForceMatcher.Add(mtxToFindDescriptors);

mtxMatchIndices = new Matrix<int>(mtxSceneDescriptors.Rows, intKNumNearestNeighbors);


mtxDistance = new Matrix<float>(mtxSceneDescriptors.Rows, intKNumNearestNeighbors);

bruteForceMatcher.KnnMatch(mtxSceneDescriptors, mtxMatchIndices, mtxDistance,


intKNumNearestNeighbors, null);

mtxMask = new Matrix<byte>(mtxDistance.Rows, 1);


mtxMask.SetValue(255);

Features2DToolbox.VoteForUniqueness(mtxDistance, dblUniquenessThreshold, mtxMask);

intNumNonZeroElements = CvInvoke.cvCountNonZero(mtxMask);
if ((intNumNonZeroElements >= 4))
{
intNumNonZeroElements = Features2DToolbox.VoteForSizeAndOrientation(vkpToFindKeyPoints,
vkpSceneKeyPoints, mtxMatchIndices, mtxMask, dblScaleIncrement, intRotationBins);
if ((intNumNonZeroElements >= 4))
{
homographyMatrix =
Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(vkpToFindKeyPoints, vkpSceneKeyPoints,
mtxMatchIndices, mtxMask, dblRansacReprojectionThreshold);
}
}
imgCopyOfImageToFindWidthBorder = imgToFindColor.Copy();
imgCopyOfImageToFindWidthBorder.Draw(new Rectangle(1, 1, imgCopyOfImageToFindWidthBorder.Width
- 3, imgCopyOfImageToFindWidthBorder.Height - 3), bgrFoundImageColor, 2);

if ((ckDrawKeyPoints.Checked == true & ckDrawMatchingLines.Checked == true))


{
imgResult = Features2DToolbox.DrawMatches(imgCopyOfImageToFindWidthBorder,
vkpToFindKeyPoints, imgSceneColor, vkpSceneKeyPoints, mtxMatchIndices, bgrMatchingLinesColor,
bgrKeyPointColor, mtxMask, Features2DToolbox.KeypointDrawType.DEFAULT);
}
else if ((ckDrawKeyPoints.Checked == true & ckDrawMatchingLines.Checked == false))
{
imgResult = Features2DToolbox.DrawKeypoints(imgSceneColor, vkpSceneKeyPoints,
bgrKeyPointColor, Features2DToolbox.KeypointDrawType.DEFAULT);
imgCopyOfImageToFindWidthBorder =
Features2DToolbox.DrawKeypoints(imgCopyOfImageToFindWidthBorder, vkpToFindKeyPoints, bgrKeyPointColor,
Features2DToolbox.KeypointDrawType.DEFAULT);
imgResult = imgResult.ConcateHorizontal(imgCopyOfImageToFindWidthBorder);
}
else if ((ckDrawKeyPoints.Checked == false & ckDrawMatchingLines.Checked == false))
{
imgResult = imgSceneColor;
imgResult = imgResult.ConcateHorizontal(imgCopyOfImageToFindWidthBorder);
}
else
{
//should be never here
}

if ((homographyMatrix != null))
{
rectImageToFind.X = 0;
rectImageToFind.Y = 0;
rectImageToFind.Width = imgToFindGray.Width;
rectImageToFind.Height = imgToFindGray.Height;

ptfPointsF = new PointF[] {


new PointF(rectImageToFind.Left, rectImageToFind.Top),
new PointF(rectImageToFind.Right, rectImageToFind.Top),
new PointF(rectImageToFind.Right, rectImageToFind.Bottom),
new PointF(rectImageToFind.Left, rectImageToFind.Bottom)
};

homographyMatrix.ProjectPoints(ptfPointsF);

ptPoints = new Point[] {


Point.Round(ptfPointsF[0]),
Point.Round(ptfPointsF[1]),
Point.Round(ptfPointsF[2]),
Point.Round(ptfPointsF[3])
};

imgResult.DrawPolyline(ptPoints, true, bgrFoundImageColor, 2);


}

ibResult.Image = imgResult;

stopwatch.Stop();
this.Text = "processing time = " + stopwatch.Elapsed.TotalSeconds.ToString() + "Sec, done
processing, choose another image if desired";

private void ckDrawKeyPoints_CheckedChanged(object sender, EventArgs e)


{
if ((ckDrawKeyPoints.Checked == false))
{
ckDrawMatchingLines.Checked = false;
ckDrawMatchingLines.Enabled = false;
}
else if ((ckDrawKeyPoints.Checked == true))
{
ckDrawMatchingLines.Enabled = true;
}

btnPerformSURFOrGetImageToTrack_Click(new object(), new EventArgs());

}
private void ckDrawMatchingLines_CheckedChanged(object sender, EventArgs e)
{
btnPerformSURFOrGetImageToTrack_Click(new object(), new EventArgs());

}
}
}

Vous aimerez peut-être aussi