![c#圖片各種處理旋轉(zhuǎn)-裁剪-分辨率調(diào)整_第1頁](http://file4.renrendoc.com/view/09e1fe12fc462a0cbae160c9de47177c/09e1fe12fc462a0cbae160c9de47177c1.gif)
![c#圖片各種處理旋轉(zhuǎn)-裁剪-分辨率調(diào)整_第2頁](http://file4.renrendoc.com/view/09e1fe12fc462a0cbae160c9de47177c/09e1fe12fc462a0cbae160c9de47177c2.gif)
![c#圖片各種處理旋轉(zhuǎn)-裁剪-分辨率調(diào)整_第3頁](http://file4.renrendoc.com/view/09e1fe12fc462a0cbae160c9de47177c/09e1fe12fc462a0cbae160c9de47177c3.gif)
![c#圖片各種處理旋轉(zhuǎn)-裁剪-分辨率調(diào)整_第4頁](http://file4.renrendoc.com/view/09e1fe12fc462a0cbae160c9de47177c/09e1fe12fc462a0cbae160c9de47177c4.gif)
![c#圖片各種處理旋轉(zhuǎn)-裁剪-分辨率調(diào)整_第5頁](http://file4.renrendoc.com/view/09e1fe12fc462a0cbae160c9de47177c/09e1fe12fc462a0cbae160c9de47177c5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
PAGEPAGE25一、各種旋轉(zhuǎn)、改變大小注意:先要添加畫圖相關(guān)的using引用。//向右旋轉(zhuǎn)圖像90°代碼如下:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap("rama.jpg");//加載圖像g.FillRectangle(Brushes.White,this.ClientRectangle);//填充窗體背景為白色Point[]destinationPoints={newPoint(100,0),//destinationforupper-leftpointoforiginalnewPoint(100,100),//destinationforupper-rightpointoforiginalnewPoint(0,0)};//destinationforlower-leftpointoforiginalg.DrawImage(bmp,destinationPoints);}//旋轉(zhuǎn)圖像180°代碼如下:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap("rama.jpg");g.FillRectangle(Brushes.White,this.ClientRectangle);Point[]destinationPoints={newPoint(0,100),//destinationforupper-leftpointoforiginalnewPoint(100,100),//destinationforupper-rightpointoforiginalnewPoint(0,0)};//destinationforlower-leftpointoforiginalg.DrawImage(bmp,destinationPoints);}//圖像切變代碼:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap("rama.jpg");g.FillRectangle(Brushes.White,this.ClientRectangle);Point[]destinationPoints={newPoint(0,0),//destinationforupper-leftpointoforiginalnewPoint(100,0),//destinationforupper-rightpointoforiginalnewPoint(50,100)};//destinationforlower-leftpointoforiginalg.DrawImage(bmp,destinationPoints);}//圖像截取:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap("rama.jpg");g.FillRectangle(Brushes.White,this.ClientRectangle);Rectanglesr=newRectangle(80,60,400,400);//要截取的矩形區(qū)域Rectangledr=newRectangle(0,0,200,200);//要顯示到Form的矩形區(qū)域g.DrawImage(bmp,dr,sr,GraphicsUnit.Pixel);}//改變圖像大小:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap("rama.jpg");g.FillRectangle(Brushes.White,this.ClientRectangle);intwidth=bmp.Width;intheight=bmp.Height;//改變圖像大小使用低質(zhì)量的模式g.InterpolationMode=InterpolationMode.NearestNeighbor;g.DrawImage(bmp,newRectangle(10,10,120,120),//sourcerectanglenewRectangle(0,0,width,height),//destinationrectangleGraphicsUnit.Pixel);//使用高質(zhì)量模式//g.CompositingQuality=CompositingQuality.HighSpeed;g.InterpolationMode=InterpolationMode.HighQualityBicubic;g.DrawImage(bmp,newRectangle(130,10,120,120),newRectangle(0,0,width,height),GraphicsUnit.Pixel);}//設(shè)置圖像的分辯率:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap("rama.jpg");g.FillRectangle(Brushes.White,this.ClientRectangle);bmp.SetResolution(300f,300f);g.DrawImage(bmp,0,0);bmp.SetResolution(1200f,1200f);g.DrawImage(bmp,180,0);}//用GDI+畫圖privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){GraphicsgForm=e.Graphics;gForm.FillRectangle(Brushes.White,this.ClientRectangle);for(inti=1;i<=7;++i){//在窗體上面畫出橙色的矩形Rectangler=newRectangle(i*40-15,0,15,this.ClientRectangle.Height);gForm.FillRectangle(Brushes.Orange,r);}//在內(nèi)存中創(chuàng)建一個Bitmap并設(shè)置CompositingModeBitmapbmp=newBitmap(260,260,System.Drawing.Imaging.PixelFormat.Format32bppArgb);GraphicsgBmp=Graphics.FromImage(bmp);gBmp.CompositingMode=System.Drawing.Drawing2D.CompositingMode.SourceCopy;//創(chuàng)建一個帶有Alpha的紅色區(qū)域//并將其畫在內(nèi)存的位圖里面Colorred=Color.FromArgb(0x60,0xff,0,0);BrushredBrush=newSolidBrush(red);gBmp.FillEllipse(redBrush,70,70,160,160);//創(chuàng)建一個帶有Alpha的綠色區(qū)域Colorgreen=Color.FromArgb(0x40,0,0xff,0);BrushgreenBrush=newSolidBrush(green);gBmp.FillRectangle(greenBrush,10,10,140,140);//在窗體上面畫出位圖nowdrawthebitmaponourwindowgForm.DrawImage(bmp,20,20,bmp.Width,bmp.Height);//清理資源bmp.Dispose();gBmp.Dispose();redBrush.Dispose();greenBrush.Dispose();}//在窗體上面繪圖并顯示圖像privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;PenblackPen=newPen(Color.Black,1);if(ClientRectangle.Height/10>0){for(inty=0;y<ClientRectangle.Height;y+=ClientRectangle.Height/10){g.DrawLine(blackPen,newPoint(0,0),newPoint(ClientRectangle.Width,y));}}blackPen.Dispose();}C#使用Bitmap類進(jìn)行圖片裁剪在Mapwin(手機(jī)游戲地圖編輯器)生成的地圖txt文件中添加自己需要處理的數(shù)據(jù)后轉(zhuǎn)換成可在手機(jī)(Ophone)開發(fā)環(huán)境中使用的字節(jié)流地圖文件的小工具,其中就涉及到圖片的裁剪和生成了。有以下幾種方式。方法一:拷貝像素。當(dāng)然這種方法是最笨的,效率也就低了些。在Bitmap類中我們可以看到這樣兩個方法:GetPixel(intx,inty)和SetPixel(intx,inty,Colorcolor)方法。從字面的含以上就知道前者是獲取圖像某點(diǎn)像素值,是用Color對象返回的;后者是將已知像素描畫到制定的位置。下面就來做個實例檢驗下:1.首先創(chuàng)建一個WindowsForm窗體程序,往該窗體上拖放7個PictureBox控件,第一個用于放置并顯示原始的大圖片,其后6個用于放置并顯示裁剪后新生成的6個小圖;2.放置原始大圖的PictureBox控件name屬性命名為pictureBoxBmpRes,其后pictureBox1到pictureBox6依次命名,并放置在合適的位置;3.雙擊Form窗體,然后在Form1_Load事件中加入下面的代碼即可。//導(dǎo)入圖像資源BitmapbmpRes=null;StringstrPath=Application.ExecutablePath;try{intnEndIndex=strPath.LastIndexOf('//');strPath=strPath.Substring(0,nEndIndex)+"http://Bmp//BmpResMM.bmp";bmpRes=newBitmap(strPath);//窗體上顯示加載圖片pictureBoxBmpRes.Width=bmpRes.Width;pictureBoxBmpRes.Height=bmpRes.Height;pictureBoxBmpRes.Image=bmpRes;}catch(Exceptionex){System.Windows.Forms.MessageBox.Show("圖片資源加載失??!/r/n"+ex.ToString());}//裁剪圖片(裁成2行3列的6張圖片)intnYClipNum=2,nXClipNum=3;Bitmap[]bmpaClipBmpArr=newBitmap[nYClipNum*nXClipNum];for(intnYClipNumIndex=0;nYClipNumIndex<nYClipNum;nYClipNumIndex++){for(intnXClipNumIndex=0;nXClipNumIndex<nXClipNum;nXClipNumIndex++){intnClipWidth=bmpRes.Width/nXClipNum;intnClipHight=bmpRes.Height/nYClipNum;intnBmpIndex=nXClipNumIndex+nYClipNumIndex*nYClipNum+(nYClipNumIndex>0?1:0);bmpaClipBmpArr[nBmpIndex]=newBitmap(nClipWidth,nClipHight);for(intnY=0;nY<nClipHight;nY++){for(intnX=0;nX<nClipWidth;nX++){intnClipX=nX+nClipWidth*nXClipNumIndex;intnClipY=nY+nClipHight*nYClipNumIndex;ColorcClipPixel=bmpRes.GetPixel(nClipX,nClipY);bmpaClipBmpArr[nBmpIndex].SetPixel(nX,nY,cClipPixel);}}}}PictureBox[]picbShow=newPictureBox[nYClipNum*nXClipNum];picbShow[0]=pictureBox1;picbShow[1]=pictureBox2;picbShow[2]=pictureBox3;picbShow[3]=pictureBox4;picbShow[4]=pictureBox5;picbShow[5]=pictureBox6;for(intnLoop=0;nLoop<nYClipNum*nXClipNum;nLoop++){picbShow[nLoop].Width=bmpRes.Width/nXClipNum;picbShow[nLoop].Height=bmpRes.Height/nYClipNum;picbShow[nLoop].Image=bmpaClipBmpArr[nLoop];}現(xiàn)在看看那些地方需要注意的了。其中intnBmpIndex=nXClipNumIndex+nYClipNumIndex*nYClipNum+(nYClipNumIndex>0?1:0);這句定義了存儲裁剪圖片對象在數(shù)組中的索引,需要注意的就是后面的(nYClipNumIndex>0?1:0)——因為只有當(dāng)裁剪的對象處于第一行以外的行時需要將索引加1;另外,因為這種方法的效率不高,程序運(yùn)行起來還是頓了下。如果有興趣的話,可以將以上的代碼放到一個按鈕Click事件函數(shù)中,當(dāng)單擊該按鈕時就可以感覺到了。方法二:運(yùn)用Clone函數(shù)局部復(fù)制。同樣在Bitmap中可以找到Clone()方法,該方法有三個重載方法。Clone(),Clone(Rectangle,PixelFormat)和Clone(RectangleF,PixelFormat)。第一個方法將創(chuàng)建并返回一個精確的實例對象,后兩個就是我們這里需要用的局部裁剪了(其實后兩個方法本人覺得用法上差不多)。將上面的程序稍稍改進(jìn)下——將裁剪的處理放到一個按鈕事件函數(shù)中,然后再托一個按鈕好窗體上,最后將下面的代碼復(fù)制到該按鈕的事件函數(shù)中。for(intnYClipNumIndex=0;nYClipNumIndex<nYClipNum;nYClipNumIndex++){for(intnXClipNumIndex=0;nXClipNumIndex<nXClipNum;nXClipNumIndex++){intnClipWidth=bmpRes.Width/nXClipNum;intnClipHight=bmpRes.Height/nYClipNum;intnBmpIndex=nXClipNumIndex+nYClipNumIndex*nYClipNum+(nYClipNumIndex>0?1:0);RectanglerClipRect=newRectangle(nClipWidth*nXClipNumIndex,nClipHight*nYClipNumIndex,nClipWidth,nClipHight);bmpaClipBmpArr[nBmpIndex]=bmpRes.Clone(rClipRect,bmpRes.PixelFormat);}}運(yùn)行程序,單擊按鈕檢驗下,發(fā)現(xiàn)速度明顯快可很多。其實這種方法較第一中方法不同的地方僅只是變換了for循環(huán)中的拷貝部分的處理,RectanglerClipRect=newRectangle(nClipWidth*nXClipNumIndex,nClipHight*nYClipNumIndex,nClipWidth,nClipHight);bmpaClipBmpArr[nBmpIndex]=bmpRes.Clone(rClipRect,bmpRes.PixelFormat);一.底片效果原理:GetPixel方法獲得每一點(diǎn)像素的值,然后再使用SetPixel方法將取反后的顏色值設(shè)置到對應(yīng)的點(diǎn).效果圖:代碼實現(xiàn):privatevoidbutton1_Click(objectsender,EventArgse){//以底片效果顯示圖像try{intHeight=this.pictureBox1.Image.Height;intWidth=this.pictureBox1.Image.Width;Bitmapnewbitmap=newBitmap(Width,Height);Bitmapoldbitmap=(Bitmap)this.pictureBox1.Image;Colorpixel;for(intx=1;x<Width;x++){for(inty=1;y<Height;y++){intr,g,b;pixel=oldbitmap.GetPixel(x,y);r=255-pixel.R;g=255-pixel.G;b=255-pixel.B;newbitmap.SetPixel(x,y,Color.FromArgb(r,g,b));}}this.pictureBox1.Image=newbitmap;}catch(Exceptionex){MessageBox.Show(ex.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);}}二.浮雕效果原理:對圖像像素點(diǎn)的像素值分別與相鄰像素點(diǎn)的像素值相減后加上128,然后將其作為新的像素點(diǎn)的值.效果圖:代碼實現(xiàn):privatevoidbutton1_Click(objectsender,EventArgse){//以浮雕效果顯示圖像try{intHeight=this.pictureBox1.Image.Height;intWidth=this.pictureBox1.Image.Width;BitmapnewBitmap=newBitmap(Width,Height);BitmapoldBitmap=(Bitmap)this.pictureBox1.Image;Colorpixel1,pixel2;for(intx=0;x<Width-1;x++){for(inty=0;y<Height-1;y++){intr=0,g=0,b=0;pixel1=oldBitmap.GetPixel(x,y);pixel2=oldBitmap.GetPixel(x+1,y+1);r=Math.Abs(pixel1.R-pixel2.R+128);g=Math.Abs(pixel1.G-pixel2.G+128);b=Math.Abs(pixel1.B-pixel2.B+128);if(r>255)r=255;if(r<0)r=0;if(g>255)g=255;if(g<0)g=0;if(b>255)b=255;if(b<0)b=0;newBitmap.SetPixel(x,y,Color.FromArgb(r,g,b));}}this.pictureBox1.Image=newBitmap;}catch(Exceptionex){MessageBox.Show(ex.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);}}三.黑白效果原理:彩色圖像處理成黑白效果通常有3種算法;(1).最大值法:使每個像素點(diǎn)的R,G,B值等于原像素點(diǎn)的RGB(顏色值)中最大的一個;(2).平均值法:使用每個像素點(diǎn)的R,G,B值等于原像素點(diǎn)的RGB值的平均值;(3).加權(quán)平均值法:對每個像素點(diǎn)的R,G,B值進(jìn)行加權(quán)自認(rèn)為第三種方法做出來的黑白效果圖像最"真實".效果圖:代碼實現(xiàn):privatevoidbutton1_Click(objectsender,EventArgse){//以黑白效果顯示圖像try{intHeight=this.pictureBox1.Image.Height;intWidth=this.pictureBox1.Image.Width;BitmapnewBitmap=newBitmap(Width,Height);BitmapoldBitmap=(Bitmap)this.pictureBox1.Image;Colorpixel;for(intx=0;x<Width;x++)for(inty=0;y<Height;y++){pixel=oldBitmap.GetPixel(x,y);intr,g,b,Result=0;r=pixel.R;g=pixel.G;b=pixel.B;//實例程序以加權(quán)平均值法產(chǎn)生黑白圖像intiType=2;switch(iType){case0://平均值法Result=((r+g+b)/3);break;case1://最大值法Result=r>g?r:g;Result=Result>b?Result:b;break;case2://加權(quán)平均值法Result=((int)(0.7*r)+(int)(0.2*g)+(int)(0.1*b));break;}newBitmap.SetPixel(x,y,Color.FromArgb(Result,Result,Result));}this.pictureBox1.Image=newBitmap;}catch(Exceptionex){MessageBox.Show(ex.Message,"信息提示");}}四.柔化效果原理:當(dāng)前像素點(diǎn)與周圍像素點(diǎn)的顏色差距較大時取其平均值.效果圖:代碼實現(xiàn):privatevoidbutton1_Click(objectsender,EventArgse){//以柔化效果顯示圖像try{intHeight=this.pictureBox1.Image.Height;intWidth=this.pictureBox1.Image.Width;Bitmapbitmap=newBitmap(Width,Height);BitmapMyBitmap=(Bitmap)this.pictureBox1.Image;Colorpixel;//高斯模板int[]Gauss={1,2,1,2,4,2,1,2,1};for(intx=1;x<Width-1;x++)for(inty=1;y<Height-1;y++){intr=0,g=0,b=0;intIndex=0;for(intcol=-1;col<=1;col++)for(introw=-1;row<=1;row++){pixel=MyBitmap.GetPixel(x+row,y+col);r+=pixel.R*Gauss[Index];g+=pixel.G*Gauss[Index];b+=pixel.B*Gauss[Index];Index++;}r/=16;g/=16;b/=16;//處理顏色值溢出r=r>255?255:r;r=r<0?0:r;g=g>255?255:g;g=g<0?0:g;b=b>255?255:b;b=b<0?0:b;bitmap.SetPixel(x-1,y-1,Color.FromArgb(r,g,b));}this.pictureBox1.Image=bitmap;}catch(Exceptionex){MessageBox.Show(ex.Message,"信息提示");}}五.銳化效果原理:突出顯示顏色值大(即形成形體邊緣)的像素點(diǎn).效果圖:實現(xiàn)代碼:privatevoidbutton1_Click(objectsender,EventArgse){//以銳化效果顯示圖像try{intHeight=this.pictureBox1.Image.Height;intWidth=this.pictureBox1.Image.Width;BitmapnewBitmap=newBitmap(Width,Height);BitmapoldBitmap=(Bitmap)this.pictureBox1.Image;Colorpixel;//拉普拉斯模板int[]Laplacian={-1,-1,-1,-1,9,-1,-1,-1,-1};for(intx=1;x<Width-1;x++)for(inty=1;y<Height-1;y++){intr=0,g=0,b=0;intIndex=0;for(intcol=-1;col<=1;col++)for(introw=-1;row<=1;row++){pixel=oldBitmap.GetPixel(x+row,y+col);r+=pixel.R*Laplacian[Index];g+=pixel.G*Laplacian[Index];b+=pixel.B*Laplacian[Index];Index++;}//處理顏色值溢出r=r>255?255:r;r=r<0?0:r;g=g>255?255:g;g=g<0?0:g;b=b>255?255:b;b=b<0?0:b;newBitmap.SetPixel(x-1,y-1,Color.FromArgb(r,g,b));}this.pictureBox1.Image=newBitmap;}catch(Exceptionex){MessageBox.Show(ex.Message,"信息提示");}}六.霧化效果原理:在圖像中引入一定的隨機(jī)值,打亂圖像中的像素值效果圖:實現(xiàn)代碼:privatevoidbutton1_Click(objectsender,EventArgse){//以霧化效果顯示圖像try{intHeight=this.pictureBox1.Image.Height;intWidth=this.pictureBox1.Image.Width;BitmapnewBitmap=newBitmap(Width,Height);BitmapoldBitmap=(Bitmap)this.pictureBox1.Image;Colorpixel;for(intx=1;x<Width-1;x++)for(inty=1;y<Height-1;y++){System.RandomMyRandom=newRandom();intk=MyRandom.Next(123456);//像素塊大小intdx=x+k%19;intdy=y+k%19;if(dx>=Width)dx=Width-1;if(dy>=Height)dy=Height-1;pixel=oldBitmap.GetPixel(dx,dy);newBitmap.SetPixel(x,y,pixel);}this.pictureBox1.Image=newBitmap;}catch(Exceptionex){MessageBox.Show(ex.Message,"信息提示");}}淺談VisualC#進(jìn)行圖像處理作者:彭軍這里之所以說“淺談”是因為我這里只是簡單的介紹如何使用VisualC#進(jìn)行圖像的讀入、保存以及對像素的訪問。而不涉及太多的算法。一、讀入圖像在VisualC#中我們可以使用一個PictureBox控件來顯示圖片,如下:privatevoidbtnOpenImage_Click(objectsender,EventArgse){OpenFileDialogofd=newOpenFileDialog();ofd.Filter="BMPFiles(*.bmp)|*.bmp|JPGFiles(*.jpg;*.jpeg)|*.jpg;*.jpeg|AllFiles(*.*)|*.*";ofd.CheckFileExists=true;ofd.CheckPathExists=true;if(ofd.ShowDialog()==DialogResult.OK){//pbxShowImage.ImageLocation=ofd.FileName;bmp=newBitmap(ofd.FileName);if(bmp==null){MessageBox.Show("加載圖片失敗!","錯誤");return;}pbxShowImage.Image=bmp;ofd.Dispose();}}其中bmp為類的一個對象:privateBitmapbmp=null;在使用Bitmap類和BitmapData類之前,需要使用usingSystem.Drawing.Imaging;二、保存圖像privatevoidbtnSaveImage_Click(objectsender,EventArgse){if(bmp==null)return;SaveFileDialogsfd=newSaveFileDialog();sfd.Filter="BMPFiles(*.bmp)|*.bmp|JPGFiles(*.jpg;*.jpeg)|*.jpg;*.jpeg|AllFiles(*.*)|*.*";if(sfd.ShowDialog()==DialogResult.OK){pbxShowImage.Image.Save(sfd.FileName);MessageBox.Show("保存成功!","提示");sfd.Dispose();}}三、對像素的訪問我們可以來建立一個GrayBitmapData類來做相關(guān)的處理。整個類的程序如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Windows.Forms;namespaceImageElf{classGrayBitmapData{publicbyte[,]Data;//保存像素矩陣publicintWidth;//圖像的寬度publicintHeight;//圖像的高度publicGrayBitmapData(){this.Width=0;this.Height=0;this.Data=null;}publicGrayBitmapData(Bitmapbmp){BitmapDatabmpData=bmp.LockBits(newRectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);this.Width=bmpData.Width;this.Height=bmpData.Height;Data=newbyte[Height,Width];unsafe{byte*ptr=(byte*)bmpData.Scan0.ToPointer();for(inti=0;i<Height;i++){for(intj=0;j<Width;j++){//將24位的RGB彩色圖轉(zhuǎn)換為灰度圖inttemp=(int)(0.114*(*ptr++))+(int)(0.587*(*ptr++))+(int)(0.299*(*ptr++));Data[i,j]=(byte)temp;}ptr+=bmpData.Stride-Width*3;//指針加上填充的空白空間}}bmp.UnlockBits(bmpData);}publicGrayBitmapData(stringpath):this(newBitmap(path)){}publicBitmapToBitmap(){Bitmapbmp=newBitmap(Width,Height,PixelFormat.Format24bppRgb);BitmapDatabmpData=bmp.LockBits(newRectangle(0,0,Width,Height),ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);unsafe{byte*ptr=(byte*)bmpData.Scan0.ToPointer();for(inti=0;i<Height;i++){for(intj=0;j<Width;j++){*(ptr++)=Data[i,j];*(ptr++)=Data[i,j];*(ptr++)=Data[i,j];}ptr+=bmpData.Stride-Width*3;}}bmp.UnlockBits(bmpData);returnbmp;}publicvoidShowImage(PictureBoxpbx){Bitmapb=this.ToBitmap();pbx.Image=b;//b.Dispose();}publicvoidSaveImage(stringpath){Bitmapb=ToBitmap();b.Save(path);//b.Dispose();}//均值濾波publicvoidAverageFilter(intwindowSize){if(windowSize%2==0){return;}for(inti=0;i<Height;i++){for(intj=0;j<Width;j++){intsum=0;for(intg=-(windowSize-1)/2;g<=(windowSize-1)/2;g++){for(intk=-(windowSize-1)/2;k<=(windowSize-1)/2;k++){inta=i+g,b=j+k;if(a<0)a=0;if(a>Height-1)a=Height-1;if(b<0)b=0;if(b>Width-1)b=Width-1;sum+=Data[a,b];}}Data[i,j]=(byte)(sum/(windowSize*windowSize));}}}//中值濾波publicvoidMidFilter(intwindowSize){if(windowSize%2==0){return;}int[]temp=newint[windowSize*windowSize];byte[,]newdata=newbyte[Height,Width];for(inti=0;i<Height;i++){for(intj=0;j<Width;j++){intn=0;for(intg=-(windowSize-1)/2;g<=(windowSize-1)/2;g++){for(intk=-(windowSize-1)/2;k<=(windowSize-1)/2;k++){inta=i+g,b=j+k;if(a<0)a=0;if(a>Height-1)a=Height-1;if(b<0)b=0;if(b>Width-1)b=Width-1;temp[n++]=Data[a,b];}}newdata[i,j]=GetMidValue(temp,windowSize*windowSize);}}for(inti=0;i<Height;i++){for(intj=0;j<Width;j++){Data[i,j]=newdata[i,j];}}}//獲得一個向量的中值privatebyteGetMidValue(int[]t,intlength){inttemp=0;for(inti=0;i<length-2;i++){for(intj=i+1;j<length-1;j++){if(t[i]>t[j]){temp=t[i];t[i]=t[j];t[j]=temp;}}}return(byte)t[(length-1)/2];}//一種新的濾波方法,是亮的更亮、暗的更暗publicvoidNewFilter(intwindowSize){if(windowSize%2==0){return;}for(inti=0;i<Height;i++){for(intj=0;j<Width;j++){intsum=0;for(intg=-(windowSize-1)/2;g<=(windowSize-1)/2;g++){for(intk=-(windowSize-1)/2;k<=(windowSize-1)/2;k++){inta=i+g,b=j+k;if(a<0)a=0;if(a>Height-1)a=Height-1;if(b<0)b=0;if(b>Width-1)b=Width-1;sum+=Data[a,b];}}doubleavg=(sum+0.0)/(windowSize*windowSize);if(avg/255<0.5){Data[i,j]=(byte)(2*avg/255*Data[i,j]);}else{Data[i,j]=(byte)((1-2*(1-avg/255.0)*(1-Data[i,j]/255.0))*255);}}}}//直方圖均衡publicvoidHistEqual(){double[]num=newdouble[25
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年金剛石膜-聲表面波器件(SAW)項目規(guī)劃申請報告模板
- 2025年絕緣材料:絕緣套管項目提案報告模范
- 2025年個體經(jīng)營物流配送協(xié)議
- 2025年耐高溫可加工陶瓷項目立項申請報告
- 2025年發(fā)泡消泡劑項目規(guī)劃申請報告
- 2025年授權(quán)代理業(yè)務(wù)綜合合同范本
- 2025年建筑器材租賃合同標(biāo)桿
- 2025年倉儲物流服務(wù)合作協(xié)議合同
- 2025年工業(yè)外包合同中的環(huán)境管理措施
- 2025年城市綠化養(yǎng)護(hù)服務(wù)合同文本
- 10以內(nèi)除法口算練習(xí)題100套(十)
- 中醫(yī)小兒常見皮膚病
- 《醫(yī)療機(jī)構(gòu)環(huán)境表面清潔與消毒管理規(guī)范》-華西醫(yī)院案例
- 第45屆世界技能大賽餐廳服務(wù)項目全國選拔賽技術(shù)工作文件
- 個人保證無糾紛承諾保證書
- DB51T10009-2024DB50T10009-2024康養(yǎng)度假氣候類型劃分
- 九年級道德與法治下冊時政熱點(diǎn)專題新人教版
- 【壓縮式落葉清掃機(jī)設(shè)計(論文)6900字】
- 生產(chǎn)安全重大事故隱患檢查表(根據(jù)住建部房屋市政工程生產(chǎn)安全重大事故隱患判定標(biāo)準(zhǔn)(2022版)編制)
- 教育管理學(xué)教程褚宏啟課后問題及補(bǔ)充完整版
- 水利水電工程工地試驗室建設(shè)導(dǎo)則(征求意見稿)
評論
0/150
提交評論