程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#完成圖片二值化例子(诟谇後果)

c#完成圖片二值化例子(诟谇後果)

編輯:C#入門知識

c#完成圖片二值化例子(诟谇後果)。本站提示廣大學習愛好者:(c#完成圖片二值化例子(诟谇後果))文章只能為提供參考,不一定能成為您想要的結果。以下是c#完成圖片二值化例子(诟谇後果)正文


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

原圖:

二值化後的圖象:

完成代碼:

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;
        //求兩個區域的灰度的均勻值
        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");
      }
    }
  }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved