• <td id="ae6ms"><li id="ae6ms"></li></td>
  • <xmp id="ae6ms"><td id="ae6ms"></td><table id="ae6ms"></table>
  • <table id="ae6ms"></table>
  • <td id="ae6ms"></td>
    <td id="ae6ms"></td>
  • <table id="ae6ms"></table><table id="ae6ms"><td id="ae6ms"></td></table>
  • <td id="ae6ms"></td>
  • <table id="ae6ms"><li id="ae6ms"></li></table>
  • <table id="ae6ms"></table>
    西西軟件園多重安全檢測下載網站、值得信賴的軟件下載站!
    軟件
    軟件
    文章
    搜索

    首頁編程開發C#.NET → C#數字圖像處理的3種方法::提取像素法、內存法和指針法

    C#數字圖像處理的3種方法::提取像素法、內存法和指針法

    相關軟件相關文章發表評論 來源:西西整理時間:2014/1/28 16:06:56字體大?。?em class="fontsize">A-A+

    作者:西西點擊:725次評論:0次標簽: 圖像處理

    FlashPaper圖像處理軟件v2.2 綠色漢化版
    • 類型:圖像處理大?。?i>4.2M語言:中文 評分:3.9
    • 標簽:
    立即下載

    本文主要通過彩色圖象灰度化來介紹C#處理數字圖像的3種方法,Bitmap類、BitmapData類和Graphics類是C#處理圖像的的3個重要的類。

    Bitmap只要用于處理由像素數據定義的圖像的對象,主要方法和屬性如下:

             GetPixel方法和SetPixel方法,獲取和設置一個圖像的指定像素的顏色。

             PixelFormat屬性,返回圖像的像素格式。

             Palette屬性,獲取或折紙圖像所使用的顏色調色板。

             Height屬性和Width屬性,返回圖像的高度和寬度。

             LockBits方法和UnlockBits方法,分別鎖定和解鎖系統內存中的位圖像素。

    BitmapData對象指定了位圖的屬性:

             Height屬性,被鎖定位圖的高度。

             Width屬性,被鎖定位圖的寬度。

             PixelFormat屬性,數據的實際像素格式。

             Scan0屬性,被鎖定數組的首字節地址。

             Stride屬性,步幅,也稱掃描寬度。

    彩色圖象灰度化

    24位彩色圖象每個像素用3個字節表示,每個字節對應著R、G、B分量的亮度(紅、綠、藍)。當3個分量不想同時表現為灰度圖像。下面有三種轉換公式:

    Gray(I,j)為轉換后的灰度圖像在(I,j)點出的灰度值。由于人眼對顏色的感應不同,有了下面的轉換公式:

    觀察發現綠色所占比重最大,所以轉換時直接使用G值作為轉換結果:

    圖像處理的3種方法分別是:提取像素法、內存法和指針法,它們各自有各自的特點。

    提取像素法

    使用的是GDI+中的Bitmap.GetPixel和Bitmap.SetPixel方法。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    if (bitmap != null)
    {
        newbitmap = bitmap.Clone() as Bitmap;
        Color pixel;
        int ret;
        for (int x = 0; x < newbitmap.Width; x++)
        {
            for (int y = 0; y < newbitmap.Height; y++)
            {
                pixel = newbitmap.GetPixel(x, y);
                ret = (int)(pixel.R * 0.299 + pixel.G * 0.587 + pixel.B * 0.114);
                newbitmap.SetPixel(x, y, Color.FromArgb(ret, ret, ret));
            }
        }
        pictureBox1.Image = newbitmap.Clone() as Image;
    }

    內存法

    內存法是把圖像數據直接復制到內存中,這樣程序的運行速度就能大大提高了。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    if (bitmap != null)
    {
        newbitmap = bitmap.Clone() as Bitmap;
        Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height);
        System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat);
        IntPtr ptr = bmpdata.Scan0;
        int bytes = newbitmap.Width * newbitmap.Height * 3;
        byte[] rgbvalues = new byte[bytes];
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, bytes);
        double colortemp = 0;
        for (int i = 0; i < rgbvalues.Length; i += 3)
        {
            colortemp = rgbvalues[i + 2] * 0.299 + rgbvalues[i + 1] * 0.587 + rgbvalues[i] * 0.114;
            rgbvalues[i] = rgbvalues[i + 1] = rgbvalues[i + 2] = (byte)colortemp;
        }
        System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr, bytes);
        newbitmap.UnlockBits(bmpdata);
        pictureBox1.Image = newbitmap.Clone() as Image;
    }

    指針法

    這個方法和內存法相似,開始都是通過LockBits方法來獲取位圖的首地址,這個方法更簡潔,直接用指針進行位圖操作。所以對內存的操作需要在unsafe下進行操作。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    if (bitmap != null)
    {
        newbitmap = bitmap.Clone() as Bitmap;
        Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height);
        System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat);
        byte temp;
        unsafe
        {
            byte* ptr = (byte*)(bmpdata.Scan0);
            for (int x = 0; x < bmpdata.Width; x++)
            {
                for (int y = 0; y < bmpdata.Height; y++)
                {
                    temp = (byte)(0.299 * ptr[2] + 0.587 * ptr[1] + 0.114 * ptr[0]);
                    ptr[0] = ptr[1] = ptr[2] = temp;
                    ptr += 3;
                }
                ptr += bmpdata.Stride - bmpdata.Width * 3;
            }
        }
        newbitmap.UnlockBits(bmpdata);
        pictureBox1.Image = newbitmap.Clone() as Image;
    }

    3種方法的比較

    比較一下可以得出結論,提取像素法比較簡單,但是效率比較低;內存法效率有了很大的提高,但是代碼比較復雜;指針法效率比內存法更高一些,但是不安全。綜上比較結果內存法比較好,效率即高又能發揮C#安全的優點。

    程序下載:http://pan.baidu.com/s/1bnEJtuf

      圖像處理軟件
      (257)圖像處理軟件
      在學校的時候,圖形圖像處理的學生要學習繪畫和圖像構成等等軟件,西西為大家整理了一些好用的圖像處理軟件大全。圖像處理軟件大全簡介圖像處理軟件是用于處理圖像信息的各種應用軟件的總稱,專業的圖像處理軟件有的系列基于應用的處理管理處理軟件等,還有國內很實用的大眾型軟件彩影,非主流軟件有美圖秀秀,動態圖片處理軟件有,等圖像處理軟件哪個好西西當然是推薦眾所周知的啦,用過的都說好哦,入門也容易,其他美圖秀秀光影...更多>>

      相關評論

      閱讀本文后您有什么感想? 已有人給出評價!

      • 8 喜歡喜歡
      • 3 頂
      • 1 難過難過
      • 5 囧
      • 3 圍觀圍觀
      • 2 無聊無聊

      熱門評論

      最新評論

      發表評論 查看所有評論(0)

      昵稱:
      表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
      字數: 0/500 (您的評論需要經過審核才能顯示)
      女人让男人桶30分钟免费视频,女人张开腿让男人桶个爽,一进一出又大又粗爽视频
    • <td id="ae6ms"><li id="ae6ms"></li></td>
    • <xmp id="ae6ms"><td id="ae6ms"></td><table id="ae6ms"></table>
    • <table id="ae6ms"></table>
    • <td id="ae6ms"></td>
      <td id="ae6ms"></td>
    • <table id="ae6ms"></table><table id="ae6ms"><td id="ae6ms"></td></table>
    • <td id="ae6ms"></td>
    • <table id="ae6ms"><li id="ae6ms"></li></table>
    • <table id="ae6ms"></table>