]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkSegmentationConnectivity.cxx
679e0706aa5530eb68a30d28860d995f98e7ac86
[bbtk.git] / packages / vtk / src / bbvtkSegmentationConnectivity.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbvtkSegmentationConnectivity.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:51:58 $
33   Version:   $Revision: 1.8 $
34 =========================================================================*/
35
36 /**
37  *  \file 
38  *  \brief 
39  */
40
41 #ifdef _USE_VTK_
42
43 #include "bbvtkSegmentationConnectivity.h"
44 #include "bbvtkPackage.h"
45
46 namespace bbvtk
47 {
48   BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,SegmentationConnectivity);
49   BBTK_BLACK_BOX_IMPLEMENTATION(SegmentationConnectivity,
50                                 bbtk::AtomicBlackBox);
51
52 // --------------------------------------------------------------
53
54   void SegmentationConnectivity::bbUserSetDefaultValues() 
55   { 
56           thresh2       = NULL;
57           cast2         = NULL;
58           connect2      = NULL;
59           cast4         = NULL;
60                 
61           bbSetInputIn(NULL);
62           std::vector<int> position;
63           position.push_back(0);
64           position.push_back(0);
65           position.push_back(0);
66           bbSetInputPositionXYZ(position);
67           std::vector<int> threshold;
68           threshold.push_back(0);
69           threshold.push_back(0);
70           bbSetInputThresholdMinMax(threshold);
71           bbSetOutputOut(NULL);
72   }
73
74 // --------------------------------------------------------------
75                 
76         void SegmentationConnectivity::bbUserInitializeProcessing()
77         {
78                 thresh2 = vtkImageThreshold::New();
79                         thresh2->SetInValue(255);
80                         thresh2->SetOutputScalarTypeToUnsignedShort();
81                         thresh2->SetOutValue(0);
82                 cast2 = vtkImageCast::New();
83                         cast2->SetInput(thresh2->GetOutput());
84                         cast2->SetOutputScalarTypeToUnsignedChar();
85                 connect2 = vtkImageSeedConnectivity::New();
86                         connect2->SetInput(cast2->GetOutput());
87                         connect2->SetInputConnectValue(255);
88                         connect2->SetOutputConnectedValue(255);
89                         connect2->SetOutputUnconnectedValue(0);
90                 cast4 = vtkImageCast::New();
91                         cast4->SetInput(connect2->GetOutput());
92                         cast4->SetOutputScalarTypeToUnsignedShort();
93         }
94
95 // --------------------------------------------------------------
96                 
97   void SegmentationConnectivity::bbUserFinalizeProcessing() 
98   { 
99           if (thresh2!=NULL)
100           {
101                   thresh2->Delete();
102                   thresh2=NULL;
103           }
104           if (cast2!=NULL)
105           {
106                   cast2->Delete();
107                   cast2=NULL;
108           }
109           if (connect2!=NULL)
110           {
111                   connect2->Delete();
112                   connect2=NULL;
113           }
114           if (cast4!=NULL)
115           {
116                   cast4->Delete();
117                   cast4=NULL;
118           }  
119   }
120
121 // --------------------------------------------------------------
122                 
123   void SegmentationConnectivity::DoProcess()
124   {
125     vtkImageData *imagedata = bbGetInputIn();
126     imagedata->UpdateInformation();
127     imagedata->SetUpdateExtent(imagedata->GetWholeExtent());
128     imagedata->Update();
129     thresh2->ThresholdBetween(3000, 3001);
130     thresh2->SetInput(imagedata);
131     thresh2->ThresholdBetween(bbGetInputThresholdMinMax()[0], bbGetInputThresholdMinMax()[1]);
132     thresh2->Modified();
133     thresh2->Update();
134     cast2->Modified();
135     cast2->Update();
136     connect2->RemoveAllSeeds ();
137           
138         int x = bbGetInputPositionXYZ()[0];
139         int y = bbGetInputPositionXYZ()[1];
140         int z = bbGetInputPositionXYZ()[2];
141
142           int ext[6];
143           imagedata->GetWholeExtent(ext);
144           int maxx = ext[1]-ext[0]+1;
145           int maxy = ext[3]-ext[2]+1;
146           int maxz = ext[5]-ext[4]+1;
147           if ( x<0 ) {  printf("Warnnig bbtk::vtk::SegmentationConnectivity::DoProcess limites of the image out of range x: %d -> 0 \n",x ); x=0; }
148           if ( y<0 ) {  printf("Warnnig bbtk::vtk::SegmentationConnectivity::DoProcess limites of the image out of range y: %d -> 0 \n",y ); y=0;}
149           if ( z<0 ) {  printf("Warnnig bbtk::vtk::SegmentationConnectivity::DoProcess limites of the image out of range z: %d -> 0 \n",z ); z=0;}
150
151           if ( x>=maxx ) {  printf("Warnnig bbtk::vtk::SegmentationConnectivity::DoProcess limites of the image out of range x: %d -> %d \n",x, maxx-1 ); x=maxx-1; }
152           if ( y>=maxy ) {  printf("Warnnig bbtk::vtk::SegmentationConnectivity::DoProcess limites of the image out of range y: %d -> %d \n",y, maxy-1 ); y=maxy-1; }
153           if ( z>=maxz ) {  printf("Warnnig bbtk::vtk::SegmentationConnectivity::DoProcess limites of the image out of range z: %d -> %d \n",y, maxz-1 ); z=maxz-1; }
154           
155           connect2->AddSeed( x,y,z );
156           connect2->Modified();
157           connect2->Update();
158           cast4->Update();                      
159           bbSetOutputOut(cast4->GetOutput() );
160   }
161   
162 }// EO namespace bbvtk
163
164 #endif //_USE_VTK_
165
166
167
168
169