Medical Imaging Interaction Toolkit  2018.4.99-e1268d66
Medical Imaging Interaction Toolkit
CLOverlayRoiCenterOfMass.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #ifndef mitkCLPolyToNrrd_cpp
13 #define mitkCLPolyToNrrd_cpp
14 
15 #include "time.h"
16 #include <sstream>
17 #include <fstream>
18 
19 #include <mitkIOUtil.h>
20 #include "mitkCommandLineParser.h"
21 
24 #include <itkImageRegionIteratorWithIndex.h>
25 
26 #include <QApplication>
28 #include "QmitkRegisterClasses.h"
29 #include "QmitkRenderWindow.h"
30 #include "vtkRenderLargeImage.h"
31 #include "vtkPNGWriter.h"
32 
33 #include <mitkImageAccessByItk.h>
34 #include <mitkImageCast.h>
35 
36 typedef itk::Image< double, 3 > FloatImageType;
37 typedef itk::Image< unsigned char, 3 > MaskImageType;
38 
39 
40 template<typename TPixel, unsigned int VImageDimension>
41 static void
42 FindMostSampleSlice(itk::Image<TPixel, VImageDimension>* mask, int & selectedSlice)
43 {
44  int idx = VImageDimension - 1;
45 
46  int size = mask->GetLargestPossibleRegion().GetSize()[idx];
47  std::vector<int> numberOfSamples;
48  numberOfSamples.resize(size,0);
49 
50  itk::ImageRegionIteratorWithIndex<itk::Image<TPixel, VImageDimension> > mask1Iter(mask, mask->GetLargestPossibleRegion());
51  while (!mask1Iter.IsAtEnd())
52  {
53  if (mask1Iter.Value() > 0)
54  {
55  numberOfSamples[mask1Iter.GetIndex()[idx]]+=1;
56  }
57  ++mask1Iter;
58  }
59  selectedSlice = 0;
60  for (std::size_t i = 0; i < numberOfSamples.size(); ++i)
61  {
62  if (numberOfSamples[selectedSlice] < numberOfSamples[i])
63  selectedSlice = i;
64  }
65 }
66 
67 static
69 {
70  // Create a Standalone Datastorage for the single purpose of saving screenshots..
72  QmitkRenderWindow renderWindow;
73  renderWindow.GetRenderer()->SetDataStorage(ds);
74 
75  auto nodeI = mitk::DataNode::New();
76  nodeI->SetData(image);
77  auto nodeM = mitk::DataNode::New();
78  nodeM->SetData(mask);
79  ds->Add(nodeI);
80  ds->Add(nodeM);
81 
82  auto geo = ds->ComputeBoundingGeometry3D(ds->GetAll());
84  mask->GetTimeGeometry(), mitk::RenderingManager::REQUEST_UPDATE_ALL, true);
85 
87  sliceNaviController->SetViewDirection(mitk::SliceNavigationController::Axial);
88  unsigned int numberOfSteps = 1;
89  if (sliceNaviController)
90  {
91  numberOfSteps = sliceNaviController->GetSlice()->GetSteps();
92  sliceNaviController->GetSlice()->SetPos(numberOfSteps-index);
93  }
94 
95  renderWindow.show();
96  renderWindow.resize(256, 256);
97 
98  //if (sliceNaviController)
99  //{
100  // sliceNaviController->GetSlice()->SetPos(index);
101  //}
102  renderWindow.GetRenderer()->PrepareRender();
103 
104  vtkRenderWindow* renderWindow2 = renderWindow.GetVtkRenderWindow();
105  mitk::BaseRenderer* baserenderer = mitk::BaseRenderer::GetInstance(renderWindow2);
106  auto vtkRender = baserenderer->GetVtkRenderer();
107  vtkRender->GetRenderWindow()->WaitForCompletion();
108 
109  vtkRenderLargeImage* magnifier = vtkRenderLargeImage::New();
110  magnifier->SetInput(vtkRender);
111  magnifier->SetMagnification(3.0);
112 
113  std::stringstream ss;
114  ss << path <<".png";
115  std::string tmpImageName;
116  ss >> tmpImageName;
117  auto fileWriter = vtkPNGWriter::New();
118  fileWriter->SetInputConnection(magnifier->GetOutputPort());
119  fileWriter->SetFileName(tmpImageName.c_str());
120  fileWriter->Write();
121  fileWriter->Delete();
122 }
123 
124 int main(int argc, char* argv[])
125 {
126  mitkCommandLineParser parser;
127  parser.setArgumentPrefix("--", "-");
128 
129  parser.addArgument("image", "i", mitkCommandLineParser::Image, "Input Image", "", us::Any(),false, false, false, mitkCommandLineParser::Input);
130  parser.addArgument("mask", "m", mitkCommandLineParser::Image, "Input Image", "", us::Any(),false, false, false, mitkCommandLineParser::Input);
131  parser.addArgument("output", "o", mitkCommandLineParser::Image, "Output Image", "", us::Any(),false, false, false, mitkCommandLineParser::Input);
132 
133  // Miniapp Infos
134  parser.setCategory("Classification Tools");
135  parser.setTitle("Image with Overlay Plotter");
136  parser.setDescription("Plots ");
137  parser.setContributor("German Cancer Research Center (DKFZ)");
138 
139  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
140 
141  std::string imagePath = us::any_cast<std::string>(parsedArgs["image"]);
142  std::string maskPath = us::any_cast<std::string>(parsedArgs["mask"]);
143  std::string outputPath = us::any_cast<std::string>(parsedArgs["output"]);
144 
145  if (parsedArgs.size()==0)
146  {
147  return EXIT_FAILURE;
148  }
149  if ( parsedArgs.count("help") || parsedArgs.count("h"))
150  {
151  return EXIT_SUCCESS;
152  }
153 
154  std::string version = "Version: 1.0";
155  MITK_INFO << version;
156 
157  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(imagePath);
158  mitk::Image::Pointer mask = mitk::IOUtil::Load<mitk::Image>(maskPath);
159 
160  // Create a QTApplication and a Datastorage
161  // This is necessary in order to save screenshots of
162  // each image / slice.
163  QApplication qtapplication(argc, argv);
165 
166  int currentSlice = 0;
167  AccessByItk_1(mask, FindMostSampleSlice, currentSlice);
168 
169  SaveSliceOrImageAsPNG(image, mask, outputPath, currentSlice);
170 
171  return 0;
172 }
173 
174 #endif
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
#define MITK_INFO
Definition: mitkLogMacros.h:18
itk::Image< unsigned char, 3 > MaskImageType
vtkRenderer * GetVtkRenderer() const
void SetDataStorage(mitk::DataStorage *storage) override
set the datastorage that will be used for rendering
Organizes the rendering process.
void setContributor(std::string contributor)
MITKQTWIDGETS_EXPORT void QmitkRegisterClasses()
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
static void FindMostSampleSlice(itk::Image< TPixel, VImageDimension > *mask, int &selectedSlice)
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
itk::Image< double, 3 > FloatImageType
static Pointer New()
static RenderingManager * GetInstance()
Definition: usAny.h:163
MITK implementation of the QVTKWidget.
virtual mitk::VtkPropRenderer * GetRenderer()
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
virtual void PrepareRender()
This methods contains all method neceassary before a VTK Render() call.
static void SaveSliceOrImageAsPNG(mitk::Image::Pointer image, mitk::Image::Pointer mask, std::string path, int index)
mitk::Image::Pointer mask
virtual mitk::SliceNavigationController * GetSliceNavigationController()
vtkRenderWindow * GetVtkRenderWindow() override
void setTitle(std::string title)
void setDescription(std::string description)
int main(int argc, char *argv[])