Performing multiple image averaging in c#

2019-09-20 08:04发布

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?

2条回答
时光不老,我们不散
2楼-- · 2019-09-20 08:29

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.

查看更多
Luminary・发光体
3楼-- · 2019-09-20 08:35

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

查看更多
登录 后发表回答