$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2011, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 00037 #ifndef GRABCUT3D_OBJECT_SEGMENTER_H 00038 #define GRABCUT3D_OBJECT_SEGMENTER_H 00039 00040 //#include "bosch_object_segmentation_gui/ObjectSegmentationGuiAction.h" 00041 00042 #include <opencv2/highgui/highgui.hpp> 00043 #include <opencv2/core/core.hpp> 00044 #include <opencv2/imgproc/imgproc.hpp> 00045 #include <iostream> 00046 #include <ros/ros.h> 00047 00048 using namespace std; 00049 using namespace cv; 00050 00051 namespace image_segmentation_demo { 00052 00053 class GrabCut3DObjectSegmenter 00054 { 00055 public: 00056 /* Allowed background colors for OpenCV window */ 00057 enum MouseEvent { LEFT_BUTTON_DOWN, RIGHT_BUTTON_DOWN, LEFT_BUTTON_UP, RIGHT_BUTTON_UP, MOUSE_MOVE}; 00058 00059 /* States of grabbing/marking action */ 00060 enum GrabState { NOT_SET, IN_PROCESS, SET, EMPTY }; 00061 00062 /* Allowed background colors for OpenCV window */ 00063 enum WinColor { BLACK, GRAY, WHITE, GREEN, BLUE }; 00064 00065 /* Marker for default rectangle. */ 00066 static const Rect DEFAULT_RECT; 00067 00068 /* Constructor/destructor */ 00069 GrabCut3DObjectSegmenter(); 00070 ~GrabCut3DObjectSegmenter(); 00071 00072 /* Accessors for application state */ 00073 inline bool initialized() { return initialized_; } 00074 void initializedIs(bool); 00075 00076 /* Accessors/mutators for base image and mask */ 00077 void setImages(const Mat& image, const Mat& depth_image); 00078 inline const Mat& image() { return image_; } 00079 inline const Mat& mask() { return mask_; } 00080 inline const Mat& displayImage() { return display_image_; } 00081 inline const Mat& depthImage() { return depth_image_; } 00082 Mat binaryMask(); 00083 00084 /* update display image */ 00085 void updateDisplayImage(); 00086 /* get window color */ 00087 inline WinColor winColor() { return win_color_; } 00088 /* set window color */ 00089 void setWinColor(WinColor _c); 00090 00091 /* Mutator for GC rectangle. */ 00092 void rectIs(const Rect&); 00093 00094 /* Accessor for state variables */ 00095 inline GrabState rectState() { return rect_state_; } 00096 00097 /* Mouse callback for image topic subscription */ 00098 void mouseClick( MouseEvent event, int x, int y, bool control_down, bool shift_down); 00099 00100 /* Iteration control accessors */ 00101 inline int iterCount() const { return iter_count_; } 00102 void iterCountIs(int _icnt); 00103 inline void iterCountInc() { iterCountIs(iterCount()+1); } 00104 00105 private: 00106 /* Transfer marked rectangle to mask image */ 00107 void setRectInMask(); 00108 /* Transfer marked labels to mask image */ 00109 void setLblsInMask(bool control_down, bool shift_down, Point p, bool isPr ); 00110 00111 /* Current state of OpenCV window image */ 00112 WinColor win_color_; 00113 00114 /* Segmentation process data */ 00115 Mat image_; // image to be segmented 00116 Mat depth_image_; // depth image to be segmented 00117 Mat display_image_; // image used for rendering the results 00118 Mat mask_; // fg/bg mask for grabcut algo. 00119 Mat bgd_model_, fgd_model_; // temporary arrays used in segmentation 00120 Rect rect_; // marked rectangle 00121 vector<Point> fgd_pxls_; // definite foreground points 00122 vector<Point> bgd_pxls_; // definite background points 00123 vector<Point> pr_fgd_pxls_; // probable foreground points 00124 vector<Point> pr_bgd_pxls_; // probable background points 00125 int iter_count_; // number of most recent completed iteration 00126 00127 /* Current state of grabbing / marking process */ 00128 GrabState rect_state_, lbls_state_, pr_lbls_state_; 00129 00130 /* Used to allow client to return application to uninitialized state. */ 00131 bool initialized_; 00132 00133 }; 00134 } 00135 00136 #endif // GRABCUT3D_OBJECT_SEGMENTER_H