PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkAndorVideoSourceTest.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2  Program: Plus
3  Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4  See License.txt for details.
5 =========================================================Plus=header=end*/
6 
18 #include "PlusConfigure.h"
19 #include "vtkCallbackCommand.h"
20 #include "vtkCommand.h"
21 
22 #include "vtkContextScene.h"
23 #include "vtkContextView.h"
24 #include "vtkFloatArray.h"
25 
26 #include "vtkImageData.h"
27 #include "vtkImageViewer.h"
28 
29 #include "vtkInformation.h"
30 #include "vtkInformationVector.h"
31 #include "vtkRenderWindow.h"
32 #include "vtkRenderWindowInteractor.h"
33 #include "vtkRenderer.h"
34 #include "vtkSmartPointer.h"
36 #include "vtkPlusDataSource.h"
37 #include "igtlOSUtil.h" // for Sleep
38 
39 #include "vtkXMLUtilities.h"
40 #include "vtksys/CommandLineArguments.hxx"
41 #include <stdlib.h>
42 
43 class vtkMyCallback : public vtkCommand
44 {
45 public:
46  static vtkMyCallback* New()
47  {
48  return new vtkMyCallback;
49  }
50 
51  virtual void Execute(vtkObject* caller, unsigned long, void*)
52  {
53  m_Viewer->Render();
54 
55  //update the timer so it will trigger again
56  m_Interactor->CreateTimer(VTKI_TIMER_UPDATE);
57  }
58 
59  vtkRenderWindowInteractor* m_Interactor;
60  vtkImageViewer* m_Viewer;
61 
62 private:
63 
64  vtkMyCallback()
65  {
66  m_Interactor = NULL;
67  m_Viewer = NULL;
68  }
69 };
70 
71 
72 int main(int argc, char* argv[])
73 {
74  int left;
75  left = -1;
76  int startX_01 = left < 0 ? 0 : left;
77  left = 10;
78  int startX_02 = left < 0 ? 0 : left;
79 
80  bool printHelp(false);
81  bool renderingOff(false);
82 
83  std::string inputConfigFileName;
84  std::string outputFileName("AndorCameraTest.mha");
85  std::string flatCorrectionPath;
86  std::string biasDarkCorrectionPath;
87 
88  vtksys::CommandLineArguments args;
89  args.Initialize(argc, argv);
90 
91  int verboseLevel = vtkPlusLogger::LOG_LEVEL_INFO;
92 
93  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
94  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName, "Config file containing the device configuration.");
95  args.AddArgument("--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff, "Run test without rendering.");
96  args.AddArgument("--output-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputFileName, "Filename of the output video buffer sequence metafile (Default: AndorCameraTest.mha)");
97  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level 1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
98  args.AddArgument("--flat-correction-image", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &flatCorrectionPath, "Image file (.png) containing the flat field correction.");
99  args.AddArgument("--bias-correction-image", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &biasDarkCorrectionPath, "Image file (.png) containing the combined bias+dark and dark correction.");
100 
101  if(!args.Parse())
102  {
103  std::cerr << "Problem parsing arguments" << std::endl;
104  std::cout << "\n\nvtkPlusAndorVideoSourceTest1 help:" << args.GetHelp() << std::endl;
105  exit(EXIT_FAILURE);
106  }
107 
108 
109  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
110 
111  if(printHelp)
112  {
113  std::cout << "\n\nvtkPlusAndorVideoSourceTest help:" << args.GetHelp() << std::endl;
114  exit(EXIT_SUCCESS);
115  }
116 
117 
118  vtkSmartPointer< vtkPlusAndorVideoSource > andorCamDevice = vtkSmartPointer< vtkPlusAndorVideoSource >::New();
119  andorCamDevice->SetDeviceId("BLICamera");
120 
121 
122  // Read config file
123  LOG_DEBUG("Reading config file...");
124  vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
125 
126  if(PlusXmlUtils::ReadDeviceSetConfigurationFromFile(configRootElement, inputConfigFileName.c_str()) == PLUS_FAIL)
127  {
128  LOG_ERROR("Unable to read configuration from file " << inputConfigFileName.c_str());
129  return EXIT_FAILURE;
130  }
131 
132  andorCamDevice->ReadConfiguration(configRootElement);
133 
134  if(andorCamDevice->Connect() != PLUS_SUCCESS)
135  {
136  LOG_ERROR("Unable to connect to Andor camera");
137  exit(EXIT_FAILURE);
138  }
139 
140  LOG_INFO(andorCamDevice->GetSdkVersion());
141  vtkIndent indent;
142  andorCamDevice->PrintSelf(std::cout, indent);
143 
144  andorCamDevice->PrepareAcquisition();
145  andorCamDevice->StartBLIFrameAcquisition(-1, -1, -1, 2.0);
146  andorCamDevice->WaitForAcquisitionWithTimeout(100.0);
147  andorCamDevice->StartGrayscaleFrameAcquisition(-1, -1, -1, 2.0);
148  andorCamDevice->WaitForAcquisitionWithTimeout(100.0);
149  andorCamDevice->StartRecording();
150 
151  if(renderingOff)
152  {
153  vtkPlusChannel* raw(nullptr);
154  if(andorCamDevice->GetOutputChannelByName(raw, "BLIRawStream") != PLUS_SUCCESS)
155  {
156  LOG_ERROR("Unable to locate the channel with Id=\"BLIRawStream\". Check config file.");
157  return EXIT_FAILURE;
158  }
159 
160  vtkPlusChannel* rectified(nullptr);
161  if(andorCamDevice->GetOutputChannelByName(rectified, "BLICorrectedStream") != PLUS_SUCCESS)
162  {
163  LOG_WARNING("Unable to locate the channel with Id=\"BLICorrectedStream\". Additional mode will not be used.");
164  }
165 
166  LOG_DEBUG("Rendering disabled. Wait for just a few seconds to acquire data before exiting");
167  andorCamDevice->StartBLIFrameAcquisition(-1, -1, -1, 2.0);
168  andorCamDevice->WaitForAcquisitionWithTimeout(100.0);
169  andorCamDevice->StartGrayscaleFrameAcquisition(-1, -1, -1, 2.0);
170  andorCamDevice->WaitForAcquisitionWithTimeout(100.0);
171  andorCamDevice->StartBLIFrameAcquisition(-1, -1, -1, 2.0);
172  andorCamDevice->WaitForAcquisitionWithTimeout(100.0);
173  andorCamDevice->StartGrayscaleFrameAcquisition(-1, -1, -1, 2.0);
174  andorCamDevice->WaitForAcquisitionWithTimeout(100.0);
175 
176  vtkPlusDataSource* bSource(nullptr);
177  raw->GetVideoSource(bSource);
178  bSource->WriteToSequenceFile(outputFileName.c_str());
179 
180  if(rectified)
181  {
182  vtkPlusDataSource* rfSource(nullptr);
183  rectified->GetVideoSource(rfSource);
184  rfSource->WriteToSequenceFile((outputFileName + "_a.mha").c_str());
185  }
186  }
187  else
188  {
189  // Show the live optical image in a VTK renderer window
190 
191  vtkSmartPointer<vtkImageViewer> viewer = vtkSmartPointer<vtkImageViewer>::New();
192  viewer->SetInputConnection(andorCamDevice->GetOutputPort()); //set image to the render and window
193  viewer->SetColorWindow(256);
194  viewer->SetColorLevel(400);
195  viewer->SetZSlice(0);
196  viewer->SetSize(1024, 1024);
197 
198  //Create the interactor that handles the event loop
199  vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
200  iren->SetRenderWindow(viewer->GetRenderWindow());
201  viewer->SetupInteractor(iren);
202 
203  viewer->Render(); //must be called after iren and viewer are linked or there will be problems
204 
205  // Establish timer event and create timer to update the live image
206  vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
207  call->m_Interactor = iren;
208  call->m_Viewer = viewer;
209  iren->AddObserver(vtkCommand::TimerEvent, call);
210  iren->CreateTimer(VTKI_TIMER_FIRST);
211 
212  //iren must be initialized so that it can handle events
213  iren->Initialize();
214  iren->Start();
215  }
216 
217  andorCamDevice->Disconnect();
218 
219  //update and write configuration
220  andorCamDevice->WriteConfiguration(configRootElement);
221  std::string outConfigFilename = inputConfigFileName + "-updated.xml";
222  bool success = vtkXMLUtilities::WriteElementToFile(configRootElement, outConfigFilename.c_str());
223  if(!success)
224  {
225  LOG_ERROR("Unable to write configuration to: " << outConfigFilename + ".xml");
226  }
227  else
228  {
229  LOG_INFO("Configuration file written to: " << outConfigFilename + ".xml");
230  }
231 
232  return EXIT_SUCCESS;
233 }
234 
virtual PlusStatus WriteToSequenceFile(const char *filename, bool useCompression=false)
vtkRenderWindowInteractor * iren
#define PLUS_FAIL
Definition: PlusCommon.h:43
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
vtkImageViewer * viewer
static vtkIGSIOLogger * Instance()
int main(int argc, char *argv[])
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
PlusStatus GetVideoSource(vtkPlusDataSource *&aVideoSource) const
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Definition: PlusXmlUtils.h:23
Interface to a 3D positioning tool, video source, or generalized data stream.