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

C#图像线性变换的方法

程序员文章站 2023-01-22 12:35:03
本文实例讲述了c#图像线性变换的方法。分享给大家供大家参考。具体如下: //定义图像线性运算函数(y=kx+v) private static bitmap l...

本文实例讲述了c#图像线性变换的方法。分享给大家供大家参考。具体如下:

//定义图像线性运算函数(y=kx+v)
private static bitmap linearop(bitmap a, double k, double v)
{
  rectangle rect = new rectangle(0, 0, a.width, a.height);
  system.drawing.imaging.bitmapdata srcdata = a.lockbits(rect, system.drawing.imaging.imagelockmode.readwrite, a.pixelformat);
  intptr ptr = srcdata.scan0;
  int bytes = 0;
  bytes = srcdata.stride * a.height;
  byte[] grayvalues = new byte[bytes];
  system.runtime.interopservices.marshal.copy(ptr, grayvalues, 0, bytes);
  int temp = 0;
  for (int i = 0; i < bytes; i++)
  {
   temp = (int)(k * grayvalues[i] + v + 0.5);
   temp = (temp > 255) ? 255 : temp < 0 ? 0 : temp;
   grayvalues[i] = (byte)temp;
  }
  system.runtime.interopservices.marshal.copy(grayvalues, 0, ptr, bytes);
  a.unlockbits(srcdata);
  return a;
}

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