如果图像另一幅图像中存在C#检查 [英] C# Checking if an image exists within another image

查看:352
本文介绍了如果图像另一幅图像中存在C#检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道哪里有这种让一些指导,开始将是一件好事。我需要实现什么,检查一个大的图像(比如1280×1024),并检查是否有其他更小的图像内与否(可能是50×50像素的图像)的存在。

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).

我试图通过每个像素这实在是比较慢的这样做的,我可能需要做的是100+次,似乎适合它不会。我只是想知道是否有更好的办法?

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?

感谢

推荐答案

我只是工作类似的东西和快速和肮脏的结果,我想出了是使用AForge.Net的实现ExhaustiveTemplateMatching与图像大小的1/4。全尺寸的720p影像花了几分钟,但在1/4大小这是关于我的微不足道的计算机上的第二个。

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;
    }
}

您也当然只是检查tm.length > 0,是里面还有一些不必要的分歧:P

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

这篇关于如果图像另一幅图像中存在C#检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆