OpenCV 实现分水岭算法
OpenCV 实现分水岭算法
种子点的标记没有太搞懂,这个算法的速度还是很快的
- // watershed_test20140801.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- //
- // ch9_watershed image
- // This is an exact copy of the watershed.cpp demo in the OpenCV ../samples/c directory
- //
- // Think about using a morphologically eroded forground and background segmented image as the template
- // for the watershed algorithm to segment objects by color and edges for collecting
- //
- /* *************** License:**************************
- Oct. 3, 2008
- Right to use this code in any way you want without warrenty, support or any guarentee of it working.
- BOOK: It would be nice if you cited it:
- Learning OpenCV: Computer Vision with the OpenCV Library
- by Gary Bradski and Adrian Kaehler
- Published by O'Reilly Media, October 3, 2008
- AVAILABLE AT:
- http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
- Or: http://oreilly.com/catalog/9780596516130/
- ISBN-10: 0596516134 or: ISBN-13: 978-0596516130
- OTHER OPENCV SITES:
- * The source code is on sourceforge at:
- http://sourceforge.net/projects/opencvlibrary/
- * The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back):
- http://opencvlibrary.sourceforge.net/
- * An active user group is at:
- http://tech.groups.yahoo.com/group/OpenCV/
- * The minutes of weekly OpenCV development meetings are at:
- http://pr.willowgarage.com/wiki/OpenCV
- ************************************************** */
- #include "cv.h"
- #include "highgui.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- using namespace cv;
- #pragma comment(lib,"opencv_core2410d.lib")
- #pragma comment(lib,"opencv_highgui2410d.lib")
- #pragma comment(lib,"opencv_imgproc2410d.lib")
- IplImage* marker_mask = 0;
- IplImage* markers = 0;
- IplImage* img0 = 0, *img = 0, *img_gray = 0, *wshed = 0;
- CvPoint prev_pt = {-1,-1};
- void on_mouse( int event, int x, int y, int flags, void* param )
- {
- if( !img )
- return;
- if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
- prev_pt = cvPoint(-1,-1);
- else if( event == CV_EVENT_LBUTTONDOWN )
- prev_pt = cvPoint(x,y);
- else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
- {
- CvPoint pt = cvPoint(x,y);
- if( prev_pt.x < 0 )
- prev_pt = pt;
- cvLine( marker_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
- cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
- prev_pt = pt;
- cvShowImage( "image", img );
- }
- }
- int main( int argc, char** argv )
- {
- cout<<"input image name: "<<endl;
- string file;
- cin>>file;
- char* filename = (char *)file.c_str();
- CvRNG rng = cvRNG(-1);
- if( (img0 = cvLoadImage(filename,1)) == 0 )
- return 0;
- printf( "Hot keys: \n"
- "\tESC - quit the program\n"
- "\tr - restore the original image\n"
- "\tw or ENTER - run watershed algorithm\n"
- "\t\t(before running it, roughly mark the areas on the image)\n"
- "\t (before that, roughly outline several markers on the image)\n" );
- cvNamedWindow( "image", 1 );
- cvNamedWindow( "watershed transform", 1 );
- img = cvCloneImage( img0 );
- img_gray = cvCloneImage( img0 );
- wshed = cvCloneImage( img0 );
- marker_mask = cvCreateImage( cvGetSize(img), 8, 1 );
- markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 );
- cvCvtColor( img, marker_mask, CV_BGR2GRAY );
- cvCvtColor( marker_mask, img_gray, CV_GRAY2BGR );
- cvZero( marker_mask );
- cvZero( wshed );
- cvShowImage( "image", img );
- cvShowImage( "watershed transform", wshed );
- cvSetMouseCallback( "image", on_mouse, 0 );
- for(;;)
- {
- int c = cvWaitKey(0);
- if( (char)c == 27 )
- break;
- if( (char)c == 'r' )
- {
- cvZero( marker_mask );
- cvCopy( img0, img );
- cvShowImage( "image", img );
- }
- if( (char)c == 'w' || (char)c == '\n' )
- {
- CvMemStorage* storage = cvCreateMemStorage(0);
- CvSeq* contours = 0;
- CvMat* color_tab;
- int i, j, comp_count = 0;
- //cvSaveImage( "wshed_mask.png", marker_mask );
- //marker_mask = cvLoadImage( "wshed_mask.png", 0 );
- cvFindContours( marker_mask, storage, &contours, sizeof(CvContour),
- CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
- cvZero( markers );
- for( ; contours != 0; contours = contours->h_next, comp_count++ )
- {
- cvDrawContours( markers, contours, cvScalarAll(comp_count+1),
- cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );
- }
- color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );
- for( i = 0; i < comp_count; i++ )
- {
- uchar* ptr = color_tab->data.ptr + i*3;
- ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);
- ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);
- ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);
- }
- {
- double t = (double)cvGetTickCount();
- cvWatershed( img0, markers );
- t = (double)cvGetTickCount() - t;
- printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );
- }
- // paint the watershed image
- for( i = 0; i < markers->height; i++ )
- for( j = 0; j < markers->width; j++ )
- {
- int idx = CV_IMAGE_ELEM( markers, int, i, j );
- uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );
- if( idx == -1 )
- dst[0] = dst[1] = dst[2] = (uchar)255;
- else if( idx <= 0 || idx > comp_count )
- dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here
- else
- {
- uchar* ptr = color_tab->data.ptr + (idx-1)*3;
- dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];
- }
- }
- cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed );
- cvShowImage( "watershed transform", wshed );
- cvReleaseMemStorage( &storage );
- cvReleaseMat( &color_tab );
- }
- }
- return 1;
- }
实现效果:
OpenCV 实现分水岭算法相关推荐
- pythonopencv算法_python opencv之分水岭算法示例
本文介绍了python opencv之分水岭算法示例,分享给大家,具体如下: 目标 使用分水岭算法对基于标记的图像进行分割 使用函数cv2.watershed() 原理: 灰度图像可以被看成拓扑平面, ...
- opencv实现分水岭算法
opencv实现分水岭算法 // 分水岭算法原理 // IplImage* marker_mask = 0; IplImage* markers = 0; //IplImage* img0 = 0, ...
- 使用OpenCV的分水岭算法
<使用OpenCV的分水岭算法> 之前利用watershed想对相对前背景较为明显的图像进行图像语义分割的预打标,因为虽然前景明显,但是边缘打标也是很困难的,可以用该方法对大部分的边缘 ...
- python opencv 利用分水岭算法实现对物体的分割 图文详细注释版 以分割官网提供的硬币为例
分水岭算法可以实现自动分割多个物体,opencv中 cv.watershed() 函数实现了分水岭算法 话不多说,上代码 # 利用分水岭算法分离多个相同硬币 import numpy as np im ...
- 【OpenCV】- 分水岭算法
文章目录 什么是图像分割 分水岭算法 1.实现分水岭算法:watershed()函数 2.处理流程(视频) 3.示例程序(书中) 什么是图像分割 将图像中像素根据一定的规则分为若干个cluster集合 ...
- opencv之分水岭算法分割及图像修补
1)分水岭算法 原理: 任何一幅灰度图像都可以被看成是拓扑平面,灰度值高的区域可以被看成是山峰,灰度值低的区域可以被看成是山谷,我们向每一个山谷中灌不同颜色的水,随着水位的升高,不同山谷的水就会相遇汇 ...
- OpenCV之分水岭算法
分水岭算法 在许多实际的应用中,我们需要分割图像,但是无法从背景图像中获得有用信息.但是分水岭算法在这方面往往非常有效,它可以将图像中的边缘转化为"山脉",将均匀区域转化为&quo ...
- 【python】【openCV】分水岭算法
脑血管医学图像颅内分割尝试--分水岭算法 code 1.2 不分割颅内直接分割 code 2.0 实验版 code 3.0 批量处理版 code 3.1 加入孔洞填充 总结 本篇博客原目的同https ...
- opencv 图像分割-分水岭算法
任何灰度图都恶意被看作是一个地形面,高强度表示山峰和山丘,低强度则表示山谷.开始使用不同的水来填充每个孤立的山谷(局部最小值).随着水的上升,来自不同山谷的水,开始融合.为了避免这种情况,在水合并的地 ...
- Opencv 分水岭算法 watershed的图像分割
分水岭算法 参考博客: (1)迈克老狼2012 https://www.cnblogs.com/mikewolf2002/p/3304118.html (2)-牧野- h ...
最新文章
- C++中的Lock简单用法
- 第一个C#控制台程序
- raft算法与paxos算法相比有什么优势,使用场景有什么差异?
- ASP.NET Core 源码学习之 Options[3]:IOptionsSnapshot
- bio java 例子_JAVA BIO 服务器与客户端实现示例
- linux网络编程之通信协议格式
- 30.Android之百度地图简单学习
- matlab中数据变为nan,字符转化为数值型中出现NAN
- linux vi 撤销与恢复,vi撤销与恢复
- Java实现hsql_java – 从类创建HSQL创建表查询
- B树和B+树详细解析
- 2018,如何从技术小白升级到大牛程序员?
- ICMP报文类型和代码
- 类与对象- 课后作业1
- 史上最全linux内核配置--Device drivers
- 4选1数据选择器程序及testbench文件,给出仿真波形,分析
- Android - View 和 ViewGroup
- 信息系统项目管理师知识总结
- 用4K屏幕的笔记本跑虚拟机vmware如何放大字体?
- UE4制作多语言游戏(本地化功能详解)
热门文章
- linux测试at命令,linux at命令:(定时执行脚本)
- java invoke 返回类型_java-控制器处理程序方法支持的返回类型
- ubuntu切换用户root时认证失败
- 切片与MapTask并行度决定机制
- PS使用:利用PS制作旋转水晶球gif图
- c语言 归一化图片大小,OpenCV学习笔记(1)——resize函数实现图像大小归一化
- Global.asax取绝对路径
- C++_虚函数的实现的基本原理
- 三维重建笔记_三维重建方法导图
- karto探秘之open_karto 第一章 --- 数据结构与类的初始化