Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 3 | PointedEar | 1 | /*------------------------------------------------------------------------------------------*\ |
| 2 | This file contains material supporting chapter 5 of the cookbook: |
||
| 3 | Computer Vision Programming using the OpenCV Library. |
||
| 4 | by Robert Laganiere, Packt Publishing, 2011. |
||
| 5 | |||
| 6 | This program is free software; permission is hereby granted to use, copy, modify, |
||
| 7 | and distribute this source code, or portions thereof, for any purpose, without fee, |
||
| 8 | subject to the restriction that the copyright notice may not be removed |
||
| 9 | or altered from any source or altered source distribution. |
||
| 10 | The software is released on an as-is basis and without any warranties of any kind. |
||
| 11 | In particular, the software is not guaranteed to be fault-tolerant or free from failure. |
||
| 12 | The author disclaims all warranties with regard to this software, any use, |
||
| 13 | and any consequent failure, is purely the responsibility of the user. |
||
| 14 | |||
| 15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name |
||
| 16 | \*------------------------------------------------------------------------------------------*/ |
||
| 17 | |||
| 18 | #if !defined WATERSHS |
||
| 19 | #define WATERSHS |
||
| 20 | |||
| 21 | #include <opencv2/core/core.hpp> |
||
| 22 | #include <opencv2/imgproc/imgproc.hpp> |
||
| 23 | |||
| 24 | class WatershedSegmenter { |
||
| 25 | |||
| 26 | private: |
||
| 27 | |||
| 28 | cv::Mat markers; |
||
| 29 | |||
| 30 | public: |
||
| 31 | |||
| 32 | void setMarkers(const cv::Mat& markerImage) { |
||
| 33 | |||
| 34 | // Convert to image of ints |
||
| 35 | markerImage.convertTo(markers,CV_32S); |
||
| 36 | } |
||
| 37 | |||
| 38 | cv::Mat process(const cv::Mat &image) { |
||
| 39 | |||
| 40 | // Apply watershed |
||
| 41 | cv::watershed(image,markers); |
||
| 42 | |||
| 43 | return markers; |
||
| 44 | } |
||
| 45 | |||
| 46 | // Return result in the form of an image |
||
| 47 | cv::Mat getSegmentation() { |
||
| 48 | |||
| 49 | cv::Mat tmp; |
||
| 50 | // all segment with label higher than 255 |
||
| 51 | // will be assigned value 255 |
||
| 52 | markers.convertTo(tmp,CV_8U); |
||
| 53 | |||
| 54 | return tmp; |
||
| 55 | } |
||
| 56 | |||
| 57 | // Return watershed in the form of an image |
||
| 58 | cv::Mat getWatersheds() { |
||
| 59 | |||
| 60 | cv::Mat tmp; |
||
| 61 | markers.convertTo(tmp,CV_8U,255,255); |
||
| 62 | |||
| 63 | return tmp; |
||
| 64 | } |
||
| 65 | }; |
||
| 66 | |||
| 67 | |||
| 68 | #endif |