c# - 如何在另一张图片中找到模板图片? Kinect 和 AForge 首选

标签 c# bitmap pattern-matching kinect aforge

我从这里复制了 AForge-Sample: http://www.aforgenet.com/framework/features/template_matching.html 并希望它可以使用 2 个位图作为源,如下面的代码所示:

    Bitmap findTemplate (Bitmap sourceImage, Bitmap template)
    {

    // create template matching algorithm's instance
    // (set similarity threshold to x.y%, 1.0f = 100%)
    ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching( 0.4f );
    // find all matchings with specified above similarity
    TemplateMatch[] matchings = tm.ProcessImage( sourceImage, template ); **// "Unsupported pixel format of the source or template image." as error message**
    // highlight found matchings
    BitmapData data = sourceImage.LockBits(
        new Rectangle( 0, 0, sourceImage.Width, sourceImage.Height ),
        ImageLockMode.ReadWrite, sourceImage.PixelFormat );
    foreach ( TemplateMatch m in matchings )
    {
        AForge.Imaging.Drawing.Rectangle( data, m.Rectangle, System.Drawing.Color.White );
        // do something else with matching
    }
    sourceImage.UnlockBits( data );
    return sourceImage;
    }

但是在调用 TemplateMatch[] matchings = tm.P.... 时出现上述错误。 模板是这样生成的:

Bitmap templatebitmap=(Bitmap)AForge.Imaging.Image.FromFile("template.jpg");

来源是用kinect-webcam生成的,其中PlanarImage被格式化为Bitmap(从某处复制的方法,但它一直有效)

 Bitmap PImageToBitmap(PlanarImage PImage)
        {
            Bitmap bmap = new Bitmap(
              PImage.Width,
              PImage.Height,
              System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            BitmapData bmapdata = bmap.LockBits(
              new Rectangle(0, 0, PImage.Width,
                                   PImage.Height),
              ImageLockMode.WriteOnly,
              bmap.PixelFormat);
            IntPtr ptr = bmapdata.Scan0;
            Marshal.Copy(PImage.Bits,
                         0,
                         ptr,
                         PImage.Width *
                            PImage.BytesPerPixel *
                                 PImage.Height);
            bmap.UnlockBits(bmapdata);
            return bmap;
        }

那么,有人可以帮助我,我的错误可能在哪里吗? 或者也许有人知道将模板与 Kinect 相匹配的更好方法? 总体工作是用 kinect 检测已知物体,在我的例子中是橡皮鸭。

提前谢谢你。

最佳答案

这是使用 AForge 的解决方案 但它很慢,大约需要 5 秒,但它有效 和往常一样,你需要引入 AForge 框架下载并安装它。 指定使用 AForge 命名空间

然后复制粘贴使其工作

System.Drawing.Bitmap sourceImage = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\1.jpg");
System.Drawing.Bitmap template = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\2.jpg");
// create template matching algorithm's instance
// (set similarity threshold to 92.5%)

ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.921f);
// find all matchings with specified above similarity

TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template);
// highlight found matchings

BitmapData data = sourceImage.LockBits(
    new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
    ImageLockMode.ReadWrite, sourceImage.PixelFormat);
foreach (TemplateMatch m in matchings)
{

        Drawing.Rectangle(data, m.Rectangle, Color.White);

    MessageBox.Show(m.Rectangle.Location.ToString());
    // do something else with matching
}
sourceImage.UnlockBits(data);

关于c# - 如何在另一张图片中找到模板图片? Kinect 和 AForge 首选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971734/

相关文章:

java - 为什么位图大小在不同的屏幕上会发生变化?

pattern-matching - sparql模式匹配

C# Visual Studio 2010 : make custom preset classes

c# - 使用 XML-RPC 编写以 C#、Ruby 和 Java 编写的分布式应用程序

c# - Azure 应用服务 - 从应用程序内管理扩展

c# - 在图片框控件中显示后处理位图

c# - Visual Studio 项目中的每个引用都显示为无效

java - 如何在平板电脑上显示 Bitmap/Drawable 作为 centerCrop 以及在手机上显示 fitXY

pattern-matching - 编译器中 AST 结构的模式匹配

scala - 为什么 scala 编译器有时会在 "pattern matching"上发出警告,有时却不会?