【问题标题】:Performing multiple image averaging in c#在 C# 中执行多个图像平均
【发布时间】:2014-08-02 10:18:27
【问题描述】:

我希望从我拥有的 3 张相同尺寸的不同图像中计算出平均图像。我知道这可以在 matlab 中轻松完成..但是我该如何在 c# 中进行呢?还有一个我可以直接用于此目的的 Aforge.net 工具吗?

【问题讨论】:

  • 你能解释一下图像平均是什么意思吗?是三张图像的像素 RGB 平均值吗?
  • 是的@RobertJ。 RGB 平均就是我的意思
  • 您希望这个过程快速还是简单?
  • 快!..我已经尝试锁定位图并取RGB值的平均值并将其分配给结果图像,但我得到的结果很荒谬......
  • 好吧,LockBits 将是我的方式。也许问题出在您的计算中?你是如何得到 RGB 值的平均值的?你有没有尝试平均每个 R |克| B 表示三张图像,或者您只是将每张图像的 RGB 的值相乘并取平均值?

标签: c# .net image-processing aforge


【解决方案1】:

ImageMagick 可以非常简单地为您执行此操作 - 您可以在命令行中输入以下内容:

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

您同样可以计算一堆 TIFF 文件的中位数(而不是平均值)并保存在一个 PNG 输出文件中,如下所示:

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

简单吧?

有针对 C/C++、Perl、PHP 和各种其他语言接口的绑定,包括(正如@dlemstra 所指出的)Magick.Net 绑定可用here

ImageMagick 功能强大、免费且可用here

【讨论】:

【解决方案2】:

我找到了一篇关于 SO 的文章,它可能会为您指明正确的方向。这是代码(不安全)

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

这里是文章的链接: How to calculate the average rgb color values of a bitmap

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 2023-03-07
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多