zoukankan      html  css  js  c++  java
  • c# winform 把彩色图片转换为灰色的图片,变灰,灰度图片,速度很快,safe,unsafe

    把彩色图片转换为灰色的图片,直接用.net接口遍历每个像素点转换的效率非常低,800K的图片65万像素我的电脑要用5分钟,而用了unsafe,速度提高了几千倍,同样的图片只用了0.几秒 

    附一个常用的遍历像素点转换的代码 

    构造函数 

    C#代码  收藏代码
    1. public Tphc()  
    2. {  
    3.     InitializeComponent();  
    4.     this.pictureBox1.ImageLocation = "F:\黑色头发.jpg";  
    5. }  



    按钮单击事件 

    C#代码  收藏代码
    1. private void button3_Click(object sender, EventArgs e)  
    2. {  
    3.     int Height = this.pictureBox1.Image.Height;  
    4.     int Width = this.pictureBox1.Image.Width;  
    5.     Bitmap bitmap = new Bitmap(Width, Height);  
    6.     Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image;  
    7.     Color pixel;  
    8.     for (int x = 0; x < Width; x++)  
    9.         for (int y = 0; y < Height; y++)  
    10.         {  
    11.             pixel = MyBitmap.GetPixel(x, y);  
    12.             int r, g, b, Result = 0;  
    13.             r = pixel.R;  
    14.             g = pixel.G;  
    15.             b = pixel.B;  
    16.             //实例程序以加权平均值法产生黑白图像  
    17.             int iType = 2;  
    18.             switch (iType)  
    19.             {  
    20.                 case 0://平均值法  
    21.                     Result = ((r + g + b) / 3);  
    22.                     break;  
    23.                 case 1://最大值法  
    24.                     Result = r > g ? r : g;  
    25.                     Result = Result > b ? Result : b;  
    26.                     break;  
    27.                 case 2://加权平均值法  
    28.                     Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));  
    29.                     break;  
    30.             }  
    31.             bitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));  
    32.         }  
    33.     this.pictureBox1.Image = bitmap;  
    34. }  


  • 相关阅读:
    接口测试再思考
    Python开发简单爬虫
    正则表达式(Python)
    Git常用方法
    CNN--卷积神经网络从R-CNN到Faster R-CNN的理解(CIFAR10分类代码)
    一看就懂的K近邻算法(KNN),K-D树,并实现手写数字识别!
    我是这样一步步理解--主题模型(Topic Model)、LDA(案例代码)
    你想知道的特征工程,机器学习优化方法都在这了!收藏!
    从似然函数到EM算法(附代码实现)
    一次性弄懂马尔可夫模型、隐马尔可夫模型、马尔可夫网络和条件随机场!(词性标注代码实现)
  • 原文地址:https://www.cnblogs.com/gc2013/p/3836146.html
Copyright © 2011-2022 走看看