【发布时间】:2022-02-25 03:02:40
【问题描述】:
我从这里复制了 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 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 检测已知物体,在我的例子中是一只橡皮鸭。
谢谢你。
【问题讨论】:
-
好的,这里的答案是,它需要 Format24bppRgb-Bitmaps。尽管如此,结果还是很糟糕。数百个具有相同位置和大小的匹配项。
-
在切换到 openCV 和 Emgu Wrapper 之前,我遇到了同样的性能缓慢问题。由于原始发帖人要求更快的解决方案并且没有收到回复,因此我提供了一个链接,与 AForge 相比,该链接将在更短的时间内完成所需的任务。 Aforge 很慢,因为它是一种迭代(蛮力)方法。海报代码甚至比 AForge 还要慢。 openCV 库将非常快速地完成所需的任务,[使用基于数学函数的比较](docs.opencv.org/2.4/doc/tutorials/img
标签: c# bitmap pattern-matching kinect aforge