C#更快的方式来比较两个图像之间的像素,只写出差异 [英] C# faster way to compare pixels between two images and only write out the differences

查看:1013
本文介绍了C#更快的方式来比较两个图像之间的像素,只写出差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是对两个非常相似的图像(一个是另一个的编辑版本)进行逐像素比较,并将差异写出到新文件中。



($ y $ 0; y< height; y ++)
{
for(int x = 0; x< width; x ++)
{
pix1 = src.GetPixel(x,y);
pix2 = comp.GetPixel(x,y);
if(pix1!= pix2)
{
dest.SetPixel(x,y,pix1);
}
}
}

src comp 是要比较的两个图像, dest 只是一个新图像。
需要相当长的时间。



有什么更快的方法可以做到这一点?

也许没有必要真正得到像素为了比较它?

解决方案

比较你需要读取它们的像素。但是, GetPixel()是一种非常慢的方法,除非您只是检查非常少量的数据,否则不建议这样做。



为了获得更好的性能,最好的方法是使用不安全代码并使用指针代替。互联网上有很多这样的样本,下面是我发现的一个解释问题并提供两种不同的解决方案。



http:// davidthomasbernal。 com / blog / 2008/03/13 / c-image-processing-performance-unsafe-vs-safe-code-part-i



确保检查第二部分,他有一些基准和链接到完整的来源。


Just doing a pixel-by-pixel comparison of two images that are very similar (one is an edited version of another) and writing out the differences to a new file.

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        pix1 = src.GetPixel(x, y);
        pix2 = comp.GetPixel(x, y);
        if (pix1 != pix2)
        {
            dest.SetPixel(x, y, pix1);
        }
    }
}

src and comp are the two images to compare and dest is just a new image. It takes a fairly long time.

What's a faster way to do this?
Maybe it's not necessary to actually get the pixel in order to compare it?

解决方案

To compare the pixels you need to read them. However, GetPixel() is a very slow method of doing this and not recommended unless you are just checking a very small amount of data.

To get better performance the best way is to go with unsafe-code and use pointers instead. There are lots of samples of this on the internet, below is one that I found that explains the problems a bit and offers two different solutions to this.

http://davidthomasbernal.com/blog/2008/03/13/c-image-processing-performance-unsafe-vs-safe-code-part-i

Be sure to check part two as well where he has some benchmarks and links to the complete source.

这篇关于C#更快的方式来比较两个图像之间的像素,只写出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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