转自:

OpenCV 实现分水岭算法

种子点的标记没有太搞懂,这个算法的速度还是很快的

[cpp] view plaincopy print?
  1. // watershed_test20140801.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. //
  5. // ch9_watershed image
  6. //   This is an exact copy of the watershed.cpp demo in the OpenCV ../samples/c directory
  7. //
  8. // Think about using a morphologically eroded forground and background segmented image as the template
  9. // for the watershed algorithm to segment objects by color and edges for collecting
  10. //
  11. /* *************** License:**************************
  12. Oct. 3, 2008
  13. Right to use this code in any way you want without warrenty, support or any guarentee of it working.
  14. BOOK: It would be nice if you cited it:
  15. Learning OpenCV: Computer Vision with the OpenCV Library
  16. by Gary Bradski and Adrian Kaehler
  17. Published by O'Reilly Media, October 3, 2008
  18. AVAILABLE AT:
  19. http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
  20. Or: http://oreilly.com/catalog/9780596516130/
  21. ISBN-10: 0596516134 or: ISBN-13: 978-0596516130
  22. OTHER OPENCV SITES:
  23. * The source code is on sourceforge at:
  24. http://sourceforge.net/projects/opencvlibrary/
  25. * The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back):
  26. http://opencvlibrary.sourceforge.net/
  27. * An active user group is at:
  28. http://tech.groups.yahoo.com/group/OpenCV/
  29. * The minutes of weekly OpenCV development meetings are at:
  30. http://pr.willowgarage.com/wiki/OpenCV
  31. ************************************************** */
  32. #include "cv.h"
  33. #include "highgui.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <iostream>
  37. using namespace std;
  38. using namespace cv;
  39. #pragma comment(lib,"opencv_core2410d.lib")
  40. #pragma comment(lib,"opencv_highgui2410d.lib")
  41. #pragma comment(lib,"opencv_imgproc2410d.lib")
  42. IplImage* marker_mask = 0;
  43. IplImage* markers = 0;
  44. IplImage* img0 = 0, *img = 0, *img_gray = 0, *wshed = 0;
  45. CvPoint prev_pt = {-1,-1};
  46. void on_mouse( int event, int x, int y, int flags, void* param )
  47. {
  48. if( !img )
  49. return;
  50. if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
  51. prev_pt = cvPoint(-1,-1);
  52. else if( event == CV_EVENT_LBUTTONDOWN )
  53. prev_pt = cvPoint(x,y);
  54. else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
  55. {
  56. CvPoint pt = cvPoint(x,y);
  57. if( prev_pt.x < 0 )
  58. prev_pt = pt;
  59. cvLine( marker_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
  60. cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
  61. prev_pt = pt;
  62. cvShowImage( "image", img );
  63. }
  64. }
  65. int main( int argc, char** argv )
  66. {
  67. cout<<"input image name:  "<<endl;
  68. string file;
  69. cin>>file;
  70. char* filename = (char *)file.c_str();
  71. CvRNG rng = cvRNG(-1);
  72. if( (img0 = cvLoadImage(filename,1)) == 0 )
  73. return 0;
  74. printf( "Hot keys: \n"
  75. "\tESC - quit the program\n"
  76. "\tr - restore the original image\n"
  77. "\tw or ENTER - run watershed algorithm\n"
  78. "\t\t(before running it, roughly mark the areas on the image)\n"
  79. "\t  (before that, roughly outline several markers on the image)\n" );
  80. cvNamedWindow( "image", 1 );
  81. cvNamedWindow( "watershed transform", 1 );
  82. img = cvCloneImage( img0 );
  83. img_gray = cvCloneImage( img0 );
  84. wshed = cvCloneImage( img0 );
  85. marker_mask = cvCreateImage( cvGetSize(img), 8, 1 );
  86. markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 );
  87. cvCvtColor( img, marker_mask, CV_BGR2GRAY );
  88. cvCvtColor( marker_mask, img_gray, CV_GRAY2BGR );
  89. cvZero( marker_mask );
  90. cvZero( wshed );
  91. cvShowImage( "image", img );
  92. cvShowImage( "watershed transform", wshed );
  93. cvSetMouseCallback( "image", on_mouse, 0 );
  94. for(;;)
  95. {
  96. int c = cvWaitKey(0);
  97. if( (char)c == 27 )
  98. break;
  99. if( (char)c == 'r' )
  100. {
  101. cvZero( marker_mask );
  102. cvCopy( img0, img );
  103. cvShowImage( "image", img );
  104. }
  105. if( (char)c == 'w' || (char)c == '\n' )
  106. {
  107. CvMemStorage* storage = cvCreateMemStorage(0);
  108. CvSeq* contours = 0;
  109. CvMat* color_tab;
  110. int i, j, comp_count = 0;
  111. //cvSaveImage( "wshed_mask.png", marker_mask );
  112. //marker_mask = cvLoadImage( "wshed_mask.png", 0 );
  113. cvFindContours( marker_mask, storage, &contours, sizeof(CvContour),
  114. CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
  115. cvZero( markers );
  116. for( ; contours != 0; contours = contours->h_next, comp_count++ )
  117. {
  118. cvDrawContours( markers, contours, cvScalarAll(comp_count+1),
  119. cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );
  120. }
  121. color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );
  122. for( i = 0; i < comp_count; i++ )
  123. {
  124. uchar* ptr = color_tab->data.ptr + i*3;
  125. ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);
  126. ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);
  127. ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);
  128. }
  129. {
  130. double t = (double)cvGetTickCount();
  131. cvWatershed( img0, markers );
  132. t = (double)cvGetTickCount() - t;
  133. printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );
  134. }
  135. // paint the watershed image
  136. for( i = 0; i < markers->height; i++ )
  137. for( j = 0; j < markers->width; j++ )
  138. {
  139. int idx = CV_IMAGE_ELEM( markers, int, i, j );
  140. uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );
  141. if( idx == -1 )
  142. dst[0] = dst[1] = dst[2] = (uchar)255;
  143. else if( idx <= 0 || idx > comp_count )
  144. dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here
  145. else
  146. {
  147. uchar* ptr = color_tab->data.ptr + (idx-1)*3;
  148. dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];
  149. }
  150. }
  151. cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed );
  152. cvShowImage( "watershed transform", wshed );
  153. cvReleaseMemStorage( &storage );
  154. cvReleaseMat( &color_tab );
  155. }
  156. }
  157. return 1;
  158. }

实现效果:

OpenCV 实现分水岭算法相关推荐

  1. pythonopencv算法_python opencv之分水岭算法示例

    本文介绍了python opencv之分水岭算法示例,分享给大家,具体如下: 目标 使用分水岭算法对基于标记的图像进行分割 使用函数cv2.watershed() 原理: 灰度图像可以被看成拓扑平面, ...

  2. opencv实现分水岭算法

    opencv实现分水岭算法 // 分水岭算法原理 // IplImage* marker_mask = 0; IplImage* markers = 0; //IplImage* img0 = 0, ...

  3. 使用OpenCV的分水岭算法

    <使用OpenCV的分水岭算法>   之前利用watershed想对相对前背景较为明显的图像进行图像语义分割的预打标,因为虽然前景明显,但是边缘打标也是很困难的,可以用该方法对大部分的边缘 ...

  4. python opencv 利用分水岭算法实现对物体的分割 图文详细注释版 以分割官网提供的硬币为例

    分水岭算法可以实现自动分割多个物体,opencv中 cv.watershed() 函数实现了分水岭算法 话不多说,上代码 # 利用分水岭算法分离多个相同硬币 import numpy as np im ...

  5. 【OpenCV】- 分水岭算法

    文章目录 什么是图像分割 分水岭算法 1.实现分水岭算法:watershed()函数 2.处理流程(视频) 3.示例程序(书中) 什么是图像分割 将图像中像素根据一定的规则分为若干个cluster集合 ...

  6. opencv之分水岭算法分割及图像修补

    1)分水岭算法 原理: 任何一幅灰度图像都可以被看成是拓扑平面,灰度值高的区域可以被看成是山峰,灰度值低的区域可以被看成是山谷,我们向每一个山谷中灌不同颜色的水,随着水位的升高,不同山谷的水就会相遇汇 ...

  7. OpenCV之分水岭算法

    分水岭算法 在许多实际的应用中,我们需要分割图像,但是无法从背景图像中获得有用信息.但是分水岭算法在这方面往往非常有效,它可以将图像中的边缘转化为"山脉",将均匀区域转化为&quo ...

  8. 【python】【openCV】分水岭算法

    脑血管医学图像颅内分割尝试--分水岭算法 code 1.2 不分割颅内直接分割 code 2.0 实验版 code 3.0 批量处理版 code 3.1 加入孔洞填充 总结 本篇博客原目的同https ...

  9. opencv 图像分割-分水岭算法

    任何灰度图都恶意被看作是一个地形面,高强度表示山峰和山丘,低强度则表示山谷.开始使用不同的水来填充每个孤立的山谷(局部最小值).随着水的上升,来自不同山谷的水,开始融合.为了避免这种情况,在水合并的地 ...

  10. Opencv 分水岭算法 watershed的图像分割

    分水岭算法 参考博客: (1)迈克老狼2012   https://www.cnblogs.com/mikewolf2002/p/3304118.html (2)-牧野-              h ...

最新文章

  1. C++中的Lock简单用法
  2. 第一个C#控制台程序
  3. raft算法与paxos算法相比有什么优势,使用场景有什么差异?
  4. ASP.NET Core 源码学习之 Options[3]:IOptionsSnapshot
  5. bio java 例子_JAVA BIO 服务器与客户端实现示例
  6. linux网络编程之通信协议格式
  7. 30.Android之百度地图简单学习
  8. matlab中数据变为nan,字符转化为数值型中出现NAN
  9. linux vi 撤销与恢复,vi撤销与恢复
  10. Java实现hsql_java – 从类创建HSQL创建表查询
  11. B树和B+树详细解析
  12. 2018,如何从技术小白升级到大牛程序员?
  13. ICMP报文类型和代码
  14. 类与对象- 课后作业1
  15. 史上最全linux内核配置--Device drivers
  16. 4选1数据选择器程序及testbench文件,给出仿真波形,分析
  17. Android - View 和 ViewGroup
  18. 信息系统项目管理师知识总结
  19. 用4K屏幕的笔记本跑虚拟机vmware如何放大字体?
  20. UE4制作多语言游戏(本地化功能详解)

热门文章

  1. linux测试at命令,linux at命令:(定时执行脚本)
  2. java invoke 返回类型_java-控制器处理程序方法支持的返回类型
  3. ubuntu切换用户root时认证失败
  4. 切片与MapTask并行度决定机制
  5. PS使用:利用PS制作旋转水晶球gif图
  6. c语言 归一化图片大小,OpenCV学习笔记(1)——resize函数实现图像大小归一化
  7. Global.asax取绝对路径
  8. C++_虚函数的实现的基本原理
  9. 三维重建笔记_三维重建方法导图
  10. karto探秘之open_karto 第一章 --- 数据结构与类的初始化