C#处理灰度系数代码


 

          gamma = 1 / gamma;
           byte[] result = null;
           using (Mat target = ImgDecode(data))
           {
               byte[] lut = new byte[256];
               for (int count = 0; count < lut.Length; count++)
               {
                   lut[count] = Convert.ToByte((Math.Pow((float)(count / 255.0), gamma)) * 255.0f);
               }

               int numChannels = target.NumberOfChannels;

               byte[] imgData = new byte[target.Width * target.Height * numChannels];
               target.CopyTo<byte>(imgData);


               for (int pos = 0; pos < imgData.Length; pos += numChannels)
               {
                   for (int num = 0; num < numChannels; num++)
                   {
                       imgData[pos + num] = lut[imgData[pos + num]];
                   }
               }

               Image<Rgba, byte> img = new Image<Rgba, byte>(target.Width, target.Height);
               img.Bytes = imgData;
               var buffer = new VectorOfByte();
               CvInvoke.Imencode(".png", img, buffer);
               result = buffer.ToArray();
           }
           return result;