opencv實(shí)現(xiàn)分水嶺復(fù)習(xí)進(jìn)程_第1頁(yè)
opencv實(shí)現(xiàn)分水嶺復(fù)習(xí)進(jìn)程_第2頁(yè)
opencv實(shí)現(xiàn)分水嶺復(fù)習(xí)進(jìn)程_第3頁(yè)
opencv實(shí)現(xiàn)分水嶺復(fù)習(xí)進(jìn)程_第4頁(yè)
opencv實(shí)現(xiàn)分水嶺復(fù)習(xí)進(jìn)程_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Good is good, but better carries it.精益求精,善益求善。opencv實(shí)現(xiàn)分水嶺-usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;usingEmgu.CV;u

2、singEmgu.CV.CvEnum;usingEmgu.CV.Structure;usingEmgu.CV.UI;namespaceImageProcessLearnpublicpartialclassFormImageSegment:Form/成員變量privatestringsourceImageFileName=wky_tms_2272x1704.jpg;/源圖像文件名privateImageimageSource=null;/源圖像privateImageimageSourceClone=null;/源圖像的克隆privateImageimageMarkers=null;/標(biāo)記圖像p

3、rivatedoublexScale=1d;/原始圖像與PictureBox在x軸方向上的縮放privatedoubleyScale=1d;/原始圖像與PictureBox在y軸方向上的縮放privatePointpreviousMouseLocation=newPoint(-1,-1);/上次繪制線條時(shí),鼠標(biāo)所處的位置privateconstintLineWidth=5;/繪制線條的寬度privateintdrawCount=1;/用戶繪制的線條數(shù)目,用于指定線條的顏色publicFormImageSegment()InitializeComponent();/窗體加載時(shí)privatevoi

4、dFormImageSegment_Load(objectsender,EventArgse)/設(shè)置提示toolTip.SetToolTip(rbWatershed,可以在源圖像上用鼠標(biāo)繪制大致分割區(qū)域線條,該線條用于分水嶺算法);toolTip.SetToolTip(txtPSLevel,金字塔層數(shù)跟圖像尺寸有關(guān),該值只能是圖像尺寸被2整除的次數(shù),否則將得出錯(cuò)誤結(jié)果);toolTip.SetToolTip(txtPSThreshold1,建立連接的錯(cuò)誤閥值);toolTip.SetToolTip(txtPSThreshold2,分割簇的錯(cuò)誤閥值);toolTip.SetToolTip(txt

5、PMSFSpatialRadius,空間窗的半徑);toolTip.SetToolTip(txtPMSFColorRadius,色彩窗的半徑);toolTip.SetToolTip(btnClearMarkers,清除繪制在源圖像上,用于分水嶺算法的大致分割區(qū)域線條);/加載圖像LoadImage();/當(dāng)窗體關(guān)閉時(shí),釋放資源privatevoidFormImageSegment_FormClosing(objectsender,FormClosingEventArgse)if(imageSource!=null)imageSource.Dispose();if(imageSourceClon

6、e!=null)imageSourceClone.Dispose();if(imageMarkers!=null)imageMarkers.Dispose();/加載源圖像privatevoidbtnLoadImage_Click(objectsender,EventArgse)OpenFileDialogofd=newOpenFileDialog();ofd.CheckFileExists=true;ofd.DefaultExt=jpg;ofd.Filter=圖片文件|*.jpg;*.png;*.bmp|所有文件|*.*;if(ofd.ShowDialog(this)=DialogResul

7、t.OK)if(ofd.FileName!=)sourceImageFileName=ofd.FileName;LoadImage();ofd.Dispose();/清除分割線條privatevoidbtnClearMarkers_Click(objectsender,EventArgse)if(imageSourceClone!=null)imageSourceClone.Dispose();imageSourceClone=imageSource.Copy();pbSource.Image=imageSourceClone.Bitmap;imageMarkers.SetZero();dra

8、wCount=1;/當(dāng)鼠標(biāo)按下并在源圖像上移動(dòng)時(shí),在源圖像上繪制分割線條privatevoidpbSource_MouseMove(objectsender,MouseEventArgse)/如果按下了左鍵if(e.Button=MouseButtons.Left)if(previousMouseLocation.X=0&previousMouseLocation.Y=0)Pointp1=newPoint(int)(previousMouseLocation.X*xScale),(int)(previousMouseLocation.Y*yScale);Pointp2=newPoint(int

9、)(e.Location.X*xScale),(int)(e.Location.Y*yScale);LineSegment2Dls=newLineSegment2D(p1,p2);intthickness=(int)(LineWidth*xScale);imageSourceClone.Draw(ls,newBgr(255d,255d,255d),thickness);pbSource.Image=imageSourceClone.Bitmap;imageMarkers.Draw(ls,newGray(drawCount),thickness);previousMouseLocation=e.

10、Location;/當(dāng)松開(kāi)鼠標(biāo)左鍵時(shí),將繪圖的前一位置設(shè)置為(-1,-1)privatevoidpbSource_MouseUp(objectsender,MouseEventArgse)previousMouseLocation=newPoint(-1,-1);drawCount+;/加載源圖像privatevoidLoadImage()if(imageSource!=null)imageSource.Dispose();imageSource=newImage(sourceImageFileName);if(imageSourceClone!=null)imageSourceClone.D

11、ispose();imageSourceClone=imageSource.Copy();pbSource.Image=imageSourceClone.Bitmap;if(imageMarkers!=null)imageMarkers.Dispose();imageMarkers=newImage(imageSource.Size);imageMarkers.SetZero();xScale=1d*imageSource.Width/pbSource.Width;yScale=1d*imageSource.Height/pbSource.Height;drawCount=1;/分割圖像pri

12、vatevoidbtnImageSegment_Click(objectsender,EventArgse)if(rbWatershed.Checked)txtResult.Text+=Watershed();elseif(rbPrySegmentation.Checked)txtResult.Text+=PrySegmentation();elseif(rbPryMeanShiftFiltering.Checked)txtResult.Text+=PryMeanShiftFiltering();/分水嶺算法圖像分割/返回用時(shí)privatestringWatershed()/分水嶺算法分割I(lǐng)m

13、ageimageMarkers2=imageMarkers.Copy();Stopwatchsw=newStopwatch();sw.Start();CvInvoke.cvWatershed(imageSource.Ptr,imageMarkers2.Ptr);sw.Stop();/將分割的結(jié)果轉(zhuǎn)換到256級(jí)灰度圖像pbResult.Image=imageMarkers2.Bitmap;imageMarkers2.Dispose();returnstring.Format(分水嶺圖像分割,用時(shí):0:F05毫秒。rn,sw.Elapsed.TotalMilliseconds);/金字塔分割算法/

14、privatestringPrySegmentation()/準(zhǔn)備參數(shù)ImageimageDest=newImage(imageSource.Size);MemStoragestorage=newMemStorage();IntPtrptrComp=IntPtr.Zero;intlevel=int.Parse(txtPSLevel.Text);doublethreshold1=double.Parse(txtPSThreshold1.Text);doublethreshold2=double.Parse(txtPSThreshold2.Text);/金字塔分割Stopwatchsw=newSt

15、opwatch();sw.Start();CvInvoke.cvPyrSegmentation(imageSource.Ptr,imageDest.Ptr,storage.Ptr,outptrComp,level,threshold1,threshold2);sw.Stop();/顯示結(jié)果pbResult.Image=imageDest.Bitmap;/釋放資源imageDest.Dispose();storage.Dispose();returnstring.Format(金字塔分割,用時(shí):0:F05毫秒。rn,sw.Elapsed.TotalMilliseconds);/均值漂移分割算法/

16、privatestringPryMeanShiftFiltering()/準(zhǔn)備參數(shù)ImageimageDest=newImage(imageSource.Size);doublespatialRadius=double.Parse(txtPMSFSpatialRadius.Text);doublecolorRadius=double.Parse(txtPMSFColorRadius.Text);intmaxLevel=int.Parse(txtPMSFNaxLevel.Text);intmaxIter=int.Parse(txtPMSFMaxIter.Text);doubleepsilon=d

17、ouble.Parse(txtPMSFEpsilon.Text);MCvTermCriteriatermcrit=newMCvTermCriteria(maxIter,epsilon);/均值漂移分割Stopwatchsw=newStopwatch();sw.Start();OpenCvInvoke.cvPyrMeanShiftFiltering(imageSource.Ptr,imageDest.Ptr,spatialRadius,colorRadius,maxLevel,termcrit);sw.Stop();/顯示結(jié)果pbResult.Image=imageDest.Bitmap;/釋放

18、資源imageDest.Dispose();returnstring.Format(均值漂移分割,用時(shí):0:F05毫秒。rn,sw.Elapsed.TotalMilliseconds);/當(dāng)改變金字塔分割的參數(shù)“金字塔層數(shù)”時(shí),對(duì)參數(shù)進(jìn)行校驗(yàn)/privatevoidtxtPSLevel_TextChanged(objectsender,EventArgse)intlevel=int.Parse(txtPSLevel.Text);if(level1|imageSource.Width%(int)(Math.Pow(2,level-1)!=0|imageSource.Height%(int)(Mat

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論