~ubuntu-branches/ubuntu/oneiric/ginkgocadx/oneiric

« back to all changes in this revision

Viewing changes to src/cadxcore/commands/comandochroma.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Tille
  • Date: 2011-05-02 08:09:26 UTC
  • Revision ID: james.westby@ubuntu.com-20110502080926-bql5wep49c7hg91t
Tags: upstream-2.4.1.1
ImportĀ upstreamĀ versionĀ 2.4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
*  
 
3
*  $Id: comandochroma.cpp 3527 2011-03-16 21:45:37Z carlos $
 
4
*  Ginkgo CADx Project
 
5
*
 
6
*  Copyright 2008-10 MetaEmotion S.L. All rights reserved.
 
7
*  http://ginkgo-cadx.com
 
8
*
 
9
*  This file is licensed under LGPL v3 license.
 
10
*  See License.txt for details
 
11
*
 
12
*/
 
13
//#define _GINKGO_TRACE
 
14
//#define DUMP_IMAGENES
 
15
#include <api/globals.h>
 
16
#include "comandochroma.h"
 
17
#include <api/inotificadoresherramientas.h>
 
18
#include <api/icacheimagenes.h>
 
19
#include <main/controllers/controladorlog.h>
 
20
#include <widgets/wpoligono.h>
 
21
#include <map>
 
22
#include <wx/thread.h>
 
23
#include <api/internacionalizacion.h>
 
24
#ifdef __DEPRECATED
 
25
#undef __DEPRECATED
 
26
#endif
 
27
 
 
28
#include <itkImage.h>
 
29
#include <itkImageRegionIterator.h>
 
30
#include <itkConnectedThresholdImageFilter.h>
 
31
#include <itkThresholdImageFilter.h>
 
32
 
 
33
#include <itkBinaryThresholdImageFilter.h>
 
34
#include <itkNumericTraits.h>
 
35
#include <itkImageFileWriter.h>
 
36
 
 
37
#include "../itk/itkVTKImageToImageFilter.h"
 
38
#include <itkExceptionObject.h>
 
39
#include <itkRGBToLuminanceImageFilter.h>
 
40
#include <itkNormalizeImageFilter.h>
 
41
#include <itkCastImageFilter.h>
 
42
#include <itkRGBPixel.h>
 
43
#include <itkSmartPointer.h>
 
44
#include <vtkImageData.h>
 
45
#include <vtkPointData.h>
 
46
#include <vtkFloatArray.h>
 
47
#include <vtkImageReslice.h>
 
48
#include <vtk/vtkginkgoimageviewer.h>
 
49
 
 
50
#define IDC_CHROMA_RESETEAR              51
 
51
#define IDC_CHROMA_SEGMENTAR_PUNTO       52
 
52
#define IDC_CHROMA_CANCELAR_SEGMENTACION 53
 
53
#define IDC_CHROMA_INSERTAR_REGION       54
 
54
#define IDC_CHROMA_ELIMINAR_REGION       55
 
55
#define IDC_CHROMA_TODO                  57
 
56
#define IDC_CHROMA_INVERTIR              58
 
57
#define IDC_CHROMA_SEGMENTAR_LAZO                  59
 
58
 
 
59
// Singleton de persistencia
 
60
namespace GADAPI
 
61
{
 
62
        //callback de itk para el progreso
 
63
        class ChromaItkProgressCallback : public itk::Command
 
64
        {
 
65
 
 
66
         public:
 
67
          typedef ChromaItkProgressCallback Self;
 
68
          typedef itk::Command                Superclass;
 
69
 
 
70
          typedef itk::SmartPointer<Self>          Pointer;
 
71
          typedef itk::SmartPointer<const Self>    ConstPointer;
 
72
 
 
73
          itkTypeMacro (ChromaItkProgressCallback, itk::Command);
 
74
          itkNewMacro (Self);
 
75
 
 
76
          /** Standard Command virtual methods */
 
77
          void Execute(itk::Object *caller, const itk::EventObject &event){
 
78
                  itk::ProcessObject* po = dynamic_cast<itk::ProcessObject*>(caller);
 
79
                  if( !po || !m_pComando )
 
80
                          return;
 
81
 
 
82
                  if( typeid(event) == typeid ( itk::ProgressEvent)  )
 
83
                  {
 
84
                          try{
 
85
                                  if(!m_stop){
 
86
                                          if (!m_pComando->NotificarProgreso(po->GetProgress(),m_texto))
 
87
                                          {
 
88
                                                  po->SetAbortGenerateData(true);
 
89
                                          }
 
90
                                  }
 
91
                          }catch(std::exception& /*ex*/){
 
92
                                  po->SetAbortGenerateData(true);
 
93
                                  return;
 
94
                          }
 
95
                  }
 
96
          }
 
97
 
 
98
          void Execute(const itk::Object *caller, const itk::EventObject &event){
 
99
                  itk::ProcessObject* po = dynamic_cast<itk::ProcessObject*>( const_cast<itk::Object*>(caller));
 
100
 
 
101
                  if( !po || !m_pComando ) return;
 
102
 
 
103
                  if( typeid(event) == typeid ( itk::ProgressEvent)  )
 
104
                  {
 
105
                          try{
 
106
                                  if(!m_stop){
 
107
                                          if (!m_pComando->NotificarProgreso(po->GetProgress(),m_texto))
 
108
                                          {
 
109
                                                  po->SetAbortGenerateData(true);
 
110
                                          }
 
111
                                  }
 
112
                          }catch(std::exception& /*ex*/){
 
113
                                  po->SetAbortGenerateData(true);
 
114
                                  return;
 
115
                          }
 
116
                  }
 
117
          }
 
118
 
 
119
          void SetCommand (GNC::GCS::IComando* cmd)
 
120
          {
 
121
                  m_pComando=cmd;
 
122
          }
 
123
 
 
124
          void SetTexto  (std::string str)
 
125
          {
 
126
                  m_texto=str;
 
127
          }
 
128
 
 
129
         protected:
 
130
                 ChromaItkProgressCallback(){
 
131
                        m_pComando=NULL;
 
132
                        m_stop=false;
 
133
                 }
 
134
 
 
135
                 ~ChromaItkProgressCallback(){
 
136
                        m_pComando=NULL;
 
137
                 }
 
138
 
 
139
         private:
 
140
                GNC::GCS::IComando* m_pComando;
 
141
                std::string m_texto;
 
142
                bool m_stop;
 
143
        };
 
144
 
 
145
        ////////////////////////////////////////////////////////////
 
146
        ComandoChromaParams::ComandoChromaParams(GNC::GCS::IElementoCache* pImagen,
 
147
                vtkSmartPointer<vtkImageData> pMapaValoracion,
 
148
                GADAPI::Chroma::TipoAccion tipoAccion,
 
149
                wxCriticalSection* pMEF_CriticalSection,
 
150
                INotificadorChroma*  pNotificador,
 
151
                float sensibilidad,
 
152
                GADAPI::Chroma::TipoAmbito tipoAmbito)
 
153
        {
 
154
                m_pImagen = pImagen;
 
155
                m_pMapaValoracion = pMapaValoracion;
 
156
                m_pNotificador=pNotificador;
 
157
                m_sensibilidad=sensibilidad;
 
158
                m_pMEFCriticalSection=pMEF_CriticalSection;
 
159
 
 
160
                GTRACE(">> ComandoChromaParams::ComandoChromaParams(): " << this);
 
161
                m_TipoAccion = tipoAccion;
 
162
                m_TipoAmbito = tipoAmbito;
 
163
                m_pVista = NULL;
 
164
 
 
165
                x = 0;
 
166
                y = 0;
 
167
                GTRACE("<< ComandoChromaParams::ComandoChromaParams(): " << this);
 
168
        }
 
169
 
 
170
        ComandoChromaParams::ComandoChromaParams( GNC::GCS::IElementoCache* pImagen,
 
171
                vtkSmartPointer<vtkImageData> pMapaValoracion,
 
172
                wxCriticalSection* pMEF_CriticalSection,
 
173
                INotificadorChroma*  pNotificador,
 
174
                const TVerticesLazo& vertices, vtkGinkgoImageViewer* pVista) 
 
175
        {
 
176
                m_pImagen = pImagen;
 
177
                m_pMapaValoracion = pMapaValoracion;
 
178
                m_pNotificador=pNotificador;
 
179
                m_sensibilidad=0.0f;
 
180
                m_pMEFCriticalSection=pMEF_CriticalSection;
 
181
 
 
182
                GTRACE(">> ComandoChromaParams::ComandoChromaParams(): " << this);
 
183
                m_TipoAccion = GADAPI::Chroma::TAC_SegmentarLazo;
 
184
                m_Vertices = vertices;
 
185
                m_pVista = pVista;
 
186
 
 
187
                x = 0;
 
188
                y = 0;
 
189
                GTRACE("<< ComandoChromaParams::ComandoChromaParams(): " << this);
 
190
        }
 
191
 
 
192
        ComandoChromaParams::~ComandoChromaParams()
 
193
        {
 
194
                m_pMEFCriticalSection = NULL;
 
195
                m_pNotificador = NULL;
 
196
                m_pVista = NULL;
 
197
        }
 
198
        //////////////////////////////////////////////////////////////
 
199
 
 
200
        ComandoChroma::ComandoChroma(ComandoChromaParams* pParams) : IComando(pParams)
 
201
        {
 
202
                GTRACE(">> ComandoChroma::ComandoChroma(): " << this);
 
203
                m_pChromaParams = pParams;
 
204
                switch (m_pChromaParams->m_TipoAccion) {
 
205
                        case GADAPI::Chroma::TAC_Resetear :
 
206
                                SetId(IDC_CHROMA_RESETEAR);
 
207
                                CancelaA(IDC_CHROMA_SEGMENTAR_PUNTO);
 
208
                                CancelaA(IDC_CHROMA_SEGMENTAR_LAZO);
 
209
                                CancelaA(IDC_CHROMA_INSERTAR_REGION);
 
210
                                CancelaA(IDC_CHROMA_ELIMINAR_REGION);
 
211
                                CancelaA(IDC_CHROMA_TODO);
 
212
                                CancelaA(IDC_CHROMA_INVERTIR);
 
213
                                AbortaSi(IDC_CHROMA_RESETEAR);
 
214
                                break;
 
215
                        case GADAPI::Chroma::TAC_Todo :
 
216
                                SetId(IDC_CHROMA_TODO);
 
217
                                CancelaA(IDC_CHROMA_SEGMENTAR_PUNTO);
 
218
                                CancelaA(IDC_CHROMA_SEGMENTAR_LAZO);
 
219
                                CancelaA(IDC_CHROMA_INSERTAR_REGION);
 
220
                                CancelaA(IDC_CHROMA_ELIMINAR_REGION);
 
221
                                CancelaA(IDC_CHROMA_INVERTIR);
 
222
                                CancelaA(IDC_CHROMA_RESETEAR);
 
223
                                AbortaSi(IDC_CHROMA_TODO);
 
224
                                break;
 
225
                        case GADAPI::Chroma::TAC_Invertir :
 
226
                                SetId(IDC_CHROMA_INVERTIR);
 
227
                                EsperaA(IDC_CHROMA_INSERTAR_REGION);
 
228
                                EsperaA(IDC_CHROMA_ELIMINAR_REGION);
 
229
                                EsperaA(IDC_CHROMA_INVERTIR);
 
230
                                EsperaA(IDC_CHROMA_TODO);
 
231
                                EsperaA(IDC_CHROMA_RESETEAR);
 
232
                                EsperaA(IDC_CHROMA_INVERTIR);
 
233
                                break;
 
234
                        case GADAPI::Chroma::TAC_SegmentarPunto :
 
235
                                SetId(IDC_CHROMA_SEGMENTAR_PUNTO);
 
236
                                EsperaA(IDC_CHROMA_CANCELAR_SEGMENTACION);
 
237
                                EsperaA(IDC_CHROMA_INSERTAR_REGION);
 
238
                                EsperaA(IDC_CHROMA_ELIMINAR_REGION);
 
239
                                EsperaA(IDC_CHROMA_INVERTIR);
 
240
                                EsperaA(IDC_CHROMA_TODO);
 
241
                                CancelaA(IDC_CHROMA_SEGMENTAR_PUNTO);
 
242
                                CancelaA(IDC_CHROMA_SEGMENTAR_LAZO);
 
243
                                break;
 
244
                        case GADAPI::Chroma::TAC_CancelarSegmentacion :
 
245
                                SetId(IDC_CHROMA_CANCELAR_SEGMENTACION);
 
246
                                CancelaA(IDC_CHROMA_SEGMENTAR_PUNTO);
 
247
                                CancelaA(IDC_CHROMA_SEGMENTAR_LAZO);
 
248
                                AbortaSi(IDC_CHROMA_CANCELAR_SEGMENTACION);
 
249
                                break;
 
250
                        case GADAPI::Chroma::TAC_InsertarRegion :
 
251
                                SetId(IDC_CHROMA_INSERTAR_REGION);
 
252
                                EsperaA(IDC_CHROMA_RESETEAR);
 
253
                                EsperaA(IDC_CHROMA_SEGMENTAR_PUNTO);
 
254
                                EsperaA(IDC_CHROMA_SEGMENTAR_LAZO);
 
255
                                AbortaSi(IDC_CHROMA_CANCELAR_SEGMENTACION);
 
256
                                CancelaA(IDC_CHROMA_ELIMINAR_REGION);
 
257
                                CancelaA(IDC_CHROMA_INSERTAR_REGION);
 
258
                                EsperaA(IDC_CHROMA_TODO);
 
259
                                EsperaA(IDC_CHROMA_INVERTIR);
 
260
                                break;
 
261
                        case GADAPI::Chroma::TAC_EliminarRegion :
 
262
                                SetId(IDC_CHROMA_ELIMINAR_REGION);
 
263
                                AbortaSi(IDC_CHROMA_RESETEAR);
 
264
                                EsperaA(IDC_CHROMA_SEGMENTAR_PUNTO);
 
265
                                EsperaA(IDC_CHROMA_SEGMENTAR_LAZO);
 
266
                                AbortaSi(IDC_CHROMA_CANCELAR_SEGMENTACION);
 
267
                                CancelaA(IDC_CHROMA_INSERTAR_REGION);
 
268
                                CancelaA(IDC_CHROMA_ELIMINAR_REGION);
 
269
                                AbortaSi(IDC_CHROMA_TODO);
 
270
                                EsperaA(IDC_CHROMA_INVERTIR);
 
271
                                break;
 
272
                        case GADAPI::Chroma::TAC_SegmentarLazo :
 
273
                                SetId(IDC_CHROMA_SEGMENTAR_LAZO);
 
274
                                EsperaA(IDC_CHROMA_CANCELAR_SEGMENTACION);
 
275
                                EsperaA(IDC_CHROMA_INSERTAR_REGION);
 
276
                                EsperaA(IDC_CHROMA_ELIMINAR_REGION);
 
277
                                EsperaA(IDC_CHROMA_INVERTIR);
 
278
                                EsperaA(IDC_CHROMA_TODO);
 
279
                                CancelaA(IDC_CHROMA_SEGMENTAR_PUNTO);
 
280
                                CancelaA(IDC_CHROMA_SEGMENTAR_LAZO);
 
281
                                break;
 
282
                }
 
283
                GTRACE("<< ComandoChroma::ComandoChroma(): " << this);
 
284
        }
 
285
 
 
286
        void ComandoChroma::Execute()
 
287
        {
 
288
                std::string tarea;
 
289
 
 
290
                //------------------RESETEAR=>TODO NO CHROMA-----------------------------//
 
291
                if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Resetear) {
 
292
                        wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
293
 
 
294
                        const unsigned char mascara = MASCARA_UNSET_CHROMA & MASCARA_UNSET_PRESELECCION;//un cero en el chroma y en la preseleccion
 
295
                        unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
 
296
                        unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
 
297
                        for (unsigned int off = 0; off < numberOfTuples; ++off) {
 
298
                                data[off] =  data[off] & mascara; //se pone
 
299
                        }
 
300
 
 
301
                        delete pLockerChroma;
 
302
                }
 
303
                //------------------TODO CHROMA-----------------------------//
 
304
                else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Todo) {
 
305
                        wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
306
 
 
307
                        unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
 
308
                        unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
 
309
                        for (unsigned int off = 0; off < numberOfTuples; ++off) {
 
310
                                data[off] =  (data[off] | MASCARA_SET_CHROMA) & MASCARA_UNSET_PRESELECCION; //se pone
 
311
                        }
 
312
 
 
313
                        delete pLockerChroma;
 
314
                }
 
315
                //------------------INVERTIR-----------------------------//
 
316
                else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Invertir) {
 
317
                        wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
318
 
 
319
                        unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
 
320
                        unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
 
321
                        for (unsigned int off = 0; off < numberOfTuples; ++off) {
 
322
                                data[off] &= MASCARA_UNSET_PRESELECCION;
 
323
                                if(data[off] < 0x80) { //no enmascarado
 
324
                                        data[off] |= MASCARA_SET_CHROMA;
 
325
                                } else {
 
326
                                        data[off] &= MASCARA_UNSET_CHROMA;
 
327
                                }
 
328
                        }
 
329
 
 
330
                        delete pLockerChroma;
 
331
                }
 
332
                //------------------SEGMENTAR PUNTO-----------------------------//
 
333
                else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_SegmentarPunto) {
 
334
                        if(m_pChromaParams->m_TipoAmbito==GADAPI::Chroma::TAC_Contiguo){
 
335
                                tarea = _Std("Segmenting Chroma Local ...");
 
336
                        }else{
 
337
                                tarea = _Std("Chroma Segmenting Global ...");
 
338
                        }
 
339
                        NotificarProgreso(0.0f, tarea);
 
340
                        //lo primero hacer el cast a I
 
341
                        GNC::GCS::IElementoCacheConcreto<float>* pImagen = dynamic_cast<GNC::GCS::IElementoCacheConcreto<float>*>(m_pChromaParams->m_pImagen);
 
342
                        if(pImagen == NULL){
 
343
                                GINKGOLOGERROR("Se ha recibido en el comando chroma-Segmentar punto una imagen que no es float");
 
344
                                return;
 
345
                        }
 
346
 
 
347
                        // ComprobaciĆ³n de limites
 
348
                        if (m_pChromaParams->x < 0 ||
 
349
                                m_pChromaParams->x >= pImagen->GetAncho() ||
 
350
                                        m_pChromaParams->y < 0 ||
 
351
                                        m_pChromaParams->y >= pImagen->GetAlto()) {
 
352
 
 
353
                                #ifdef _GINKGO_DEBUG
 
354
                                        std::cerr << "Punto fuera de lĆ­mites: " << m_pChromaParams->x << ", " << m_pChromaParams->y << std::endl;
 
355
                                        #endif
 
356
                                return;
 
357
 
 
358
                        }
 
359
 
 
360
                        //se crea la imagen itk
 
361
                        typedef float TPixelValoracion;
 
362
                        typedef unsigned char TPixelSalida;
 
363
                        typedef itk::Image< TPixelValoracion, 2 > TImagenValoracion;
 
364
                        typedef itk::Image< TPixelSalida, 2 > TImagenSalida;
 
365
                        typedef itk::ImageRegionIterator<TImagenValoracion> TIteradorValoracion;
 
366
                        typedef itk::ImageRegionIterator<TImagenSalida> TIteradorSalida;
 
367
 
 
368
                        typedef itk::ImageFileWriter<TImagenSalida> TEscritor;
 
369
 
 
370
                        TImagenValoracion::Pointer imagenValoracion = TImagenValoracion::New();
 
371
                        //se copia el pixeldata
 
372
                        TImagenValoracion::IndexType start;
 
373
                        start[0] = 0; // first index on X
 
374
                        start[1] = 0; // first index on Y
 
375
 
 
376
                        TImagenValoracion::SizeType size;
 
377
 
 
378
                        size[0] = pImagen->GetAncho();
 
379
                        size[1] = pImagen->GetAlto();
 
380
 
 
381
                        TImagenValoracion::RegionType region;
 
382
                        region.SetSize(size);
 
383
                        region.SetIndex(start);
 
384
                        imagenValoracion->SetRegions(region);
 
385
                        imagenValoracion->Allocate();
 
386
 
 
387
                        TIteradorValoracion it(imagenValoracion,imagenValoracion->GetLargestPossibleRegion());
 
388
                        unsigned int off = 0;
 
389
                        float* data = pImagen->GetPixelData();
 
390
                        for (it.GoToBegin(); !it.IsAtEnd(); ++it) {
 
391
                                it.Set(data[off++]);
 
392
                        }
 
393
 
 
394
                        GTRACE("ComandoChroma::Execute(): Semilla: " << m_pChromaParams->x << ", " << m_pChromaParams->y);
 
395
 
 
396
                        if(m_pChromaParams->m_TipoAmbito==GADAPI::Chroma::TAC_Contiguo){
 
397
                                typedef itk::ConnectedThresholdImageFilter< TImagenValoracion, TImagenSalida > TConnectedFilter;
 
398
 
 
399
                                TConnectedFilter::Pointer segmentador = TConnectedFilter::New();
 
400
                                segmentador->SetInput(imagenValoracion);
 
401
 
 
402
                                TConnectedFilter::InputImageType::IndexType index;
 
403
                                index[0] = m_pChromaParams->x;
 
404
                                index[1] = m_pChromaParams->y;
 
405
                                TConnectedFilter::InputImageType::PixelType threshold = imagenValoracion->GetPixel(index);
 
406
 
 
407
                                const float nivelConfianza = (1.0f - m_pChromaParams->m_sensibilidad) / 2.0f;
 
408
 
 
409
                                // Itervalo de Confianza del threshold%
 
410
                                ChromaItkProgressCallback::Pointer cb = ChromaItkProgressCallback::New();
 
411
                                cb->SetTexto(tarea);
 
412
                                cb->SetCommand(this);
 
413
 
 
414
                                segmentador->SetLower(std::max(0.0f, threshold - nivelConfianza ) );
 
415
                                segmentador->SetUpper(std::min(1.0f, threshold + nivelConfianza ) );
 
416
                                segmentador->SetReplaceValue(255);
 
417
                                segmentador->SetSeed(index);
 
418
 
 
419
                                segmentador->AddObserver (itk::ProgressEvent(), cb);
 
420
 
 
421
                                segmentador->Update();
 
422
                                if (segmentador->GetAbortGenerateData()) {
 
423
                                        segmentador->ResetPipeline();
 
424
                                        return;
 
425
                                }
 
426
 
 
427
        #ifdef DUMP_IMAGENES
 
428
                                {
 
429
                                        try{
 
430
                                                TEscritor::Pointer writer = TEscritor::New();
 
431
                                                writer->SetInput(segmentador->GetOutput());
 
432
                                                writer->SetFileName("imagenItk.jpg");
 
433
                                                writer->Update();
 
434
                                        } catch (itk::ExceptionObject ex) {
 
435
                                                std::cerr << ex.GetDescription() << std::endl;
 
436
                                        }
 
437
                                }
 
438
        #endif
 
439
 
 
440
 
 
441
                                TIteradorSalida it2(segmentador->GetOutput(), segmentador->GetOutput()->GetLargestPossibleRegion());
 
442
 
 
443
                                wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
444
 
 
445
                                unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
 
446
                                unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
 
447
                                it2.GoToBegin();
 
448
                                for (unsigned int off = 0; off < numberOfTuples && !it2.IsAtEnd(); ++it2,++off) {
 
449
                                        if(it2.Get() == 0) { //no preseleccionado
 
450
                                                data[off] &= MASCARA_UNSET_PRESELECCION;
 
451
                                        } else {
 
452
                                                data[off] |= MASCARA_SET_PRESELECCION;
 
453
                                        }
 
454
                                }
 
455
                                delete pLockerChroma;
 
456
                        }
 
457
                        /////////////////////////GLOBAL//////////////////////////
 
458
                        else{
 
459
                                typedef itk::ThresholdImageFilter< TImagenValoracion > TThresholdFilter;
 
460
                                        TThresholdFilter::Pointer segmentador = TThresholdFilter::New();
 
461
                                        segmentador->SetInput(imagenValoracion);
 
462
 
 
463
                                        TThresholdFilter::InputImageType::IndexType index;
 
464
                                        index[0] = m_pChromaParams->x;
 
465
                                        index[1] = m_pChromaParams->y;
 
466
                                        TThresholdFilter::InputImageType::PixelType threshold = imagenValoracion->GetPixel(index);
 
467
 
 
468
                                        const float nivelConfianza = (1.0f - m_pChromaParams->m_sensibilidad) / 2.0f;
 
469
 
 
470
                                        // Itervalo de Confianza del threshold
 
471
                                        ChromaItkProgressCallback::Pointer cb = ChromaItkProgressCallback::New();
 
472
                                        cb->SetTexto(tarea);
 
473
                                        cb->SetCommand(this);
 
474
 
 
475
                                        segmentador->SetLower(std::max(0.0f, threshold - nivelConfianza ) );
 
476
                                        segmentador->SetUpper(std::min(1.0f, threshold + nivelConfianza ) );
 
477
 
 
478
                                        segmentador->AddObserver (itk::ProgressEvent(), cb);
 
479
 
 
480
                                        segmentador->Update();
 
481
                                        if (segmentador->GetAbortGenerateData()) {
 
482
                                                segmentador->ResetPipeline();
 
483
                                                //delete pLockerChroma;
 
484
                                                return;
 
485
                                        }
 
486
 
 
487
 
 
488
                #ifdef DUMP_IMAGENES
 
489
                                        try{
 
490
                                                itk::ImageFileWriter<TThresholdFilter::OutputImageType>::Pointer writer = itk::ImageFileWriter<TThresholdFilter::OutputImageType>::New();
 
491
 
 
492
                                                writer->SetInput(segmentador->GetOutput());
 
493
                                                writer->SetFileName("segmentacion_global.bmp");
 
494
                                                writer->Update();
 
495
                                        } catch (itk::ExceptionObject ex) {
 
496
                                                std::cerr << ex.GetDescription() << std::endl;
 
497
                                        }
 
498
                #endif
 
499
 
 
500
                                        TIteradorValoracion it2(segmentador->GetOutput(), segmentador->GetOutput()->GetLargestPossibleRegion());
 
501
                                        wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
502
 
 
503
                                        unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
 
504
                                        unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
 
505
                                        it2.GoToBegin();
 
506
                                        const float noPreselecc = segmentador->GetOutsideValue();
 
507
                                        for (unsigned int off = 0; off < numberOfTuples && !it2.IsAtEnd(); ++it2,++off) {
 
508
                                                if(it2.Get() == noPreselecc) { //no preseleccionado
 
509
                                                        data[off] &= MASCARA_UNSET_PRESELECCION;
 
510
                                                } else {
 
511
                                                        data[off] |= MASCARA_SET_PRESELECCION;
 
512
                                                }
 
513
                                        }
 
514
                                        delete pLockerChroma;
 
515
                                }
 
516
 
 
517
                                NotificarProgreso(1.0f, tarea);
 
518
                }
 
519
                //------------------CANCELAR SEGMENTACION-----------------------------//
 
520
                else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_CancelarSegmentacion) {
 
521
                        wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
522
 
 
523
                        unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
 
524
                        unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
 
525
                        for (unsigned int off = 0; off < numberOfTuples; ++off) {
 
526
                                data[off] &= MASCARA_UNSET_PRESELECCION;
 
527
                        }
 
528
                        delete pLockerChroma;
 
529
                }
 
530
                //------------------INSERTAR REGION-----------------------------//
 
531
                else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_InsertarRegion) {
 
532
                        wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
533
 
 
534
                        unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
 
535
                        unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
 
536
                        for (unsigned int off = 0; off < numberOfTuples; ++off) {
 
537
                                if((data[off] & MASCARA_SET_PRESELECCION) != 0) { //preseleccionado
 
538
                                        data[off] |= MASCARA_SET_CHROMA;
 
539
                                        data[off] &= MASCARA_UNSET_PRESELECCION; //se quita preseleccion
 
540
                                }
 
541
                        }
 
542
                        delete pLockerChroma;
 
543
 
 
544
                }
 
545
                //------------------ELIMINAR REGION-----------------------------//
 
546
                else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_EliminarRegion) {
 
547
                        wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
548
 
 
549
                        unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
 
550
                        unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
 
551
                        unsigned char mascara = MASCARA_UNSET_CHROMA & MASCARA_UNSET_PRESELECCION; // se quita el chroma y la preseleccion
 
552
                        for (unsigned int off = 0; off < numberOfTuples; ++off) {
 
553
                                if((data[off] & MASCARA_SET_PRESELECCION) != 0) { //preseleccionado
 
554
                                        data[off] &= mascara;
 
555
                                }
 
556
                        }
 
557
                        delete pLockerChroma;
 
558
                }
 
559
                //------------------SEGMENTAR LAZO-----------------------------//
 
560
                else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_SegmentarLazo) {
 
561
 
 
562
                        const std::string tarea = _Std("Extracting cut ...");
 
563
                        NotificarProgreso(0.0f, tarea);
 
564
                        const int* dims;
 
565
                        const double* origin;
 
566
                        const double* spacing;
 
567
 
 
568
                        GNC::GCS::Vector BoundingBoxROI[2];
 
569
                        GNC::GCS::Vector BoundingBoxTriangulo[2];
 
570
 
 
571
                        ComandoChromaParams::TVerticesLazo vertices = m_pChromaParams->m_Vertices;
 
572
 
 
573
                        GNC::GCS::Vector pv; // Vector de posicion en la imagen.
 
574
 
 
575
                        wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
576
 
 
577
                        spacing = m_pChromaParams->m_pMapaValoracion->GetSpacing();
 
578
                        origin  = m_pChromaParams->m_pMapaValoracion->GetOrigin();
 
579
                        dims    = m_pChromaParams->m_pMapaValoracion->GetDimensions();
 
580
 
 
581
                        if (dims[0] == 0 || dims[1] == 0 || vertices.size() < 3) {
 
582
                                delete pLockerChroma;
 
583
                                return;
 
584
                        }
 
585
 
 
586
                        unsigned char* scalars = (unsigned char*)(m_pChromaParams->m_pMapaValoracion->GetScalarPointer());
 
587
 
 
588
                        // Transformacion a coordenadas imagen e inicializacion del ROI (Bounding box del poligono)
 
589
 
 
590
                        {
 
591
                                GNC::GCS::Vector BoundingBoxImagen[2];
 
592
                                BoundingBoxImagen[1].Asignar(dims[0]-1, dims[1]-1);
 
593
 
 
594
                                ComandoChromaParams::TVerticesLazo::iterator nodo = vertices.begin();
 
595
                                {
 
596
                                        GNC::GCS::Nodo& n = (*nodo);
 
597
                                        m_pChromaParams->m_pVista->CoordenadasMundoACoordenadasImagen(n.x, n.y, n.x, n.y);
 
598
 
 
599
                                        n.IniciarBoundingBox(BoundingBoxROI );
 
600
                                }
 
601
 
 
602
                                for (nodo++; nodo != vertices.end(); nodo++) {
 
603
                                        GNC::GCS::Nodo& n = (*nodo);
 
604
                                        m_pChromaParams->m_pVista->CoordenadasMundoACoordenadasImagen(n.x, n.y, n.x, n.y);
 
605
                                        n.ExtenderBoundingBox(BoundingBoxROI );
 
606
                                }
 
607
                                BoundingBoxROI[0].Redondear();
 
608
                                BoundingBoxROI[1].Redondear();
 
609
 
 
610
                                GNC::GCS::Vector::IntersectarBoundingBox(BoundingBoxROI, BoundingBoxImagen, BoundingBoxROI);
 
611
                        }
 
612
 
 
613
                        ComandoChromaParams::TVerticesLazo::iterator nodo1 = vertices.begin();
 
614
                        ComandoChromaParams::TVerticesLazo::iterator nodo2 = nodo1;
 
615
                        nodo2++;
 
616
                        ComandoChromaParams::TVerticesLazo::iterator nodo3 = nodo2;
 
617
                        nodo3++;
 
618
 
 
619
                        const unsigned long stride = dims[0];
 
620
                        unsigned long x   = 0;
 
621
                        unsigned long y   = 0;
 
622
                        unsigned long ox  = 0;
 
623
 
 
624
                        //==============================================
 
625
                        #if defined(CURVAS_JORDAN) // Metodo del teorema de las curvas de jordan
 
626
 
 
627
                        if (GNC::GCS::Vector::BoundingBoxValido(BoundingBoxROI)) {
 
628
                                unsigned long ipx[2] = { BoundingBoxROI[0].x, BoundingBoxROI[1].x };
 
629
                                unsigned long ipy[2] = { BoundingBoxROI[0].y, BoundingBoxROI[1].y };
 
630
                                float step = 1.0f / (ipy[1] - ipy[0]);
 
631
                                float progress = 0.0f;
 
632
 
 
633
                                unsigned long off = (ipy[0] * dims[0]) + ipx[0];
 
634
 
 
635
                                for (y = ipy[0]; y <= ipy[1]; ++y) {
 
636
                                        ox = 0;
 
637
                                        pv.y = y;
 
638
                                        for (x = ipx[0]; x <= ipx[1]; ++x) {
 
639
                                                pv.x = x;
 
640
                                                if ( pv.DentroDePoligono2(vertices) ) {
 
641
                                                        scalars[off + (ox++)] |= MASCARA_SET_PRESELECCION;
 
642
                                                }
 
643
                                                else {
 
644
                                                        scalars[off + (ox++)] &= MASCARA_UNSET_PRESELECCION;
 
645
                                                }
 
646
                                        }
 
647
                                        off += stride;
 
648
                                        try {
 
649
                                                progress += step;
 
650
                                                NotificarProgreso(progress, tarea);
 
651
                                        }
 
652
                                        catch(std::exception& ex){
 
653
                                                delete pLockerChroma;
 
654
                                                throw ex;
 
655
                                        }
 
656
                                }
 
657
                        }
 
658
 
 
659
                        //==============================================
 
660
                        #else // Metodo de las curvas de jordan con optimizacion por triangulacion
 
661
 
 
662
                        float step = 1.0f / std::max(1.0f, (float)vertices.size() - 3.0f);
 
663
                        float progress = 0.0f;
 
664
                        while (nodo3 != vertices.end()) {
 
665
 
 
666
                                GNC::GCS::Vector triangulo[3] = { (*nodo1).Redondeado(), (*nodo2).Redondeado(), (*nodo3).Redondeado() };
 
667
 
 
668
                                triangulo[0].IniciarBoundingBox(BoundingBoxTriangulo);
 
669
                                triangulo[1].ExtenderBoundingBox(BoundingBoxTriangulo);
 
670
                                triangulo[2].ExtenderBoundingBox(BoundingBoxTriangulo);
 
671
 
 
672
                                GNC::GCS::Vector::IntersectarBoundingBox(BoundingBoxTriangulo, BoundingBoxROI, BoundingBoxTriangulo);
 
673
 
 
674
                                if ( GNC::GCS::Vector::BoundingBoxValido(BoundingBoxTriangulo) ) {
 
675
 
 
676
                                        unsigned long ipx[2] = { BoundingBoxTriangulo[0].x, BoundingBoxTriangulo[1].x };
 
677
                                        unsigned long ipy[2] = { BoundingBoxTriangulo[0].y, BoundingBoxTriangulo[1].y };
 
678
 
 
679
                                        unsigned long off = (ipy[0] * dims[0]) + ipx[0];
 
680
 
 
681
                                        for (y = ipy[0]; y <= ipy[1]; ++y) {
 
682
                                                ox = 0;
 
683
                                                pv.y = y;
 
684
                                                for (x = ipx[0]; x <= ipx[1]; ++x) {
 
685
                                                        pv.x = x;
 
686
                                                        if ( pv.DentroDePoligono2(triangulo, 3) ) {
 
687
                                                                if(( scalars[off + ox] & MASCARA_SET_PRESELECCION) == 0) {
 
688
                                                                        scalars[off + ox] |= MASCARA_SET_PRESELECCION;
 
689
                                                                } else {
 
690
                                                                        scalars[off + ox] &= MASCARA_UNSET_PRESELECCION;
 
691
                                                                }
 
692
                                                        }
 
693
                                                        ox++;
 
694
                                                }
 
695
                                                off += stride;
 
696
 
 
697
                                        }
 
698
                                }
 
699
                                try {
 
700
                                        progress += step;
 
701
                                        NotificarProgreso(progress, tarea);
 
702
                                }
 
703
                                catch(std::exception& ex){
 
704
                                        delete pLockerChroma;
 
705
                                        throw ex;
 
706
                                }
 
707
                                nodo2++;
 
708
                                nodo3++;
 
709
                        }
 
710
                        #endif
 
711
 
 
712
                        delete pLockerChroma;
 
713
                }
 
714
        }
 
715
 
 
716
        void ComandoChroma::Update()
 
717
        {
 
718
                wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
 
719
 
 
720
                if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Resetear ||
 
721
                        m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Todo ||
 
722
                        m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Invertir ||
 
723
                        m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_InsertarRegion ||
 
724
                        m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_EliminarRegion) {
 
725
                        m_pChromaParams->m_pMapaValoracion->Update();
 
726
                        if (m_pChromaParams->m_pNotificador != NULL) {
 
727
                                m_pChromaParams->m_pNotificador->ActualizarVistaChroma(true);
 
728
                        }
 
729
                }else if(m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_SegmentarPunto ||
 
730
                        m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_SegmentarLazo ||
 
731
                        m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_CancelarSegmentacion ){
 
732
                        if (m_pChromaParams->m_pNotificador != NULL) {
 
733
                                m_pChromaParams->m_pNotificador->ActualizarVistaChroma(false);
 
734
                        }
 
735
 
 
736
                }
 
737
                delete pLockerChroma;
 
738
 
 
739
        }
 
740
 
 
741
}