日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

C# 内存法图像处理

發(fā)布時(shí)間:2023/12/9 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 内存法图像处理 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

內(nèi)存法通過(guò)把圖像儲(chǔ)存在內(nèi)存中進(jìn)行處理,效率大大高于GetPixel方法,安全性高于指針?lè)ā?/p>

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

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

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

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

3.Stride:步幅,掃描寬度,形象的說(shuō)就是一行的長(zhǎng)度。

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

給出原圖:

?

一、灰度

對(duì)每個(gè)像素點(diǎn)進(jìn)行加權(quán)平均,(方法不唯一)。

/// <summary>/// 灰化實(shí)現(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);//加權(quán)平均實(shí)現(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("請(qǐng)先打開一張圖片!");}}

二、柔化

像素點(diǎn)與周圍像素點(diǎn)差別較大時(shí)取平均值。

/// <summary>/// 柔化實(shí)現(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("請(qǐng)先打開一張圖片!");}}

三、銳化

突出顯示顏色值大的像素點(diǎn)。

/// <summary>/// 銳化實(shí)現(xiàn)方法,顯示數(shù)值最大像素點(diǎn)/// </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;}}else{MessageBox.Show("請(qǐng)先打開一張圖片!");}}

四、浮雕

對(duì)圖像像素點(diǎn)的像素值分別與相鄰像素點(diǎn)的像素值相減后加上128,?然后將其作為新的像素點(diǎn)的值。

/// <summary>/// 浮雕實(shí)現(xiàn)方法/// </summary>void Image_Relief(){if (this.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_1 = (byte*)(oldData.Scan0.ToPointer());byte* pin_2 = pin_1 + (oldData.Stride); byte* pout = (byte*)(newData.Scan0.ToPointer());for (int y = 0; y < oldData.Height - 1; y++){for (int x = 0; x < oldData.Width; x++){ int b = (int)pin_1[0] - (int)pin_2[0] + 128;int g = (int)pin_1[1] - (int)pin_2[1] + 128;int r = (int)pin_1[2] - (int)pin_2[2] + 128;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;pout[0] = (byte)(b);pout[1] = (byte)(g);pout[2] = (byte)(r);pin_1 = pin_1 + 3;pin_2 = pin_2 + 3;pout = pout + 3; }pin_1 += oldData.Stride - oldData.Width * 3;pin_2 += oldData.Stride - oldData.Width * 3;pout += newData.Stride - newData.Width * 3;}bitmap.UnlockBits(newData);MyBitmap.UnlockBits(oldData);this.pbshowbox.Image = bitmap;}}else{MessageBox.Show("請(qǐng)先打開一張圖片!");}}

五、底片

顏色值取反。

/// <summary>/// 底片實(shí)現(xiàn)方法/// </summary>void Image_Negative(){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++){pout[0] = (byte)(255 - pin[0]);pout[1] = (byte)(255 - pin[1]);pout[2] = (byte)(255 - pin[2]);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("請(qǐng)先打開一張圖片!");}}

六、積木

低像素置0,高像素置255。

/// <summary>/// 積木實(shí)現(xiàn)方法/// </summary>private void Image_Block(){if (this.pbshowbox.Image != null){int Height = this.pbshowbox.Image.Height;int Width = this.pbshowbox.Image.Width;Bitmap bitmap = new Bitmap(Width, Height);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++){int avg = (pin[0] + pin[1] + pin[2]) / 3;if (avg > 128){pout[0] = 255;pout[1] = 255;pout[2] = 255;}else{pout[0] = 0;pout[1] = 0;pout[2] = 0;}pin = pin + 3;pout = pout + 3;}pin = pin + oldData.Stride - oldData.Width * 3;pout = pout + newData.Stride - newData.Width * 3;}bitmap.UnlockBits(newData);Mybitmap.UnlockBits(oldData);this.pbshowbox.Image = bitmap;}}else{MessageBox.Show("請(qǐng)先打開一張圖片!");}}

有些圖片效果看起來(lái)不明顯是因?yàn)楣P者把圖縮小了,其實(shí)效果挺明顯的- -。

?

轉(zhuǎn)載于:https://www.cnblogs.com/zjc0202/p/4398605.html

總結(jié)

以上是生活随笔為你收集整理的C# 内存法图像处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。