opencv實(shí)現(xiàn)分水嶺,金字塔,均值漂移算法進(jìn)行分割_第1頁
opencv實(shí)現(xiàn)分水嶺,金字塔,均值漂移算法進(jìn)行分割_第2頁
opencv實(shí)現(xiàn)分水嶺,金字塔,均值漂移算法進(jìn)行分割_第3頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

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

文檔簡(jiǎn)介

1、using system;using system.collections.generic; using system.componentmodel; using system.data;using system.drawing; using system.linq; using system.text;using system.windows.forms; using system.diagnostics;using system.runtime.interopservices; using emgu.cv;using emgu.cv.cvenum; using emgu.cv.struct

2、ure; using emgu.cv.ui;namespaceimageprocesslearnpublic partialclass formimagesegment : form/ 成員變量privateimage<bgr, byte> imagesourceclone=null ;/ 源圖像的克隆privateimage<gray, int32> imagemarkers =null ;/ 標(biāo)記圖像privatedouble xscale = 1d;/ 原始圖像與 picturebox 在 x 軸方向上的縮放privatedouble yscale = 1d;/

3、原始圖像與 picturebox 在 y 軸方向上的縮放privatepoint previousmouselocation =newpoint(-1, - 1 );/ 上次繪制線條時(shí),鼠標(biāo)所處的位置privateconst int linewidth =5;/繪制線條的寬度privatestring sourceimagefilename =“wky_tms_2272x1704.jpg“;/ 源圖像文件名private image<bgr, byte> imagesource =null ;/ 源圖像privateint drawcount =1 ;/ 用戶繪制的線條數(shù)目,用于指

4、定線條的顏色public formimagesegment()initializecomponent();/ 窗體加載時(shí)privatevoid formimagesegment_load(object sender, eventargs e)法“);/ 設(shè)置提示tooltip.settooltip(rbwatershed,“可以在源圖像上用鼠標(biāo)繪制大致分割區(qū)域線條,該線條用于分水嶺算tooltip.settooltip(txtpslevel,“金字塔層數(shù)跟圖像尺寸有關(guān),該值只能是圖像尺寸被 2 整除的次數(shù),否則將得出錯(cuò)誤結(jié)果“);tooltip.settooltip(txtpsthreshol

5、d1,“建立連接的錯(cuò)誤閥值“);tooltip.settooltip(txtpsthreshold2,“分割簇的錯(cuò)誤閥值“); tooltip.settooltip(txtpmsfspatialradius,“空間窗的半徑“); tooltip.settooltip(txtpmsfcolorradius,“顏色窗的半徑“); tooltip.settooltip(btnclearmarkers,“清除繪制在源圖像上,用于分水嶺算法的大致分割區(qū)域線條“);/ 加載圖像loadimage();/ 當(dāng)窗體關(guān)閉時(shí),釋放資源privatevoid formimagesegment_formclosing

6、(object sender, formclosingeventargs e)if (imagesource !=null ) imagesource.dispose();if (imagesourceclone !=null )imagesourceclone.dispose(); if (imagemarkers !=null )imagemarkers.dispose();/ 加載源圖像privatevoid btnloadimage_click(object sender, eventargs e)openfiledialog ofd =new openfiledialog(); of

7、d.checkfileexists =true ; ofd.defaultext =“jpg“ ;ofd.filter =“圖片文件|*.jpg;*.png;*.bmp|全部文件|*.*“ ; if (ofd.showdialog(this ) = dialogresult.ok)if (ofd.filename !=“ )sourceimagefilename = ofd.filename; loadimage();ofd.dispose();/ 清除分割線條privatevoid btnclearmarkers_click(object sender, eventargs e)if (im

8、agesourceclone !=null ) imagesourceclone.dispose();imagesourceclone = imagesource.copy(); pbsource.image = imagesourceclone.bitmap;imagemarkers.setzero(); drawcount =1 ;/ 當(dāng)鼠標(biāo)按下并在源圖像上移動(dòng)時(shí),在源圖像上繪制分割線條privatevoid pbsource_mousemove(object sender, mouseeventargs e)/ 假如按下了左鍵if (e.button = mousebuttons.lef

9、t)if (previousmouselocation.x >=0 && previousmouselocation.y >=0)pointp1=newpoint( int )(previousmouselocation.x (int )(previousmouselocation.y * yscale);*xscale),point p2 = new point( int )(e.location.x* xscale), ( int )(e.location.y* yscale); linesegment2d ls =new linesegment2d(p1, p

10、2);int thickness = (int )(linewidth * xscale); imagesourceclone.draw(ls,new bgr(255d, 255d, 255d), thickness); pbsource.image = imagesourceclone.bitmap;imagemarkers.draw(ls,new gray(drawcount), thickness);previousmouselocation = e.location;/ 當(dāng)松開鼠標(biāo)左鍵時(shí),將繪圖的前一位置設(shè)置為(-1 ,-1 )privatevoid pbsource_mouseup(

11、object sender, mouseeventargs e)previousmouselocation =new point(- 1, - 1 ); drawcount+;/ 加載源圖像privatevoid loadimage()if (imagesource !=null ) imagesource.dispose();imagesource =new image<bgr,byte >(sourceimagefilename); if (imagesourceclone !=null )imagesourceclone.dispose(); imagesourceclone

12、 = imagesource.copy(); pbsource.image = imagesourceclone.bitmap; if (imagemarkers !=null )imagemarkers.dispose();imagemarkers =new image<gray, int32>(imagesource.size); imagemarkers.setzero();xscale = 1d * imagesource.width / pbsource.width; yscale = 1d * imagesource.height / pbsource.height;d

13、rawcount1=;/分割圖像private void btnimagesegment_click(object sender, eventargs e)if (rbwatershed.checked) txtresult.text += watershed();else if (rbprysegmentation.checked) txtresult.text += prysegmentation();else if (rbprymeanshiftfiltering.checked) txtresult.text += prymeanshiftfiltering();/ <summa

14、ry>/ 分水嶺算法圖像分割/ </summary>/ <returns>返回用時(shí)</returns> private string watershed()/分水嶺算法分割image<gray, int32> imagemarkers2 = imagemarkers.copy(); stopwatch sw =new stopwatch();sw.start();cvinvoke.cvwatershed(imagesource.ptr, imagemarkers2.ptr); sw.stop();/將分割的結(jié)果轉(zhuǎn)換到 256 級(jí)灰度圖像pb

15、result.image = imagemarkers2.bitmap; imagemarkers2.dispose();return string.format(“ 分 水 嶺 圖 像 分 割 , 用 時(shí) :sw.elapsed.totalmilliseconds);/ <summary>/ 金字塔分割算法/ </summary>/ <returns></returns>private string prysegmentation()0:f05毫 秒 。 rn“,/預(yù)備參數(shù)image<bgr, byte> imagedest n=e

16、w image<bgr, byte>(imagesource.size);memstorage storage n=ew memstorage();intptr ptrcomp = intptr.zero;int level = int.parse(txtpslevel.text);double threshold1 = double.parse(txtpsthreshold1.text); double threshold2 = double.parse(txtpsthreshold2.text);/金字塔分割stopwatch sw =new stopwatch(); sw.s

17、tart();cvinvoke.cvpyrsegmentation(imagesource.ptr, imagedest.ptrs,torage.ptr, out ptrcomp, level, threshold1, threshold2);sw.stop();/顯示結(jié)果pbresult.image = imagedest.bitmap;/釋放資源imagedest.dispose(); storage.dispose();return string.format(“金字塔分割,用時(shí):0:f05毫秒。rn“, sw.elapsed.totalmilliseconds);/ <summa

18、ry>/ 均值漂移分割算法/ </summary>/ <returns></returns>private string prymeanshiftfiltering()/預(yù)備參數(shù)image<bgr, byte> imagedestn=ew image<bgr, byte>(imagesource.size);double spatialradius = double.parse(txtpmsfspatialradius.text); double colorradius = double.parse(txtpmsfcolorra

19、dius.text);int maxlevel = int.parse(txtpmsfnaxlevel.text); int maxiter = int.parse(txtpmsfmaxiter.text);double epsilon = double.parse(txtpmsfepsilon.text);mcvtermcriteria termcrit/均值漂移分割n=ew mcvtermcriteria(maxiter, epsilon);stopwatch sw =new stopwatch(); sw.start();opencvinvoke.cvpyrmeanshiftfilter

20、ing(imagesource.ptr, imagedest.ptr, spatialradiu colorradius, maxlevel, termcrit);sw.stop();/顯示結(jié)果pbresult.image = imagedest.bitmap;/釋放資源imagedest.dispose();returnstring.format(“ 均sw.elapsed.totalmilliseconds);值 漂 移 分 割 , 用 時(shí) : 0:f05 毫 秒 。 rn“,/ <summary>/ 當(dāng)轉(zhuǎn)變金字塔分割的參數(shù)“金字塔層數(shù)”時(shí),對(duì)參數(shù)進(jìn)行校驗(yàn)/ </summary>/ <param name=“sender“></param>/ <param name=“e“>&l

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論