![opencv实现分水岭,金字塔,均值漂移算法进行分割_第1页](http://file3.renrendoc.com/fileroot_temp3/2021-12/3/f58aa0b2-28c0-48e8-92ad-8530fc5ac5b5/f58aa0b2-28c0-48e8-92ad-8530fc5ac5b51.gif)
![opencv实现分水岭,金字塔,均值漂移算法进行分割_第2页](http://file3.renrendoc.com/fileroot_temp3/2021-12/3/f58aa0b2-28c0-48e8-92ad-8530fc5ac5b5/f58aa0b2-28c0-48e8-92ad-8530fc5ac5b52.gif)
![opencv实现分水岭,金字塔,均值漂移算法进行分割_第3页](http://file3.renrendoc.com/fileroot_temp3/2021-12/3/f58aa0b2-28c0-48e8-92ad-8530fc5ac5b5/f58aa0b2-28c0-48e8-92ad-8530fc5ac5b53.gif)
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 ;/ 标记图像privatedouble xscale = 1d;/ 原始图像与 picturebox 在 x 轴方向上的缩放privatedouble yscale = 1d;/
3、原始图像与 picturebox 在 y 轴方向上的缩放privatepoint previousmouselocation =newpoint(-1, - 1 );/ 上次绘制线条时,鼠标所处的位置privateconst int linewidth =5;/绘制线条的宽度privatestring sourceimagefilename =“wky_tms_2272x1704.jpg“;/ 源图像文件名private image<bgr, byte> imagesource =null ;/ 源图像privateint drawcount =1 ;/ 用户绘制的线条数目,用于指
4、定线条的颜色public formimagesegment()initializecomponent();/ 窗体加载时privatevoid formimagesegment_load(object sender, eventargs e)法“);/ 设置提示tooltip.settooltip(rbwatershed,“可以在源图像上用鼠标绘制大致分割区域线条,该线条用于分水岭算tooltip.settooltip(txtpslevel,“金字塔层数跟图像尺寸有关,该值只能是图像尺寸被 2 整除的次数,否则将得出错误结果“);tooltip.settooltip(txtpsthreshol
5、d1,“建立连接的错误阀值“);tooltip.settooltip(txtpsthreshold2,“分割簇的错误阀值“); tooltip.settooltip(txtpmsfspatialradius,“空间窗的半径“); tooltip.settooltip(txtpmsfcolorradius,“颜色窗的半径“); tooltip.settooltip(btnclearmarkers,“清除绘制在源图像上,用于分水岭算法的大致分割区域线条“);/ 加载图像loadimage();/ 当窗体关闭时,释放资源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 ;/ 当鼠标按下并在源图像上移动时,在源图像上绘制分割线条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;/ 当松开鼠标左键时,将绘图的前一位置设置为(-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>返回用时</returns> private string watershed()/分水岭算法分割image<gray, int32> imagemarkers2 = imagemarkers.copy(); stopwatch sw =new stopwatch();sw.start();cvinvoke.cvwatershed(imagesource.ptr, imagemarkers2.ptr); sw.stop();/将分割的结果转换到 256 级灰度图像pb
15、result.image = imagemarkers2.bitmap; imagemarkers2.dispose();return string.format(“ 分 水 岭 图 像 分 割 , 用 时 :sw.elapsed.totalmilliseconds);/ <summary>/ 金字塔分割算法/ </summary>/ <returns></returns>private string prysegmentation()0:f05毫 秒 。 rn“,/预备参数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();/显示结果pbresult.image = imagedest.bitmap;/释放资源imagedest.dispose(); storage.dispose();return string.format(“金字塔分割,用时:0:f05毫秒。rn“, sw.elapsed.totalmilliseconds);/ <summa
18、ry>/ 均值漂移分割算法/ </summary>/ <returns></returns>private string prymeanshiftfiltering()/预备参数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();/显示结果pbresult.image = imagedest.bitmap;/释放资源imagedest.dispose();returnstring.format(“ 均sw.elapsed.totalmilliseconds);值 漂 移 分 割 , 用 时 : 0:f05 毫 秒 。 rn“,/ <summary>/ 当转变金字塔分割的参数“金字塔层数”时,对参数进行校验/ </summary>/ <param name=“sender“></param>/ <param name=“e“>&l
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 体育赛事用帐篷购销合同
- 双方夫妻离婚协议书
- 柚子水果购销合同
- 软件和信息技术服务外包合作协议
- 离婚协议书去哪弄
- 环境监测技术设备供应协议
- 绿色出行服务平台合作协议
- 砂石场劳动合同
- 农产品电商运营推广合同
- 房产中介公司劳动合同
- 三年级数学下册期末测试卷及答案【可打印】
- 苏教版小学语文上册教学研究论文
- 片状锌粉行业分析!中国片状锌粉行业市场发展前景研究报告(2024版)
- 儿童绘本故事《我的情绪小怪兽》
- 部编版六年级下册道德与法治全册教案
- 2024版《供电营业规则》学习考试题库500题(含答案)
- 供货送货服务承诺书
- G -B- 43630-2023 塔式和机架式服务器能效限定值及能效等级(正式版)
- EPC项目质量保证措施
- 2023-2024学年安徽省合肥市瑶海区八年级(下)期中数学试卷(含解析)
- 【体能大循环】聚焦体能循环-探索运动奥秘-幼儿园探究体能大循环有效开展策略课件
评论
0/150
提交评论