欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#图像处理之图像均值方差计算的方法

程序员文章站 2022-05-26 15:18:44
本文实例讲述了c#图像处理之图像均值方差计算的方法。分享给大家供大家参考。具体如下: //本函数均是基于rgb颜色空间计算 //定义图像均值函数(rgb空间)...

本文实例讲述了c#图像处理之图像均值方差计算的方法。分享给大家供大家参考。具体如下:

//本函数均是基于rgb颜色空间计算
//定义图像均值函数(rgb空间)
public double anbitmap(bitmap a)
{
  double v = 0;
  rectangle rect = new rectangle(0, 0, a.width, a.height);
  system.drawing.imaging.bitmapdata bmpdata = a.lockbits(rect, system.drawing.imaging.imagelockmode.readwrite, system.drawing.imaging.pixelformat.format24bpprgb);
  unsafe
  {
   byte* pin = (byte*)bmpdata.scan0.topointer();
   byte* p;
   int r, g, b;
   double meanvalue = 0, sum = 0; 
   int stride = bmpdata.stride;
   for (int y = 0; y < a.height; y++)
   {
     for (int x = 0; x < a.width; x++)
     {
     p = pin;
     b = p[0];
     g = p[1];
     r = p[2];
     sum += b * 0.114 + g * 0.587 + r * 0.299;
     pin += 3;
     }
     pin += stride - a.width * 3;
   }
   meanvalue = sum / (a.width * a.height);
   v = meanvalue;
  }
  a.unlockbits(bmpdata);
  return v;  //返回图像均值v
}
//定义图像统计方差函数(rgb空间)
public double anconbitmap(bitmap a,double meanvalue)
{
  double v = 0;
  rectangle rect = new rectangle(0, 0, a.width, a.height);
  system.drawing.imaging.bitmapdata bmpdata = a.lockbits(rect, system.drawing.imaging.imagelockmode.readwrite, system.drawing.imaging.pixelformat.format24bpprgb);
  unsafe
  {
   byte* pin = (byte*)bmpdata.scan0.topointer();
   byte* p;
   int r, g, b;
   double conv = 0, sum = 0;
   int stride = bmpdata.stride;
   for (int y = 0; y < a.height; y++)
   {
     for (int x = 0; x < a.width; x++)
     {
     p = pin;
     b = p[0];
     g = p[1];
     r = p[2];
     sum += (b * 0.114 + g * 0.587 + r * 0.299 - meanvalue) * (b * 0.114 + g * 0.587 + r * 0.299 - meanvalue);
     pin += 3;
     }
     pin += stride - a.width * 3;
    }
    conv = sum / (a.width * a.height-1);
   v = conv;
  }
  a.unlockbits(bmpdata);
  return v;  //返回图像方差v
}

希望本文所述对大家的c#程序设计有所帮助。