Performing multiple image averaging in c#

风格不统一 提交于 2019-12-25 18:24:23

问题


I wish to calculate an average image from 3 different images of the same size that I have. I know this can be done with ease in matlab..but how do I go about this in c#? Also is there an Aforge.net tool I can use directly for this purpose?


回答1:


I have found an article on SO which might point you in the right direction. Here is the code (unsafe)

BitmapData srcData = bm.LockBits(
            new Rectangle(0, 0, bm.Width, bm.Height), 
            ImageLockMode.ReadOnly, 
            PixelFormat.Format32bppArgb);

int stride = srcData.Stride;

IntPtr Scan0 = srcData.Scan0;

long[] totals = new long[] {0,0,0};

int width = bm.Width;
int height = bm.Height;

unsafe
{
  byte* p = (byte*) (void*) Scan0;

  for (int y = 0; y < height; y++)
  {
    for (int x = 0; x < width; x++)
    {
      for (int color = 0; color < 3; color++)
      {
        int idx = (y*stride) + x*4 + color;

        totals[color] += p[idx];
      }
    }
  }
}

int avgB = totals[0] / (width*height);
int avgG = totals[1] / (width*height);
int avgR = totals[2] / (width*height);

Here is the link to the article: How to calculate the average rgb color values of a bitmap




回答2:


ImageMagick can do this for you very simply - at the command-line you could type this:

convert *.bmp -evaluate-sequence mean output.jpg

You could equally calculate the median (instead of the mean) of a bunch of TIFF files and save in a PNG output file like this:

convert *.tif -evaluate-sequence median output.png

Easy, eh?

There are bindings for C/C++, Perl, PHP and all sorts of other janguage interfaces, including (as kindly pointed out by @dlemstra) the Magick.Net binding available here.

ImageMagick is powerful, free and available here.



来源:https://stackoverflow.com/questions/24178490/performing-multiple-image-averaging-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!