• 17.3.2.5 灰度(内存处理)


    版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

    灰度的算法请参看第17.3.1.3节。17.3.1.3 灰度-CSDN博客

    【例 17.43灰度算法一:最大值法。

    1. //灰度:最大值
    2. private void btnGray1_Click(object sender, EventArgs e)
    3. {
    4. Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
    5. BitmapData sourceData = sourceImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    6. BitmapData destData = destImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
    7. IntPtr pSource = sourceData.Scan0;
    8. int allBytes = sourceData.Stride * sourceData.Height;
    9. byte[] rgbvalues = new byte[allBytes];
    10. Marshal.Copy(pSource, rgbvalues, 0, allBytes);
    11. int pos = 0;
    12. int R, G, B;
    13. int avgValue;
    14. for (int j = 0; j < sourceData.Height; j++)
    15. {
    16. for (int i = 0; i < sourceData.Width; i++)
    17. {
    18. B = rgbvalues[pos];
    19. G = rgbvalues[pos + 1];
    20. R = rgbvalues[pos + 2];
    21. byte MaxColor;
    22. MaxColor =(byte)( R > G? R: G);
    23. MaxColor = MaxColor > (byte)B ? MaxColor: (byte)B;
    24. rgbvalues[pos] = MaxColor;
    25. rgbvalues[pos + 1] = MaxColor;
    26. rgbvalues[pos + 2] = MaxColor;
    27. pos = pos + 3;
    28. }
    29. pos = pos + sourceData.Stride - sourceData.Width * 3;
    30. }
    31. IntPtr pDest = destData.Scan0;
    32. Marshal.Copy(rgbvalues, 0, pDest, allBytes);
    33. sourceImg.UnlockBits(sourceData);
    34. destImg.UnlockBits(destData);
    35. picDest.Image = destImg;
    36. }

    【例 17.44灰度算法二:平均值法。

    1. //灰度:均值法
    2. private void btnGray2_Click(object sender, EventArgs e)
    3. {
    4. Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
    5. BitmapData sourceData = sourceImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    6. BitmapData destData = destImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
    7. IntPtr pSource = sourceData.Scan0;
    8. int allBytes = sourceData.Stride * sourceData.Height;
    9. byte[] rgbvalues = new byte[allBytes];
    10. Marshal.Copy(pSource, rgbvalues, 0, allBytes);
    11. int pos = 0;
    12. int R, G, B;
    13. int avgValue;
    14. for (int j = 0; j < sourceData.Height; j++)
    15. {
    16. for (int i = 0; i < sourceData.Width; i++)
    17. {
    18. B = rgbvalues[pos];
    19. G = rgbvalues[pos + 1];
    20. R = rgbvalues[pos + 2];
    21. avgValue = (B + G + R) / 3;
    22. rgbvalues[pos] = (byte)avgValue;
    23. rgbvalues[pos + 1] = (byte)avgValue;
    24. rgbvalues[pos + 2] = (byte)avgValue;
    25. pos = pos + 3;
    26. }
    27. pos = pos + sourceData.Stride - sourceData.Width * 3;
    28. }
    29. IntPtr pDest = destData.Scan0;
    30. Marshal.Copy(rgbvalues, 0, pDest, allBytes);
    31. sourceImg.UnlockBits(sourceData);
    32. destImg.UnlockBits(destData);
    33. picDest.Image = destImg;
    34. }

    【例 17.45灰度算法三:指数加权法。

    1. //灰度:指数加权法
    2. private void btnGray3_Click(object sender, EventArgs e)
    3. {
    4. Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
    5. BitmapData sourceData = sourceImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    6. BitmapData destData = destImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
    7. IntPtr pSource = sourceData.Scan0;
    8. int allBytes = sourceData.Stride * sourceData.Height;
    9. byte[] rgbvalues = new byte[allBytes];
    10. Marshal.Copy(pSource, rgbvalues, 0, allBytes);
    11. int pos = 0;
    12. int R, G, B;
    13. byte y;
    14. for (int j = 0; j < sourceData.Height; j++)
    15. {
    16. for (int i = 0; i < sourceData.Width; i++)
    17. {
    18. B = rgbvalues[pos];
    19. G = rgbvalues[pos + 1];
    20. R = rgbvalues[pos + 2];
    21. y =(byte)( R * 0.3 + G * 0.59 + B * 0.11);
    22. rgbvalues[pos] = y;
    23. rgbvalues[pos + 1] = y;
    24. rgbvalues[pos + 2] = y;
    25. pos = pos + 3;
    26. }
    27. pos = pos + sourceData.Stride - sourceData.Width * 3;
    28. }
    29. IntPtr pDest = destData.Scan0;
    30. Marshal.Copy(rgbvalues, 0, pDest, allBytes);
    31. sourceImg.UnlockBits(sourceData);
    32. destImg.UnlockBits(destData);
    33. picDest.Image = destImg;
    34. }

    学习更多vb.net知识,请参看vb.net 教程 目录

    学习更多C#知识,请参看 C# 教程 目录

  • 相关阅读:
    【数据分享】上海市出租车GPS数据
    MySQL 的 C 语言接口
    【React】《React 学习手册 (第2版) 》笔记-Chapter3-JavaScript 函数式编程
    Linux内核分析(十六)--内存管理之管理机制
    BUUCTF WEB filejava
    2023年腾讯云2核4G配置服务器性价比怎么样?
    Taurus.MVC WebAPI 入门开发教程8:WebAPI文档与自动化测试。
    双非本计算机从零开始三年努力能做到什么程度【学习路线回顾&总结&问答】
    C++ 学习(17)STL - vector容器、string容器
    站点部署之Docker篇
  • 原文地址:https://blog.csdn.net/UruseiBest/article/details/136128141