00001 /*------------------------------------------------------------------------------------------*\ 00002 This file contains material supporting chapter 5 of the cookbook: 00003 Computer Vision Programming using the OpenCV Library. 00004 by Robert Laganiere, Packt Publishing, 2011. 00005 00006 This program is free software; permission is hereby granted to use, copy, modify, 00007 and distribute this source code, or portions thereof, for any purpose, without fee, 00008 subject to the restriction that the copyright notice may not be removed 00009 or altered from any source or altered source distribution. 00010 The software is released on an as-is basis and without any warranties of any kind. 00011 In particular, the software is not guaranteed to be fault-tolerant or free from failure. 00012 The author disclaims all warranties with regard to this software, any use, 00013 and any consequent failure, is purely the responsibility of the user. 00014 00015 Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name 00016 \*------------------------------------------------------------------------------------------*/ 00017 00018 #if !defined WATERSHS 00019 #define WATERSHS 00020 00021 #include <opencv2/core/core.hpp> 00022 #include <opencv2/imgproc/imgproc.hpp> 00023 00024 class WatershedSegmenter { 00025 00026 private: 00027 00028 cv::Mat markers; 00029 00030 public: 00031 00032 void setMarkers(const cv::Mat& markerImage) { 00033 00034 // Convert to image of ints 00035 markerImage.convertTo(markers,CV_32S); 00036 } 00037 00038 cv::Mat process(const cv::Mat &image) { 00039 00040 // Apply watershed 00041 cv::watershed(image,markers); 00042 00043 return markers; 00044 } 00045 00046 // Return result in the form of an image 00047 cv::Mat getSegmentation() { 00048 00049 cv::Mat tmp; 00050 // all segment with label higher than 255 00051 // will be assigned value 255 00052 markers.convertTo(tmp,CV_8U); 00053 00054 return tmp; 00055 } 00056 00057 // Return watershed in the form of an image 00058 cv::Mat getWatersheds() { 00059 00060 cv::Mat tmp; 00061 markers.convertTo(tmp,CV_8U,255,255); 00062 00063 return tmp; 00064 } 00065 }; 00066 00067 00068 #endif