]> Creatis software - creaVtk.git/commitdiff
#3463 KMeans box
authorEduardo DAVILA <eduardo.davila@creatis.insa-lyon.fr>
Fri, 4 Jun 2021 05:04:13 +0000 (07:04 +0200)
committerEduardo DAVILA <eduardo.davila@creatis.insa-lyon.fr>
Fri, 4 Jun 2021 05:04:13 +0000 (07:04 +0200)
bbtk_creaVtk_PKG/bbs/appli/exampleKMeans.bbg [new file with mode: 0644]
bbtk_creaVtk_PKG/bbs/appli/exampleKMeans.bbs [new file with mode: 0644]
bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.cxx [new file with mode: 0644]
bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.h [new file with mode: 0644]

diff --git a/bbtk_creaVtk_PKG/bbs/appli/exampleKMeans.bbg b/bbtk_creaVtk_PKG/bbs/appli/exampleKMeans.bbg
new file mode 100644 (file)
index 0000000..6dea424
--- /dev/null
@@ -0,0 +1,85 @@
+# ----------------------------------
+# - BBTKGEditor v 1.4 BBG BlackBox Diagram file
+# - /home/davila/Creatis/C11/creatools_source/creaVtk/bbtk_creaVtk_PKG/bbs/appli/exampleKMeans.bbg
+# ----------------------------------
+
+APP_START
+CATEGORY:<VOID>
+DESCRIPTION:Description ??
+AUTHOR:Author ??
+COMPLEXBOX:FALSE
+COMPLEXINPUTS:0
+BOXES:7
+BOX
+creaVtk:KMeansStatistics:Box00
+ISEXEC:FALSE
+-38.722672:-3.319086:-900.000000
+3.477328:-13.319086:-900.000000
+PORT
+NumberOfClusters:"5"
+FIN_BOX
+BOX
+vtk:LoadHola:Box01
+ISEXEC:FALSE
+-59.901602:70.965223:-900.000000
+-24.351602:60.965223:-900.000000
+FIN_BOX
+BOX
+creaMaracasVisu:ViewerNV:Box02
+ISEXEC:FALSE
+-11.853879:-34.929430:-900.000000
+51.866121:-44.929430:-900.000000
+FIN_BOX
+BOX
+vtk:ExtractVtkImageFilter:Box08
+ISEXEC:FALSE
+-62.588481:45.993051:-900.000000
+-15.713481:35.993051:-900.000000
+PORT
+Index:"0 0 40"
+PORT
+Size:"100 100 2"
+FIN_BOX
+BOX
+creaMaracasVisu:ViewerNV:Box09
+ISEXEC:FALSE
+-95.219704:-34.527844:-900.000000
+-31.499704:-44.527844:-900.000000
+FIN_BOX
+BOX
+creaMaracasVisu:ImageChangeInformation:Box11
+ISEXEC:FALSE
+-66.065619:24.814120:-900.000000
+-14.540619:14.814120:-900.000000
+FIN_BOX
+BOX
+wx:LayoutSplit:Box07
+ISEXEC:TRUE
+-49.154085:-72.545740:-900.000000
+-10.074085:-82.545740:-900.000000
+PORT
+Orientation:"H"
+FIN_BOX
+CONNECTIONS:7
+CONNECTION
+Box01:Out:Box08:In
+NumberOfControlPoints:0
+CONNECTION
+Box00:Out:Box02:In
+NumberOfControlPoints:0
+CONNECTION
+Box08:Out:Box11:In
+NumberOfControlPoints:0
+CONNECTION
+Box11:Out:Box00:In
+NumberOfControlPoints:0
+CONNECTION
+Box11:Out:Box09:In
+NumberOfControlPoints:0
+CONNECTION
+Box09:Widget:Box07:Widget1
+NumberOfControlPoints:0
+CONNECTION
+Box02:Widget:Box07:Widget2
+NumberOfControlPoints:0
+APP_END
diff --git a/bbtk_creaVtk_PKG/bbs/appli/exampleKMeans.bbs b/bbtk_creaVtk_PKG/bbs/appli/exampleKMeans.bbs
new file mode 100644 (file)
index 0000000..16cf8f0
--- /dev/null
@@ -0,0 +1,56 @@
+# ----------------------------------
+# - BBTKGEditor v 1.4 BBS BlackBox Script
+# - /home/davila/Creatis/C11/creatools_source/creaVtk/bbtk_creaVtk_PKG/bbs/appli/exampleKMeans.bbs
+# ----------------------------------
+
+# BBTK GEditor Script
+# ----------------------
+
+include std
+include itkvtk
+include creaVtk
+include vtk
+include creaMaracasVisu
+include wx
+
+author "Author ??"
+description "Description ??"
+category "<VOID>"
+
+new creaVtk:KMeansStatistics Box00
+  set Box00.NumberOfClusters "5"
+
+new vtk:LoadHola Box01
+
+new creaMaracasVisu:ViewerNV Box02
+
+new vtk:ExtractVtkImageFilter Box08
+  set Box08.Index "0 0 40"
+  set Box08.Size "100 100 2"
+
+new creaMaracasVisu:ViewerNV Box09
+
+new creaMaracasVisu:ImageChangeInformation Box11
+
+new wx:LayoutSplit Box07
+  set Box07.Orientation "H"
+
+
+connect Box01.Out Box08.In
+
+connect Box00.Out Box02.In
+
+connect Box08.Out Box11.In
+
+connect Box11.Out Box00.In
+
+connect Box11.Out Box09.In
+
+connect Box09.Widget Box07.Widget1
+
+connect Box02.Widget Box07.Widget2
+
+
+
+# Complex input ports
+exec Box07
diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.cxx
new file mode 100644 (file)
index 0000000..45370ac
--- /dev/null
@@ -0,0 +1,214 @@
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+
+
+
+#include <vtkActor.h>
+#include <vtkDoubleArray.h>
+#include <vtkIntArray.h>
+#include <vtkInteractorStyleTrackballCamera.h>
+#include <vtkKMeansStatistics.h>
+#include <vtkMultiBlockDataSet.h>
+#include <vtkNamedColors.h>
+#include <vtkNew.h>
+#include <vtkPointData.h>
+#include <vtkPoints.h>
+#include <vtkPolyData.h>
+#include <vtkPolyDataMapper.h>
+#include <vtkProperty.h>
+#include <vtkRenderWindow.h>
+#include <vtkRenderWindowInteractor.h>
+#include <vtkRenderer.h>
+#include <vtkTable.h>
+#include <vtkVertexGlyphFilter.h>
+#include <vtkXMLPolyDataWriter.h>
+
+#include <sstream>
+
+
+
+#include "bbcreaVtkKMeansStatistics.h"
+#include "bbcreaVtkPackage.h"
+namespace bbcreaVtk
+{
+
+BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,KMeansStatistics)
+BBTK_BLACK_BOX_IMPLEMENTATION(KMeansStatistics,bbtk::AtomicBlackBox);
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void KMeansStatistics::Process()
+{
+
+// THE MAIN PROCESSING METHOD BODY
+//   Here we simply set the input 'In' value to the output 'Out'
+//   And print out the output value
+// INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
+//    void bbSet{Input|Output}NAME(const TYPE&)
+//    const TYPE& bbGet{Input|Output}NAME() const 
+//    Where :
+//    * NAME is the name of the input/output
+//      (the one provided in the attribute 'name' of the tag 'input')
+//    * TYPE is the C++ type of the input/output
+//      (the one provided in the attribute 'type' of the tag 'input')
+
+//    bbSetOutputOut( bbGetInputIn() );
+//    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
+  
+  
+
+  printf("EED KMeansStatistics::Process Start\n");
+
+       vtkNew<vtkTable> inputData;
+       int ext[6];
+       int dimX,dimY,dimZ;
+       bbGetInputIn()->GetExtent(ext);
+       dimX = ext[1]-ext[0]+1;
+       dimY = ext[3]-ext[2]+1;
+       dimZ = ext[5]-ext[4]+1;
+
+       vtkImageData *resultImage = vtkImageData::New();
+       resultImage->Initialize();
+       resultImage->SetSpacing( bbGetInputIn()->GetSpacing() );
+       resultImage->SetDimensions(  dimX, dimY, dimZ );
+       resultImage->AllocateScalars( VTK_SHORT ,1 );
+  
+       long int size= dimX*dimY*dimZ;
+    vtkDoubleArray *doubleArrayX               = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayY               = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayZ               = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayValue1  = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayValue2  = vtkDoubleArray::New();
+    vtkDoubleArray *doubleArrayValue3  = vtkDoubleArray::New();
+    doubleArrayX->SetNumberOfComponents(1);
+    doubleArrayY->SetNumberOfComponents(1);
+    doubleArrayZ->SetNumberOfComponents(1);
+    doubleArrayValue1->SetNumberOfComponents(1);
+    doubleArrayValue2->SetNumberOfComponents(1);
+    doubleArrayValue3->SetNumberOfComponents(1);
+    doubleArrayX->SetName("X");
+    doubleArrayY->SetName("Y");
+    doubleArrayZ->SetName("Z");
+    doubleArrayValue1->SetName("Value1");
+    doubleArrayValue2->SetName("Value2");
+    doubleArrayValue3->SetName("Value3");
+    doubleArrayX->SetNumberOfTuples( size );
+    doubleArrayY->SetNumberOfTuples( size );
+    doubleArrayZ->SetNumberOfTuples( size );
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) { doubleArrayValue1->SetNumberOfTuples( size ); }
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) { doubleArrayValue2->SetNumberOfTuples( size ); }
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) { doubleArrayValue3->SetNumberOfTuples( size ); }
+    
+       double value;
+       int x,y,z;
+       for ( z=0 ; z<dimZ ; z++ )
+       {
+               for ( y=0 ; y<dimY ; y++ )
+               {
+                       for ( x=0 ; x<dimX ; x++ )
+                       {
+                               doubleArrayX->SetValue(x + y*dimX + z*dimX*dimY,  x );
+                               doubleArrayY->SetValue(x + y*dimX + z*dimX*dimY,  y );
+                               doubleArrayZ->SetValue(x + y*dimX + z*dimX*dimY,  z );
+                               if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) 
+                               { 
+                                       value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,0);
+                                       doubleArrayValue1->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
+                               }
+                               if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) 
+                               { 
+                                       value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,1);
+                                       doubleArrayValue2->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
+                               }
+                               if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) 
+                               { 
+                                       value = bbGetInputIn()->GetScalarComponentAsDouble(x,y,z,2);
+                                       doubleArrayValue3->SetValue( x + y*dimX + z*dimX*dimY ,  value ); 
+                               }
+                       } // for x
+               } // for y        
+       }// for z 
+
+    inputData->AddColumn(doubleArrayX);
+    inputData->AddColumn(doubleArrayY);
+    inputData->AddColumn(doubleArrayZ);
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) { inputData->AddColumn(doubleArrayValue1); }
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) { inputData->AddColumn(doubleArrayValue2); }
+       if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) { inputData->AddColumn(doubleArrayValue3); }
+
+
+  printf("EED KMeansStatistics::Process 1\n");
+
+  vtkNew<vtkKMeansStatistics> kMeansStatistics;
+  kMeansStatistics->SetInputData(vtkStatisticsAlgorithm::INPUT_DATA, inputData);
+  kMeansStatistics->SetColumnStatus(inputData->GetColumnName(0), 1);
+  kMeansStatistics->SetColumnStatus(inputData->GetColumnName(1), 1);
+  kMeansStatistics->SetColumnStatus(inputData->GetColumnName(2), 1);
+  if (bbGetInputIn()->GetNumberOfScalarComponents()>=1) {   kMeansStatistics->SetColumnStatus(inputData->GetColumnName(3), 1); }
+  if (bbGetInputIn()->GetNumberOfScalarComponents()>=2) {   kMeansStatistics->SetColumnStatus(inputData->GetColumnName(4), 1); }
+  if (bbGetInputIn()->GetNumberOfScalarComponents()>=3) {   kMeansStatistics->SetColumnStatus(inputData->GetColumnName(5), 1); }
+
+  kMeansStatistics->RequestSelectedColumns();
+  kMeansStatistics->SetAssessOption(true);
+  kMeansStatistics->SetDefaultNumberOfClusters( bbGetInputNumberOfClusters() );
+  kMeansStatistics->Update();
+
+// Display the results
+//  kMeansStatistics->GetOutput()->Dump();
+       for ( z=0 ; z<dimZ ; z++ )
+       {
+               for ( y=0 ; y<dimY ; y++ )
+               {
+                       for ( x=0 ; x<dimX ; x++ )
+                       {
+                               long int        r = x+y*dimX+z*dimX*dimY;
+                           vtkVariant  v = kMeansStatistics->GetOutput()->GetValue( r, kMeansStatistics->GetOutput()->GetNumberOfColumns() - 1);
+                               resultImage->SetScalarComponentFromDouble(x,y,z,0,(double) (v.ToInt()) );
+                       } // for x
+               } // for y        
+       }// for z   
+  bbSetOutputOut( resultImage );
+  printf("EED KMeansStatistics::Process Start\n");  
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void KMeansStatistics::bbUserSetDefaultValues()
+{
+
+//  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
+//    Here we initialize the input 'In' to 0
+   bbSetInputIn(NULL);
+   bbSetInputNumberOfClusters(3);
+  
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void KMeansStatistics::bbUserInitializeProcessing()
+{
+
+//  THE INITIALIZATION METHOD BODY :
+//    Here does nothing 
+//    but this is where you should allocate the internal/output pointers 
+//    if any 
+
+  
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void KMeansStatistics::bbUserFinalizeProcessing()
+{
+
+//  THE FINALIZATION METHOD BODY :
+//    Here does nothing 
+//    but this is where you should desallocate the internal/output pointers 
+//    if any
+  
+}
+}
+// EO namespace bbcreaVtk
+
+
diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.h b/bbtk_creaVtk_PKG/src/bbcreaVtkKMeansStatistics.h
new file mode 100644 (file)
index 0000000..de19521
--- /dev/null
@@ -0,0 +1,52 @@
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+#ifndef __bbcreaVtkKMeansStatistics_h_INCLUDED__
+#define __bbcreaVtkKMeansStatistics_h_INCLUDED__
+
+#include "bbcreaVtk_EXPORT.h"
+#include "bbtkAtomicBlackBox.h"
+#include "iostream"
+
+#include <vtkImageData.h>
+
+namespace bbcreaVtk
+{
+
+class bbcreaVtk_EXPORT KMeansStatistics
+ : 
+   public bbtk::AtomicBlackBox
+{
+  BBTK_BLACK_BOX_INTERFACE(KMeansStatistics,bbtk::AtomicBlackBox);
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+  BBTK_DECLARE_INPUT(In,vtkImageData*);
+  BBTK_DECLARE_INPUT(NumberOfClusters,int);
+  
+  BBTK_DECLARE_OUTPUT(Out,vtkImageData*);
+  BBTK_PROCESS(Process);
+  void Process();
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+};
+
+BBTK_BEGIN_DESCRIBE_BLACK_BOX(KMeansStatistics,bbtk::AtomicBlackBox);
+  BBTK_NAME("KMeansStatistics");
+  BBTK_AUTHOR("InfoDev");
+  BBTK_DESCRIPTION("No Description.");
+  BBTK_CATEGORY("empty");
+  BBTK_INPUT(KMeansStatistics,In,"Input image",vtkImageData*,"");
+  BBTK_INPUT(KMeansStatistics,NumberOfClusters,"(default 3) Number of Clusters", int ,"");
+  
+  BBTK_OUTPUT(KMeansStatistics,Out,"Output image with labels",vtkImageData*,"");
+BBTK_END_DESCRIBE_BLACK_BOX(KMeansStatistics);
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+}
+// EO namespace bbcreaVtk
+
+#endif // __bbcreaVtkKMeansStatistics_h_INCLUDED__
+