C# 检查一个图像是否存在于另一个图像中

发布于 2024-08-14 08:51:24 字数 175 浏览 2 评论 0原文

我不知道从哪里开始,所以一些指导会很好。我需要实现的是,检查一个大图像(例如 1280x1024)并检查其中是否存在另一个较小的图像(可能是 50x50 像素图像)。

我尝试通过比较每个像素来做到这一点,这非常慢,而且我可能需要执行 100 多次,所以它看起来不合适。我只是想知道是否有更好的方法?

谢谢

I'm not sure where to start with this so some guidance would be good. What I need to achieve is, examine a large image (say 1280x1024) and check to see if another smaller image exists within it or not (maybe a 50x50 pixel image).

I tried doing this by comparing every pixel which is really slow and I may need to do it 100+ times so it doesn't seem suitable. I'm just wondering if there is a better way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼睛不忧郁 2024-08-21 08:51:24

我只是在做类似的事情,我想出的快速而肮脏的结果是使用 AForge.Net 的“ExhaustiveTemplateMatching”实现,图像大小是其大小的 1/4。全尺寸的 720p 图像需要几分钟的时间,但在我的小电脑上,1/4 尺寸的图像大约需要一秒钟。

public static class BitmapExtensions
{
    /// <summary>
    /// See if bmp is contained in template with a small margin of error.
    /// </summary>
    /// <param name="template">The Bitmap that might contain.</param>
    /// <param name="bmp">The Bitmap that might be contained in.</param>        
    /// <returns>You guess!</returns>
    public static bool Contains(this Bitmap template, Bitmap bmp)
    {
        const Int32 divisor = 4;
        const Int32 epsilon = 10;

        ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.9f);                      

        TemplateMatch[] tm = etm.ProcessImage(
            new ResizeNearestNeighbor(template.Width / divisor, template.Height / divisor).Apply(template),
            new ResizeNearestNeighbor(bmp.Width / divisor, bmp.Height / divisor).Apply(bmp)
            );

        if (tm.Length == 1)
        {
            Rectangle tempRect = tm[0].Rectangle;

            if (Math.Abs(bmp.Width / divisor - tempRect.Width) < epsilon
                &&
                Math.Abs(bmp.Height / divisor - tempRect.Height) < epsilon)
            {
                return true;
            }                
        }

        return false;
    }
}

您当然也可以只检查 tm.length > 0 是的,其中有一些不必要的划分:P

I was just working on something similar and the quick and dirty result I came up with is to use AForge.Net's implementation of "ExhaustiveTemplateMatching" with images 1/4 of their size. 720p images at full size took a couple minutes but at 1/4 size it's about a second on my puny computer.

public static class BitmapExtensions
{
    /// <summary>
    /// See if bmp is contained in template with a small margin of error.
    /// </summary>
    /// <param name="template">The Bitmap that might contain.</param>
    /// <param name="bmp">The Bitmap that might be contained in.</param>        
    /// <returns>You guess!</returns>
    public static bool Contains(this Bitmap template, Bitmap bmp)
    {
        const Int32 divisor = 4;
        const Int32 epsilon = 10;

        ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.9f);                      

        TemplateMatch[] tm = etm.ProcessImage(
            new ResizeNearestNeighbor(template.Width / divisor, template.Height / divisor).Apply(template),
            new ResizeNearestNeighbor(bmp.Width / divisor, bmp.Height / divisor).Apply(bmp)
            );

        if (tm.Length == 1)
        {
            Rectangle tempRect = tm[0].Rectangle;

            if (Math.Abs(bmp.Width / divisor - tempRect.Width) < epsilon
                &&
                Math.Abs(bmp.Height / divisor - tempRect.Height) < epsilon)
            {
                return true;
            }                
        }

        return false;
    }
}

You could also of course just check for tm.length > 0 and yes there are some unnecessary divides in there :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文