欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#實現(xiàn)圖片二值化例子(黑白效果)

 更新時間:2014年08月15日 08:50:19   投稿:junjie  
這篇文章主要介紹了c#實現(xiàn)圖片二值化例子,圖像二值化就是將圖像上的像素點的灰度值設(shè)置為0或255,也就是將整個圖像呈現(xiàn)出明顯的黑白效果,需要的朋友可以參考下

C#將圖片2值化示例代碼,原圖及二值化后的圖片如下:

原圖:

二值化后的圖像:

實現(xiàn)代碼:

using System;
using System.Drawing;
namespace BMP2Grey
{
  class Program
  {
    static void ToGrey(Bitmap img1)
    {
      for (int i = 0; i < img1.Width; i++)
      {
        for (int j = 0; j < img1.Height; j++)
        {
          Color pixelColor = img1.GetPixel(i, j);
          //計算灰度值
          int grey = (int)(0.299 * pixelColor.R + 0.587 * pixelColor.G + 0.114 * pixelColor.B);
          Color newColor = Color.FromArgb(grey, grey, grey);
          img1.SetPixel(i, j, newColor);
        }
      }
    }
    static void Thresholding(Bitmap img1)
    {
      int[] histogram = new int[256];
      int minGrayValue=255, maxGrayValue=0;
      //求取直方圖
      for (int i = 0; i < img1.Width; i++)
      {
        for (int j = 0; j < img1.Height; j++)
        {
          Color pixelColor = img1.GetPixel(i, j);
          histogram[pixelColor.R]++;
          if (pixelColor.R > maxGrayValue) maxGrayValue = pixelColor.R;
          if (pixelColor.R < minGrayValue) minGrayValue = pixelColor.R;
        }
      }
      //迭代計算閥值
      int threshold = -1;
      int newThreshold = (minGrayValue + maxGrayValue) / 2;
      for(int iterationTimes = 0; threshold != newThreshold && iterationTimes < 100; iterationTimes++)
      {
        threshold = newThreshold;
        int lP1 =0;
        int lP2 =0;
        int lS1 = 0;
        int lS2 = 0;
        //求兩個區(qū)域的灰度的平均值
        for (int i = minGrayValue;i < threshold;i++)
        {
          lP1 += histogram[i] * i;
          lS1 += histogram[i];
        }
        int mean1GrayValue = (lP1 / lS1);
        for (int i = threshold+1;i < maxGrayValue;i++)
        {
          lP2 += histogram[i] * i;
          lS2 += histogram[i];
        }
        int mean2GrayValue = (lP2 / lS2);
        newThreshold = (mean1GrayValue + mean2GrayValue) / 2;
      }
      //計算二值化
      for (int i = 0; i < img1.Width; i++)
      {
        for (int j = 0; j < img1.Height; j++)
        {
          Color pixelColor = img1.GetPixel(i, j);
          if (pixelColor.R > threshold) img1.SetPixel(i, j, Color.FromArgb(255, 255, 255));
          else img1.SetPixel(i, j, Color.FromArgb(0, 0, 0));
        }
      }
    }
    static void Main(string[] args)
    {
      try
      {
        //打開位圖文件
        Bitmap img1 = new Bitmap("test.jpg", true);
        //灰度化
        ToGrey(img1);
        //二值化
        Thresholding(img1);
        //寫回位圖文件
        img1.Save("output.jpg");
        Console.WriteLine("Converted.");
      }
      catch (ArgumentException)
      {
        Console.WriteLine("Invalid usage!");
        Console.WriteLine("Usage: bmp2grey source object");
      }
    }
  }
}

相關(guān)文章

  • C#實現(xiàn)撲克游戲(21點)的示例代碼

    C#實現(xiàn)撲克游戲(21點)的示例代碼

    21點又名黑杰克,該游戲由2到6個人玩,使用除大小王之外的52張牌,游戲者的目標是使手中的牌的點數(shù)之和不超過21點且盡量大。本文將用C#實現(xiàn)這一經(jīng)典游戲,需要的可以參考一下
    2022-08-08
  • C#使用HtmlAgilityPack實現(xiàn)解析提取HTML內(nèi)容

    C#使用HtmlAgilityPack實現(xiàn)解析提取HTML內(nèi)容

    HtmlAgilityPack是一個HTML解析類庫,這篇文章主要為大家詳細介紹了C#如何使用HtmlAgilityPack實現(xiàn)解析提取HTML內(nèi)容,感興趣的小伙伴可以參考一下
    2023-12-12
  • C#中判斷一個集合是否是另一個集合的子集的簡單方法

    C#中判斷一個集合是否是另一個集合的子集的簡單方法

    本文介紹利用C#中內(nèi)置的系統(tǒng)函數(shù)判斷一個集合是否是一個集合的子集的方法,此方法代碼量極少,分享給大家。
    2016-04-04
  • C# 如何規(guī)范的寫 DEBUG 輸出

    C# 如何規(guī)范的寫 DEBUG 輸出

    本文來告訴大家一個規(guī)范,如何去寫 DEBUG 的輸出。本文給大家介紹的非常詳細,需要的朋友參考下吧
    2017-12-12
  • C# NullReferenceException解決案例講解

    C# NullReferenceException解決案例講解

    這篇文章主要介紹了C# NullReferenceException解決案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C#獲取路徑的幾種方式實例分析

    C#獲取路徑的幾種方式實例分析

    這篇文章主要介紹了C#獲取路徑的幾種方式,實例分析了C#常用的路徑操作技巧,需要的朋友可以參考下
    2015-06-06
  • C#中Task.Yield的用途深入講解

    C#中Task.Yield的用途深入講解

    這篇文章主要給大家介紹了關(guān)于C#中Task.Yield的用途的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • C#中struct與class的區(qū)別詳解

    C#中struct與class的區(qū)別詳解

    本文主要介紹了C#中struct與class的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法

    DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法

    這篇文章主要介紹了DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法,在項目開發(fā)中有應(yīng)用價值,需要的朋友可以參考下
    2014-08-08
  • VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法

    VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法

    這篇文章主要介紹了VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評論