国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C# 內(nèi)存法圖像處理

2019-11-17 02:27:59
字體:
來源:轉載
供稿:網(wǎng)友

C# 內(nèi)存法圖像處理

內(nèi)存法通過把圖像儲存在內(nèi)存中進行處理,效率大大高于GetPixel方法,安全性高于指針法。

筆者當初寫圖像處理的時候發(fā)現(xiàn)網(wǎng)上多是用GetPixel方法實現(xiàn),提到內(nèi)存法的時候也沒有具體實現(xiàn),所以筆者在這里具體實現(xiàn)一下- -,望指正。

首先講一下用到的一些方法。

1.LockBits和UnlockBits:使用 LockBits方法,可在系統(tǒng)內(nèi)存中鎖定現(xiàn)有的位圖,以便通過編程方式進行更改,每調(diào)用LockBits之后都應該調(diào)用一次UnlockBits。

2.Scan0:圖像的第一個字節(jié)地址。

3.Stride:步幅,掃描寬度,形象的說就是一行的長度。

4.PixelFormat:數(shù)據(jù)的實際像素格式。

給出原圖:

一、灰度

對每個像素點進行加權平均,(方法不唯一)。

        /// <summary>        /// 灰化實現(xiàn)方法        /// </summary>        void Image_Ashing()        {            if (pbshowbox.Image != null)            {                int Height = this.pbshowbox.Image.Height;                int Width = this.pbshowbox.Image.Width;                Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format24bpPRgb);                Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;                BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);                unsafe                {                    byte* pin = (byte*)(oldData.Scan0.ToPointer());                    byte* pout = (byte*)(newData.Scan0.ToPointer());                    for (int y = 0; y < oldData.Height; y++)                    {                        for (int x = 0; x < oldData.Width; x++)                        {                            byte Result = (byte)(pin[0] * 0.1 + pin[1] * 0.2 + pin[2] * 0.7);//加權平均實現(xiàn)灰化                            pout[0] = (byte)(Result);                            pout[1] = (byte)(Result);                            pout[2] = (byte)(Result);                            pin = pin + 3;                            pout = pout + 3;                        }                        pin += oldData.Stride - oldData.Width * 3;                        pout += newData.Stride - newData.Width * 3;                    }                    bitmap.UnlockBits(newData);                    MyBitmap.UnlockBits(oldData);                    this.pbshowbox.Image = bitmap;                }            }            else            {                MessageBox.Show("請先打開一張圖片!");            }        }

二、柔化

像素點與周圍像素點差別較大時取平均值。

        /// <summary>        /// 柔化實現(xiàn)方法        /// </summary>        void Image_Soften()        {            if (pbshowbox.Image != null)            {                int Height = this.pbshowbox.Image.Height;                int Width = this.pbshowbox.Image.Width;                Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);                Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;                BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);                unsafe                {                    byte* pin = (byte*)(oldData.Scan0.ToPointer());                    byte* pout = (byte*)(newData.Scan0.ToPointer());                    //高斯模板                    int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };                    for (int i = 1; i < Width - 1; i++)                    {                        for (int j = 1; j < Height - 1; j++)                        {                            int r = 0, g = 0, b = 0;                            int Index = 0;                            for (int col = -1; col <= 1; col++)                            {                                for (int row = -1; row <= 1; row++)                                {                                    int off = ((j + row) * (Width) + (i + col)) * 4;                                    r += pin[off + 0] * Gauss[Index];                                    g += pin[off + 1] * Gauss[Index];                                    b += pin[off + 2] * Gauss[Index];                                    Index++;                                }                            }                            r /= 16;                            g /= 16;                            b /= 16;                            //處理顏色值溢出                            if (r < 0) r = 0;                            if (r > 255) r = 255;                            if (g < 0) g = 0;                            if (g > 255) g = 255;                            if (b < 0) b = 0;                            if (b > 255) b = 255;                            int off2 = (j * Width + i) * 4;                            pout[off2 + 0] = (byte)r;                            pout[off2 + 1] = (byte)g;                            pout[off2 + 2] = (byte)b;                        }                    }                    bitmap.UnlockBits(newData);                    MyBitmap.UnlockBits(oldData);                    this.pbshowbox.Image = bitmap;                }            }            else            {                MessageBox.Show("請先打開一張圖片!");            }        }

三、銳化

突出顯示顏色值大的像素點。

        /// <summary>        /// 銳化實現(xiàn)方法,顯示數(shù)值最大像素點        /// </summary>        void Image_Sharpen()        {            if (this.pbshowbox.Image != null)            {                int Height = this.pbshowbox.Image.Height;                int Width = this.pbshowbox.Image.Width;                Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);                Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;                BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);                BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);                unsafe                {                    byte* pin = (byte*)(oldData.Scan0.ToPointer());                    byte* pout = (byte*)(newData.Scan0.ToPointer());                    //拉普拉斯模板                    int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };                    for (int i = 1; i < Width - 1; i++)                    {                        for (int j = 1; j < Height - 1; j++)                        {                            int r = 0, g = 0, b = 0;                            int Index = 0;                            for (int col = -1; col <= 1; col++)                            {                                for (int row = -1; row <= 1; row++)                                {                                    int off = ((j + row) * (Width) + (i + col)) * 4;                                    r += pin[off + 0] * Laplacian[Index];                                    g += pin[off + 1] * Laplacian[Index];                                    b += pin[off + 2] * Laplacian[Index];                                    Index++;                                }                            }                            if (r < 0) r = 0;                            if (r > 255) r = 255;                            if (g < 0) g = 0;                            if (g > 255) g = 255;                            if (b < 0) b = 0;                            if (b > 255) b = 255;                            int off2 = (j * Width + i) * 4;                            pout[off2 + 0] = (byte)r;                            pout[off2 + 1] = (byte)g;                            pout[off2 + 2] = (byte)b;                        }                    }                    bitmap.UnlockBits(newData);                    MyBitmap.UnlockBits(oldData);                    this.pbshowbox.Image = bitmap;                }            }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大理市| 祁东县| 中卫市| 舒兰市| 永年县| 安平县| 沿河| 商南县| 平凉市| 石柱| 郁南县| 湄潭县| 巴马| 宜丰县| 泸州市| 新宁县| 邛崃市| 大同市| 涞水县| 东安县| 新安县| 花莲县| 大兴区| 凌源市| 和政县| 浦江县| 金堂县| 东光县| 鄂托克前旗| 自治县| 北宁市| 沙坪坝区| 乌拉特后旗| 高平市| 育儿| 仲巴县| 体育| 汉阴县| 资源县| 永城市| 汉川市|