3
* $Id: comandochroma.cpp 3527 2011-03-16 21:45:37Z carlos $
6
* Copyright 2008-10 MetaEmotion S.L. All rights reserved.
7
* http://ginkgo-cadx.com
9
* This file is licensed under LGPL v3 license.
10
* See License.txt for details
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>
22
#include <wx/thread.h>
23
#include <api/internacionalizacion.h>
29
#include <itkImageRegionIterator.h>
30
#include <itkConnectedThresholdImageFilter.h>
31
#include <itkThresholdImageFilter.h>
33
#include <itkBinaryThresholdImageFilter.h>
34
#include <itkNumericTraits.h>
35
#include <itkImageFileWriter.h>
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>
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
59
// Singleton de persistencia
62
//callback de itk para el progreso
63
class ChromaItkProgressCallback : public itk::Command
67
typedef ChromaItkProgressCallback Self;
68
typedef itk::Command Superclass;
70
typedef itk::SmartPointer<Self> Pointer;
71
typedef itk::SmartPointer<const Self> ConstPointer;
73
itkTypeMacro (ChromaItkProgressCallback, itk::Command);
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 )
82
if( typeid(event) == typeid ( itk::ProgressEvent) )
86
if (!m_pComando->NotificarProgreso(po->GetProgress(),m_texto))
88
po->SetAbortGenerateData(true);
91
}catch(std::exception& /*ex*/){
92
po->SetAbortGenerateData(true);
98
void Execute(const itk::Object *caller, const itk::EventObject &event){
99
itk::ProcessObject* po = dynamic_cast<itk::ProcessObject*>( const_cast<itk::Object*>(caller));
101
if( !po || !m_pComando ) return;
103
if( typeid(event) == typeid ( itk::ProgressEvent) )
107
if (!m_pComando->NotificarProgreso(po->GetProgress(),m_texto))
109
po->SetAbortGenerateData(true);
112
}catch(std::exception& /*ex*/){
113
po->SetAbortGenerateData(true);
119
void SetCommand (GNC::GCS::IComando* cmd)
124
void SetTexto (std::string str)
130
ChromaItkProgressCallback(){
135
~ChromaItkProgressCallback(){
140
GNC::GCS::IComando* m_pComando;
145
////////////////////////////////////////////////////////////
146
ComandoChromaParams::ComandoChromaParams(GNC::GCS::IElementoCache* pImagen,
147
vtkSmartPointer<vtkImageData> pMapaValoracion,
148
GADAPI::Chroma::TipoAccion tipoAccion,
149
wxCriticalSection* pMEF_CriticalSection,
150
INotificadorChroma* pNotificador,
152
GADAPI::Chroma::TipoAmbito tipoAmbito)
155
m_pMapaValoracion = pMapaValoracion;
156
m_pNotificador=pNotificador;
157
m_sensibilidad=sensibilidad;
158
m_pMEFCriticalSection=pMEF_CriticalSection;
160
GTRACE(">> ComandoChromaParams::ComandoChromaParams(): " << this);
161
m_TipoAccion = tipoAccion;
162
m_TipoAmbito = tipoAmbito;
167
GTRACE("<< ComandoChromaParams::ComandoChromaParams(): " << this);
170
ComandoChromaParams::ComandoChromaParams( GNC::GCS::IElementoCache* pImagen,
171
vtkSmartPointer<vtkImageData> pMapaValoracion,
172
wxCriticalSection* pMEF_CriticalSection,
173
INotificadorChroma* pNotificador,
174
const TVerticesLazo& vertices, vtkGinkgoImageViewer* pVista)
177
m_pMapaValoracion = pMapaValoracion;
178
m_pNotificador=pNotificador;
180
m_pMEFCriticalSection=pMEF_CriticalSection;
182
GTRACE(">> ComandoChromaParams::ComandoChromaParams(): " << this);
183
m_TipoAccion = GADAPI::Chroma::TAC_SegmentarLazo;
184
m_Vertices = vertices;
189
GTRACE("<< ComandoChromaParams::ComandoChromaParams(): " << this);
192
ComandoChromaParams::~ComandoChromaParams()
194
m_pMEFCriticalSection = NULL;
195
m_pNotificador = NULL;
198
//////////////////////////////////////////////////////////////
200
ComandoChroma::ComandoChroma(ComandoChromaParams* pParams) : IComando(pParams)
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);
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);
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);
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);
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);
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);
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);
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);
283
GTRACE("<< ComandoChroma::ComandoChroma(): " << this);
286
void ComandoChroma::Execute()
290
//------------------RESETEAR=>TODO NO CHROMA-----------------------------//
291
if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Resetear) {
292
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
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
301
delete pLockerChroma;
303
//------------------TODO CHROMA-----------------------------//
304
else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Todo) {
305
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
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
313
delete pLockerChroma;
315
//------------------INVERTIR-----------------------------//
316
else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_Invertir) {
317
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
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;
326
data[off] &= MASCARA_UNSET_CHROMA;
330
delete pLockerChroma;
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 ...");
337
tarea = _Std("Chroma Segmenting Global ...");
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);
343
GINKGOLOGERROR("Se ha recibido en el comando chroma-Segmentar punto una imagen que no es float");
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()) {
354
std::cerr << "Punto fuera de lĆmites: " << m_pChromaParams->x << ", " << m_pChromaParams->y << std::endl;
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;
368
typedef itk::ImageFileWriter<TImagenSalida> TEscritor;
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
376
TImagenValoracion::SizeType size;
378
size[0] = pImagen->GetAncho();
379
size[1] = pImagen->GetAlto();
381
TImagenValoracion::RegionType region;
382
region.SetSize(size);
383
region.SetIndex(start);
384
imagenValoracion->SetRegions(region);
385
imagenValoracion->Allocate();
387
TIteradorValoracion it(imagenValoracion,imagenValoracion->GetLargestPossibleRegion());
388
unsigned int off = 0;
389
float* data = pImagen->GetPixelData();
390
for (it.GoToBegin(); !it.IsAtEnd(); ++it) {
394
GTRACE("ComandoChroma::Execute(): Semilla: " << m_pChromaParams->x << ", " << m_pChromaParams->y);
396
if(m_pChromaParams->m_TipoAmbito==GADAPI::Chroma::TAC_Contiguo){
397
typedef itk::ConnectedThresholdImageFilter< TImagenValoracion, TImagenSalida > TConnectedFilter;
399
TConnectedFilter::Pointer segmentador = TConnectedFilter::New();
400
segmentador->SetInput(imagenValoracion);
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);
407
const float nivelConfianza = (1.0f - m_pChromaParams->m_sensibilidad) / 2.0f;
409
// Itervalo de Confianza del threshold%
410
ChromaItkProgressCallback::Pointer cb = ChromaItkProgressCallback::New();
412
cb->SetCommand(this);
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);
419
segmentador->AddObserver (itk::ProgressEvent(), cb);
421
segmentador->Update();
422
if (segmentador->GetAbortGenerateData()) {
423
segmentador->ResetPipeline();
430
TEscritor::Pointer writer = TEscritor::New();
431
writer->SetInput(segmentador->GetOutput());
432
writer->SetFileName("imagenItk.jpg");
434
} catch (itk::ExceptionObject ex) {
435
std::cerr << ex.GetDescription() << std::endl;
441
TIteradorSalida it2(segmentador->GetOutput(), segmentador->GetOutput()->GetLargestPossibleRegion());
443
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
445
unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
446
unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
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;
452
data[off] |= MASCARA_SET_PRESELECCION;
455
delete pLockerChroma;
457
/////////////////////////GLOBAL//////////////////////////
459
typedef itk::ThresholdImageFilter< TImagenValoracion > TThresholdFilter;
460
TThresholdFilter::Pointer segmentador = TThresholdFilter::New();
461
segmentador->SetInput(imagenValoracion);
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);
468
const float nivelConfianza = (1.0f - m_pChromaParams->m_sensibilidad) / 2.0f;
470
// Itervalo de Confianza del threshold
471
ChromaItkProgressCallback::Pointer cb = ChromaItkProgressCallback::New();
473
cb->SetCommand(this);
475
segmentador->SetLower(std::max(0.0f, threshold - nivelConfianza ) );
476
segmentador->SetUpper(std::min(1.0f, threshold + nivelConfianza ) );
478
segmentador->AddObserver (itk::ProgressEvent(), cb);
480
segmentador->Update();
481
if (segmentador->GetAbortGenerateData()) {
482
segmentador->ResetPipeline();
483
//delete pLockerChroma;
490
itk::ImageFileWriter<TThresholdFilter::OutputImageType>::Pointer writer = itk::ImageFileWriter<TThresholdFilter::OutputImageType>::New();
492
writer->SetInput(segmentador->GetOutput());
493
writer->SetFileName("segmentacion_global.bmp");
495
} catch (itk::ExceptionObject ex) {
496
std::cerr << ex.GetDescription() << std::endl;
500
TIteradorValoracion it2(segmentador->GetOutput(), segmentador->GetOutput()->GetLargestPossibleRegion());
501
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
503
unsigned char * data = (unsigned char*)m_pChromaParams->m_pMapaValoracion->GetScalarPointer();
504
unsigned int numberOfTuples = m_pChromaParams->m_pMapaValoracion->GetPointData()->GetScalars()->GetNumberOfTuples();
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;
511
data[off] |= MASCARA_SET_PRESELECCION;
514
delete pLockerChroma;
517
NotificarProgreso(1.0f, tarea);
519
//------------------CANCELAR SEGMENTACION-----------------------------//
520
else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_CancelarSegmentacion) {
521
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
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;
528
delete pLockerChroma;
530
//------------------INSERTAR REGION-----------------------------//
531
else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_InsertarRegion) {
532
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
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
542
delete pLockerChroma;
545
//------------------ELIMINAR REGION-----------------------------//
546
else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_EliminarRegion) {
547
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
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;
557
delete pLockerChroma;
559
//------------------SEGMENTAR LAZO-----------------------------//
560
else if (m_pChromaParams->m_TipoAccion == GADAPI::Chroma::TAC_SegmentarLazo) {
562
const std::string tarea = _Std("Extracting cut ...");
563
NotificarProgreso(0.0f, tarea);
565
const double* origin;
566
const double* spacing;
568
GNC::GCS::Vector BoundingBoxROI[2];
569
GNC::GCS::Vector BoundingBoxTriangulo[2];
571
ComandoChromaParams::TVerticesLazo vertices = m_pChromaParams->m_Vertices;
573
GNC::GCS::Vector pv; // Vector de posicion en la imagen.
575
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
577
spacing = m_pChromaParams->m_pMapaValoracion->GetSpacing();
578
origin = m_pChromaParams->m_pMapaValoracion->GetOrigin();
579
dims = m_pChromaParams->m_pMapaValoracion->GetDimensions();
581
if (dims[0] == 0 || dims[1] == 0 || vertices.size() < 3) {
582
delete pLockerChroma;
586
unsigned char* scalars = (unsigned char*)(m_pChromaParams->m_pMapaValoracion->GetScalarPointer());
588
// Transformacion a coordenadas imagen e inicializacion del ROI (Bounding box del poligono)
591
GNC::GCS::Vector BoundingBoxImagen[2];
592
BoundingBoxImagen[1].Asignar(dims[0]-1, dims[1]-1);
594
ComandoChromaParams::TVerticesLazo::iterator nodo = vertices.begin();
596
GNC::GCS::Nodo& n = (*nodo);
597
m_pChromaParams->m_pVista->CoordenadasMundoACoordenadasImagen(n.x, n.y, n.x, n.y);
599
n.IniciarBoundingBox(BoundingBoxROI );
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 );
607
BoundingBoxROI[0].Redondear();
608
BoundingBoxROI[1].Redondear();
610
GNC::GCS::Vector::IntersectarBoundingBox(BoundingBoxROI, BoundingBoxImagen, BoundingBoxROI);
613
ComandoChromaParams::TVerticesLazo::iterator nodo1 = vertices.begin();
614
ComandoChromaParams::TVerticesLazo::iterator nodo2 = nodo1;
616
ComandoChromaParams::TVerticesLazo::iterator nodo3 = nodo2;
619
const unsigned long stride = dims[0];
622
unsigned long ox = 0;
624
//==============================================
625
#if defined(CURVAS_JORDAN) // Metodo del teorema de las curvas de jordan
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;
633
unsigned long off = (ipy[0] * dims[0]) + ipx[0];
635
for (y = ipy[0]; y <= ipy[1]; ++y) {
638
for (x = ipx[0]; x <= ipx[1]; ++x) {
640
if ( pv.DentroDePoligono2(vertices) ) {
641
scalars[off + (ox++)] |= MASCARA_SET_PRESELECCION;
644
scalars[off + (ox++)] &= MASCARA_UNSET_PRESELECCION;
650
NotificarProgreso(progress, tarea);
652
catch(std::exception& ex){
653
delete pLockerChroma;
659
//==============================================
660
#else // Metodo de las curvas de jordan con optimizacion por triangulacion
662
float step = 1.0f / std::max(1.0f, (float)vertices.size() - 3.0f);
663
float progress = 0.0f;
664
while (nodo3 != vertices.end()) {
666
GNC::GCS::Vector triangulo[3] = { (*nodo1).Redondeado(), (*nodo2).Redondeado(), (*nodo3).Redondeado() };
668
triangulo[0].IniciarBoundingBox(BoundingBoxTriangulo);
669
triangulo[1].ExtenderBoundingBox(BoundingBoxTriangulo);
670
triangulo[2].ExtenderBoundingBox(BoundingBoxTriangulo);
672
GNC::GCS::Vector::IntersectarBoundingBox(BoundingBoxTriangulo, BoundingBoxROI, BoundingBoxTriangulo);
674
if ( GNC::GCS::Vector::BoundingBoxValido(BoundingBoxTriangulo) ) {
676
unsigned long ipx[2] = { BoundingBoxTriangulo[0].x, BoundingBoxTriangulo[1].x };
677
unsigned long ipy[2] = { BoundingBoxTriangulo[0].y, BoundingBoxTriangulo[1].y };
679
unsigned long off = (ipy[0] * dims[0]) + ipx[0];
681
for (y = ipy[0]; y <= ipy[1]; ++y) {
684
for (x = ipx[0]; x <= ipx[1]; ++x) {
686
if ( pv.DentroDePoligono2(triangulo, 3) ) {
687
if(( scalars[off + ox] & MASCARA_SET_PRESELECCION) == 0) {
688
scalars[off + ox] |= MASCARA_SET_PRESELECCION;
690
scalars[off + ox] &= MASCARA_UNSET_PRESELECCION;
701
NotificarProgreso(progress, tarea);
703
catch(std::exception& ex){
704
delete pLockerChroma;
712
delete pLockerChroma;
716
void ComandoChroma::Update()
718
wxCriticalSectionLocker* pLockerChroma = new wxCriticalSectionLocker(*(m_pChromaParams->m_pMEFCriticalSection));
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);
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);
737
delete pLockerChroma;