Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
CLScreenshot.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 
23 #include <mitkProperties.h>
24 
25 #include <QApplication>
27 #include "QmitkRegisterClasses.h"
28 #include "QmitkRenderWindow.h"
29 #include "vtkRenderLargeImage.h"
30 #include "vtkPNGWriter.h"
31 
32 
33 static
34 void SaveSliceOrImageAsPNG(std::vector<std::string> listOfOutputs, std::string path)
35 {
36  std::vector<mitk::ColorProperty::Pointer> colorList;
37  colorList.push_back(mitk::ColorProperty::New(0.9569, 0.16471, 0.25490)); // Red
38  colorList.push_back(mitk::ColorProperty::New(1, 0.839, 0)); // Yellow
39  colorList.push_back(mitk::ColorProperty::New(0, 0.6, 0.2)); // Green
40  colorList.push_back(mitk::ColorProperty::New(0, 0.2784, 0.7255)); // BLue
41  colorList.push_back(mitk::ColorProperty::New(1,0.3608,0)); // Orange
42  colorList.push_back(mitk::ColorProperty::New(0.839215,0.141176,0.80784)); // Violett
43  colorList.push_back(mitk::ColorProperty::New(0.1372,0.81568,0.7647)); // Turkis
44  colorList.push_back(mitk::ColorProperty::New(0.61176,0.9568,0.16078)); // Bright Green
45  colorList.push_back(mitk::ColorProperty::New(1,0.4274,0.16862)); // Dark Orange
46  colorList.push_back(mitk::ColorProperty::New(0.88633,0.14901,0.64705)); // Pink
47 
48  // Create a Standalone Datastorage for the single purpose of saving screenshots..
50  QmitkRenderWindow renderWindow;
51  renderWindow.GetRenderer()->SetDataStorage(ds);
52 
53  int numberOfSegmentations = 0;
54  bool isSegmentation = false;
55  for (auto name : listOfOutputs)
56  {
57  mitk::Image::Pointer tmpImage = mitk::IOUtil::Load<mitk::Image>(name);
58  auto nodeI = mitk::DataNode::New();
59  nodeI->SetData(tmpImage);
60  nodeI->GetPropertyValue("binary",isSegmentation);
61  if (isSegmentation)
62  {
63  nodeI->SetProperty("color", colorList[numberOfSegmentations % colorList.size()]);
64  nodeI->SetProperty("binaryimage.hoveringannotationcolor", colorList[numberOfSegmentations % colorList.size()]);
65  nodeI->SetProperty("binaryimage.hoveringcolor", colorList[numberOfSegmentations % colorList.size()]);
66  nodeI->SetProperty("binaryimage.selectedannotationcolor", colorList[numberOfSegmentations % colorList.size()]);
67  nodeI->SetProperty("binaryimage.selectedcolor", colorList[numberOfSegmentations % colorList.size()]);
68  numberOfSegmentations++;
69  }
70  ds->Add(nodeI);
71  }
72 
73  auto geo = ds->ComputeBoundingGeometry3D(ds->GetAll());
75 
77  unsigned int numberOfSteps = 1;
78  if (sliceNaviController)
79  {
80  numberOfSteps = sliceNaviController->GetSlice()->GetSteps();
81  sliceNaviController->GetSlice()->SetPos(0);
82  }
83 
84  renderWindow.show();
85  renderWindow.resize(512, 512);
86 
87  for (unsigned int currentStep = 0; currentStep < numberOfSteps; ++currentStep)
88  {
89  if (sliceNaviController)
90  {
91  sliceNaviController->GetSlice()->SetPos(currentStep);
92  }
93 
94  renderWindow.GetRenderer()->PrepareRender();
95 
96  vtkRenderWindow* renderWindow2 = renderWindow.GetVtkRenderWindow();
97  mitk::BaseRenderer* baserenderer = mitk::BaseRenderer::GetInstance(renderWindow2);
98  auto vtkRender = baserenderer->GetVtkRenderer();
99  vtkRender->GetRenderWindow()->WaitForCompletion();
100 
101  vtkRenderLargeImage* magnifier = vtkRenderLargeImage::New();
102  magnifier->SetInput(vtkRender);
103  magnifier->SetMagnification(3.0);
104 
105  std::stringstream ss;
106  ss << path << "screenshot_step-"<<currentStep<<".png";
107  std::string tmpImageName;
108  ss >> tmpImageName;
109  auto fileWriter = vtkPNGWriter::New();
110  fileWriter->SetInputConnection(magnifier->GetOutputPort());
111  fileWriter->SetFileName(tmpImageName.c_str());
112  fileWriter->Write();
113  fileWriter->Delete();
114  }
115 }
116 
117 int main(int argc, char* argv[])
118 {
119  mitkCommandLineParser parser;
120  parser.setArgumentPrefix("--", "-");
121 
122  // Required Parameter
123  parser.addArgument("image", "i", mitkCommandLineParser::Image, "Input Image", "Path to the input image files (Separated with semicolons)", us::Any(), false, false, false, mitkCommandLineParser::Input);
124  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output text file", "Path to output file. The output statistic is appended to this file.", us::Any(), false, false, false, mitkCommandLineParser::Output);
125  parser.addArgument("direction", "dir", mitkCommandLineParser::String, "Int", "Allows to specify the direction for Cooc and RL. 0: All directions, 1: Only single direction (Test purpose), 2,3,4... Without dimension 0,1,2... ", us::Any());
126 
127  // Miniapp Infos
128  parser.setCategory("Classification Tools");
129  parser.setTitle("Screenshot of a single image");
130  parser.setDescription("");
131  parser.setContributor("German Cancer Research Center (DKFZ)");
132 
133  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
134 
135  if (parsedArgs.size()==0)
136  {
137  return EXIT_FAILURE;
138  }
139  if ( parsedArgs.count("help") || parsedArgs.count("h"))
140  {
141  return EXIT_SUCCESS;
142  }
143 
144  std::string version = "Version: 1.0";
145  MITK_INFO << version;
146 
147  //int direction = 0;
148  if (parsedArgs.count("direction"))
149  {
150  MITK_INFO << "Warning: Option direction currently not supported";
151  // direction = mitk::cl::splitDouble(parsedArgs["direction"].ToString(), ';')[0];
152  }
153 
154  auto listOfFiles = mitk::cl::splitString(parsedArgs["image"].ToString(), ';');
155 
156  // Create a QTApplication and a Datastorage
157  // This is necessary in order to save screenshots of
158  // each image / slice.
159  QApplication qtapplication(argc, argv);
161 
162  SaveSliceOrImageAsPNG(listOfFiles, parsedArgs["output"].ToString());
163 
164  return 0;
165 }
166 
167 #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
vtkRenderer * GetVtkRenderer() const
void SetDataStorage(mitk::DataStorage *storage) override
set the datastorage that will be used for rendering
int main(int argc, char *argv[])
static Pointer New()
Organizes the rendering process.
void setContributor(std::string contributor)
MITKQTWIDGETS_EXPORT void QmitkRegisterClasses()
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)
static void SaveSliceOrImageAsPNG(std::vector< std::string > listOfOutputs, std::string path)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
static Pointer New()
static RenderingManager * GetInstance()
Definition: usAny.h:163
MITK implementation of the QVTKWidget.
virtual mitk::VtkPropRenderer * GetRenderer()
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
virtual void PrepareRender()
This methods contains all method neceassary before a VTK Render() call.
virtual mitk::SliceNavigationController * GetSliceNavigationController()
vtkRenderWindow * GetVtkRenderWindow() override
void setTitle(std::string title)
std::vector< std::string > MITKCLUTILITIES_EXPORT splitString(std::string str, char delimiter)
void setDescription(std::string description)