版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quá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;/標記圖像p
3、rivatedoublexScale=1d;/原始圖像與PictureBox在x軸方向上的縮放privatedoubleyScale=1d;/原始圖像與PictureBox在y軸方向上的縮放privatePointpreviousMouseLocation=newPoint(-1,-1);/上次繪制線條時,鼠標所處的位置privateconstintLineWidth=5;/繪制線條的寬度privateintdrawCount=1;/用戶繪制的線條數(shù)目,用于指定線條的顏色publicFormImageSegment()InitializeComponent();/窗體加載時privatevoi
4、dFormImageSegment_Load(objectsender,EventArgse)/設(shè)置提示toolTip.SetToolTip(rbWatershed,可以在源圖像上用鼠標繪制大致分割區(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();/當窗體關(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;/當鼠標按下并在源圖像上移動時,在源圖像上繪制分割線條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;/當松開鼠標左鍵時,將繪圖的前一位置設(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()/準備參數(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()/準備參數(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);/當改變金字塔分割的參數(shù)“金字塔層數(shù)”時,對參數(shù)進行校驗/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)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 外貿(mào)進出口2025年代理報關(guān)合同協(xié)議
- 2026年安徽郵電職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測試模擬試題有答案解析
- 2026年阜陽幼兒師范高等??茖W(xué)校單招綜合素質(zhì)考試備考試題帶答案解析
- 2026年邯鄲幼兒師范高等專科學(xué)校單招職業(yè)技能考試模擬試題帶答案解析
- 2026年河北科技學(xué)院單招綜合素質(zhì)考試模擬試題帶答案解析
- 體檢結(jié)果解讀合同(2025年服務(wù)條款)
- 2026年湖南理工職業(yè)技術(shù)學(xué)院單招綜合素質(zhì)考試備考題庫帶答案解析
- 2026年福建農(nóng)林大學(xué)金山學(xué)院單招綜合素質(zhì)考試備考題庫帶答案解析
- 2026年甘肅林業(yè)職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性考試備考試題帶答案解析
- 2026年廣西教育學(xué)院高職單招職業(yè)適應(yīng)性測試模擬試題有答案解析
- 2026年包頭職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測試參考題庫帶答案解析
- 2025年醫(yī)院檢驗科主任年終述職報告
- 2025-2026學(xué)年人教版(簡譜)(新教材)初中音樂七年級(上冊)期末測試卷附答案(共三套)
- 2025年大學(xué)(森林保護)森林病理學(xué)期末試題及答案
- (南開中學(xué))重慶市高2026屆高三第五次質(zhì)量檢測物理試卷(含答案詳解)
- 骨質(zhì)疏松骨折課件
- 2025寧夏賀蘭工業(yè)園區(qū)管委會招聘40人筆試備考試題及答案解析
- 糖尿病足病新進展課件
- 2025山西朔州市公安局招聘留置看護崗位輔警260人備考核心題庫及答案解析
- 中國臨床腫瘤學(xué)會(CSCO)癌癥診療指南(2025年版)
- 2025安徽滁州市公安機關(guān)第二批次招聘警務(wù)輔助人員50人考試筆試備考試題及答案解析
評論
0/150
提交評論