In this post I am going to show "how to use ExhaustiveTemplateMatching feature is Accord.Net " to detect object i am image.So Lets create a new project and add reference "Accord.Imaging" to your project.
My Form Design:
In the Select Image Textbox i am going to select the input image and in template image textbox i am going to select the Template image.
Code Behind for Selecting Input Image and Template Image:
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Filter = "Jpeg|*.jpg";
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
PictureInput.Load(openFileDialog1.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show("Exception:" + ex.ToString());
}
}
//Select Template Image
private void btnTemplate_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Filter = "Jpeg|*.jpg";
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
//PictureInput.Load(openFileDialog1.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show("Exception:" + ex.ToString());
}
}
Template Matching Code:
private void btnMatch_Click(object sender, EventArgs e)
{
//Converting Template into GrayScale Image
Bitmap templateImage = new Bitmap(textBox2.Text);
Grayscale gg = new GrayscaleBT709();
Bitmap grayTemplate = gg.Apply(templateImage);
// create template matching algorithm's instance
ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0);
// find all matchings with specified above similarity
Bitmap sourceImage = new Bitmap(textBox1.Text);
Bitmap graySource = gg.Apply(sourceImage);
TemplateMatch[] matchings = tm.ProcessImage(graySource, grayTemplate);
Graphics g = Graphics.FromImage(sourceImage);
if (matchings[0].Similarity > 0.8f)
{
int X = matchings[0].Rectangle.X;
int Y = matchings[0].Rectangle.Y;
g.DrawRectangle(new Pen(Color.Red, 3), X, Y, matchings[0].Rectangle.Width, matchings[0].Rectangle.Height);
PicTemplate.Image = sourceImage;
MessageBox.Show("Match found...");
}
else
{
MessageBox.Show("Match Not Found...");
}
}
Output:
Download Source Code
Hi,
ReplyDeleteis the source code available?
Cheers
Extremely slow 3 minutes for full HD picture and 60x60 template.
ReplyDeleteif any runnable source code available pls send it me dude.
Deleteuse this :balananosoft@gmail.com
Grayscale gg = new GrayscaleBT709();//error
ReplyDeleteExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0);//error
TemplateMatch[] matchings = tm.ProcessImage(graySource, grayTemplate);//error
is there any declaration need?
reply me.
hi. where does picTemplate came from? tnx
ReplyDelete