zoukankan      html  css  js  c++  java
  • C# 图片处理之:彩色图片转为黑白图 .

    首先解释下所谓的黑白图片。其实更准确地应该叫256级灰度图。当一个颜色点的R=G=B时,就是我们所谓的“灰色”。由于RGB的取值范围在[0,255],所以一共只有256种可能。

    所以彩色图片转为黑白图片的原理非常简单。只要扫描彩图的每一点,让输出图对应点的R=G=B就成了。现在问题的关键就是如何取值了。

    一般有两种,一种是彩图RGB三分量的算数平均值,另一种是加权平均值。加权平均是考虑到人类眼睛对不同分量的敏感程度。 

     具体代码如下:



            /**//// <summary>
            
    /// 变成黑白图
            
    /// </summary>
            
    /// <param name="bmp">原始图</param>
            
    /// <param name="mode">模式。0:加权平均  1:算数平均</param>
            
    /// <returns></returns>

            private Bitmap ToGray(Bitmap bmp,int mode)
            ...{
                if (bmp == null)
                ...{
                    return null;
                }


                int w = bmp.Width;
                int h = bmp.Height;
                try
                ...{
                    byte newColor = 0;
                    BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                    unsafe
                    ...{
                        byte* p = (byte*)srcData.Scan0.ToPointer();
                        for (int y = 0; y < h; y++)
                        ...{
                            for (int x = 0; x < w; x++)
                            ...{

                                if (mode == 0) // 加权平均
                                ...{
                                    newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
                                }

                                else    // 算数平均
                                ...{
                                    newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
                                }

                                p[0] = newColor;
                                p[1] = newColor;
                                p[2] = newColor;

                                p += 3;
                            }

                            p += srcData.Stride - w * 3;
                        }

                        bmp.UnlockBits(srcData);
                        return bmp;
                    }

                }

                catch
                ...{
                    return null;
                }


            }
  • 相关阅读:
    application.properties多环境配置文件、jar包外部配置文件、配置项加密、程序中配置使用
    SpringBoot项目打war包部署Tomcat教程
    spring boot 集成 redis lettuce
    spring boot maven打包可运行jar包
    IDEA项目搭建十四——Web站点Controller基类及布局页静态资源设计
    Nginx Windows详细安装部署教程
    多线程编程CompletableFuture与parallelStream
    IDEA项目搭建十三——服务消费端与生产端通信实现
    【异常】MySQL建表报错:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"order"' at line 1
    【警告】mysql链接警告信息:Establishing SSL connection without server's identity verification is not recommended
  • 原文地址:https://www.cnblogs.com/chennie/p/2324597.html
Copyright © 2011-2022 走看看