




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Good is good, but better carries it.精益求精,善益求善。opencv實現(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);/上次繪制線條時,鼠標(biāo)所處的位置privateconstintLineWidth=5;/繪制線條的寬度privateintdrawCount=1;/用戶繪制的線條數(shù)目,用于指定線條的顏色publicFormImageSegment()InitializeComponent();/窗體加載時privatevoi
4、dFormImageSegment_Load(objectsender,EventArgse)/設(shè)置提示toolTip.SetToolTip(rbWatershed,可以在源圖像上用鼠標(biāo)繪制大致分割區(qū)域線條,該線條用于分水嶺算法);toolTip.SetToolTip(txtPSLevel,金字塔層數(shù)跟圖像尺寸有關(guān),該值只能是圖像尺寸被2整除的次數(shù),否則將得出錯誤結(jié)果);toolTip.SetToolTip(txtPSThreshold1,建立連接的錯誤閥值);toolTip.SetToolTip(txtPSThreshold2,分割簇的錯誤閥值);toolTip.SetToolTip(txt
5、PMSFSpatialRadius,空間窗的半徑);toolTip.SetToolTip(txtPMSFColorRadius,色彩窗的半徑);toolTip.SetToolTip(btnClearMarkers,清除繪制在源圖像上,用于分水嶺算法的大致分割區(qū)域線條);/加載圖像LoadImage();/當(dāng)窗體關(guān)閉時,釋放資源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)按下并在源圖像上移動時,在源圖像上繪制分割線條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)松開鼠標(biāo)左鍵時,將繪圖的前一位置設(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();/分水嶺算法圖像分割/返回用時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級灰度圖像pbResult.Image=imageMarkers2.Bitmap;imageMarkers2.Dispose();returnstring.Format(分水嶺圖像分割,用時: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(金字塔分割,用時: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(均值漂移分割,用時:0:F05毫秒。rn,sw.Elapsed.TotalMilliseconds);/當(dāng)改變金字塔分割的參數(shù)“金字塔層數(shù)”時,對參數(shù)進(jì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. 本站所有資源如無特殊說明,都需要本地電腦安裝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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 青島幼兒師范高等??茖W(xué)?!冬F(xiàn)代漢語I》2023-2024學(xué)年第二學(xué)期期末試卷
- 慶陽職業(yè)技術(shù)學(xué)院《商務(wù)統(tǒng)計》2023-2024學(xué)年第二學(xué)期期末試卷
- 沈陽醫(yī)學(xué)院《應(yīng)用多元統(tǒng)計分析實訓(xùn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 嘉興南湖學(xué)院《歷史文獻(xiàn)檢索與論文寫作》2023-2024學(xué)年第二學(xué)期期末試卷
- 江西理工大學(xué)《材料科學(xué)基礎(chǔ)應(yīng)用研究》2023-2024學(xué)年第二學(xué)期期末試卷
- 沈陽師范大學(xué)《兒歌表演唱(二)》2023-2024學(xué)年第二學(xué)期期末試卷
- 麗江文化旅游學(xué)院《中外戲劇比較研究》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣東農(nóng)工商職業(yè)技術(shù)學(xué)院《電工與電子技術(shù)A》2023-2024學(xué)年第二學(xué)期期末試卷
- 新疆工程學(xué)院《核醫(yī)學(xué)E》2023-2024學(xué)年第二學(xué)期期末試卷
- 池州學(xué)院《跨文化研究》2023-2024學(xué)年第二學(xué)期期末試卷
- 2023年開展的課外讀物負(fù)面清單管理的具體措施
- 《國防動員實施》課件
- 上高雙胞胎弘安畜牧有限公司田心鎮(zhèn)現(xiàn)代化18萬出欄育肥場建設(shè)項目環(huán)評報告
- 《米酒的釀造過程》課件
- 2024手機攝影課ppt課件完整版
- 醫(yī)院班子成員考核方案
- 2024年九省聯(lián)考安徽省新高考?xì)v史試卷(含答案)
- 汽車維修保養(yǎng)協(xié)議書
- HG T 3690-2022 工業(yè)用鋼骨架聚乙烯塑料復(fù)合管
- 單色版畫課件
- 《現(xiàn)代教育技術(shù)》教案-第一章 教育技術(shù)概述
評論
0/150
提交評論