13 #include "PlusConfigure.h" 17 #include "igsioTrackedFrame.h" 18 #include "vtkCallbackCommand.h" 19 #include "vtkCommand.h" 21 #include "vtkMatrix4x4.h" 23 #include "vtkIGSIOSequenceIO.h" 24 #include "vtkSmartPointer.h" 25 #include "vtkIGSIOTrackedFrameList.h" 26 #include "vtkTransform.h" 27 #include "vtkIGSIOTransformRepository.h" 28 #include "vtkIGSIOTransformRepository.h" 29 #include "vtkXMLDataElement.h" 30 #include "vtkXMLUtilities.h" 31 #include "vtksys/CommandLineArguments.hxx" 32 #include "vtksys/SystemTools.hxx" 48 int main(
int argc,
char* argv[])
50 std::string inputCalibrationSeqMetafile;
51 std::string inputValidationSeqMetafile;
53 std::string inputConfigFileName;
54 std::string resultConfigFileName =
"";
55 std::string saveResultsFilename;
56 std::string strOperation;
66 int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
68 vtksys::CommandLineArguments cmdargs;
69 cmdargs.Initialize(argc, argv);
71 cmdargs.AddArgument(
"--calibration-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputCalibrationSeqMetafile,
"Sequence metafile name of input calibration dataset.");
72 cmdargs.AddArgument(
"--validation-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputValidationSeqMetafile,
"Sequence metafile name of input validation dataset.");
74 cmdargs.AddArgument(
"--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName,
"Configuration file name prefix");
75 cmdargs.AddArgument(
"--save-results-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &saveResultsFilename,
"Save results file name");
76 cmdargs.AddArgument(
"--operation", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &strOperation,
"Type of experiment to perform");
78 cmdargs.AddArgument(
"--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel,
"Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
82 std::cerr <<
"Problem parsing arguments" << std::endl;
83 std::cout <<
"Help: " << cmdargs.GetHelp() << std::endl;
89 LOG_INFO(
"Initialize");
92 vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
95 LOG_ERROR(
"Unable to read configuration from file " << inputConfigFileName.c_str());
101 vtkSmartPointer<vtkIGSIOTransformRepository> transformRepository = vtkSmartPointer<vtkIGSIOTransformRepository>::New();
102 if (transformRepository->ReadConfiguration(configRootElement) !=
PLUS_SUCCESS)
104 LOG_ERROR(
"Failed to read CoordinateDefinitions!");
108 vtkSmartPointer<vtkPlusProbeCalibrationAlgo> freehandCalibration = vtkSmartPointer<vtkPlusProbeCalibrationAlgo>::New();
109 freehandCalibration->ReadConfiguration(configRootElement);
119 vtkSmartPointer<vtkIGSIOTrackedFrameList> calibrationTrackedFrameList = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
120 if (vtkIGSIOSequenceIO::Read(inputCalibrationSeqMetafile, calibrationTrackedFrameList) !=
PLUS_SUCCESS)
122 LOG_ERROR(
"Reading calibration images from '" << inputCalibrationSeqMetafile <<
"' failed!");
126 int numberOfSuccessfullySegmentedCalibrationImages = 0;
127 std::vector<unsigned int> segmentedCalibrationFramesIndices;
128 if (patternRecognition.
RecognizePattern(calibrationTrackedFrameList, error, &numberOfSuccessfullySegmentedCalibrationImages, &segmentedCalibrationFramesIndices) !=
PLUS_SUCCESS)
130 LOG_ERROR(
"Error occured during segmentation of calibration images!");
134 LOG_INFO(
"Segmentation success rate of calibration images: " << numberOfSuccessfullySegmentedCalibrationImages <<
" out of " << calibrationTrackedFrameList->GetNumberOfTrackedFrames());
137 vtkSmartPointer<vtkIGSIOTrackedFrameList> validationTrackedFrameList = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
138 if (vtkIGSIOSequenceIO::Read(inputValidationSeqMetafile, validationTrackedFrameList) !=
PLUS_SUCCESS)
140 LOG_ERROR(
"Reading validation images from '" << inputValidationSeqMetafile <<
"' failed!");
144 int numberOfSuccessfullySegmentedValidationImages = 0;
145 if (patternRecognition.
RecognizePattern(validationTrackedFrameList, error, &numberOfSuccessfullySegmentedValidationImages) !=
PLUS_SUCCESS)
147 LOG_ERROR(
"Error occured during segmentation of validation images!");
151 LOG_INFO(
"Segmentation success rate of validation images: " << numberOfSuccessfullySegmentedValidationImages <<
" out of " << validationTrackedFrameList->GetNumberOfTrackedFrames());
157 std::ofstream outputFile;
158 outputFile.open(saveResultsFilename.c_str(), std::ios_base::app);
163 if (strOperation.empty())
165 LOG_INFO(
"No modification operation has been specified (specify --operation parameter to change the input sequence).");
167 else if (STRCASECMP(strOperation.c_str(),
"INCREMENTAL_FRAME_DISTANCE") == 0)
171 else if (STRCASECMP(strOperation.c_str(),
"INCREMENTAL_NUMBER_OF_FRAMES") == 0)
175 else if (STRCASECMP(strOperation.c_str(),
"SAME_NUMBER_OF_FRAMES") == 0)
179 else if (STRCASECMP(strOperation.c_str(),
"MOBILE_WINDOW") == 0)
185 int maxFrame = segmentedCalibrationFramesIndices.size();
186 int numberOfConfigurations = 15;
187 outputFile <<
"Number of configurations = " << numberOfConfigurations <<
"\n";
188 std::vector<int> numberOfCalibrationFrames;
189 std::vector< std::vector<int> > selectedFrames;
193 for (
int i = 0;
i < numberOfConfigurations;
i++)
195 numberOfCalibrationFrames.push_back(5 +
i * 5);
196 std::vector<int> frames;
197 for (
int j = 0; j < numberOfCalibrationFrames.at(
i) ; j++)
199 int randomIndex = rand() % (maxFrame - minFrame) + minFrame;
200 while (std::find(frames.begin(), frames.end(), randomIndex) != frames.end())
203 randomIndex = rand() % (maxFrame - minFrame) + minFrame;
205 frames.push_back(randomIndex);
207 selectedFrames.push_back(frames);
214 int numberOfFrames = 80;
215 for (
int i = 0;
i < numberOfConfigurations;
i++)
217 numberOfCalibrationFrames.push_back(numberOfFrames);
218 std::vector<int> frames;
219 for (
int j = 0; j < numberOfCalibrationFrames.at(
i) ; j++)
221 int randomIndex = rand() % (maxFrame - minFrame) + minFrame;
222 while (std::find(frames.begin(), frames.end(), randomIndex) != frames.end())
225 randomIndex = rand() % (maxFrame - minFrame) + minFrame;
227 frames.push_back(randomIndex);
229 selectedFrames.push_back(frames);
235 int numberOfFrames = 10;
236 for (
int i = 0;
i < numberOfConfigurations;
i++)
238 maxFrame = (2 +
i) * numberOfFrames;
239 std::vector<int> frames;
240 for (
int j = 0; j < numberOfFrames ; j++)
242 int randomIndex = rand() % (maxFrame - minFrame) + minFrame;
243 while (std::find(frames.begin(), frames.end(), randomIndex) != frames.end())
246 randomIndex = rand() % (maxFrame - minFrame) + minFrame;
248 frames.push_back(randomIndex);
250 selectedFrames.push_back(frames);
257 int numberOfFrames = 2 *
width + 1;
258 for (
int i = 0;
i < numberOfConfigurations;
i++)
260 int center = rand() % (maxFrame - minFrame - numberOfFrames) +
width ;
261 std::vector<int> frames;
262 for (
int j = 0; j < numberOfFrames ; j++)
264 frames.push_back(center -
width + j);
266 selectedFrames.push_back(frames);
270 vtkSmartPointer<vtkIGSIOTrackedFrameList> sequenceTrackedFrameList = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
272 std::string methods[] = {
"NO_OPTIMIZATION",
"7param2D",
"7param3D",
"8param2D",
"8param3D"};
273 int numberOfMethods =
sizeof(methods) /
sizeof(methods[0]);
274 outputFile <<
"Number of methods = " << numberOfMethods <<
"\n";
275 std::vector<double> calibError;
276 std::vector<double> validError;
277 vnl_matrix_fixed<double, 4, 4> imageToProbeTransformMatrix;
278 FrameSizeType frameSize = calibrationTrackedFrameList->GetTrackedFrame(0)->GetFrameSize();
279 outputFile <<
"Frame size = " << frameSize[0] <<
" " << frameSize[1] <<
"\n";
280 for (
int i = 0;
i < numberOfConfigurations;
i++)
282 bool calibrationFail =
false;
283 int numberOfFramesOfTheSequence = selectedFrames.at(
i).size();
284 outputFile <<
"Frames of the sequence = ";
285 for (
int j = 0; j < numberOfFramesOfTheSequence; j++)
287 int indexInCalibrationSequence = selectedFrames.at(
i).at(j);
288 sequenceTrackedFrameList->AddTrackedFrame(calibrationTrackedFrameList->GetTrackedFrame(indexInCalibrationSequence));
289 outputFile << indexInCalibrationSequence <<
" ";
291 outputFile <<
" \n ";
293 for (
int k = 0; k < numberOfMethods; k++)
295 outputFile <<
"Method = " << methods[k] <<
" \n ";
296 if (!calibrationFail)
303 LOG_ERROR(
"Calibration failed!");
304 calibError.push_back(-1);
305 calibError.push_back(-1);
306 validError.push_back(-1);
307 validError.push_back(-1);
308 calibrationFail =
true;
312 freehandCalibration->GetCalibrationReport(&calibError, &validError, &imageToProbeTransformMatrix);
315 outputFile <<
"Calibration error = ";
316 outputFile << calibError.at(0) <<
" " << calibError.at(1) <<
" ";
317 outputFile << validError.at(0) <<
" " << validError.at(1) <<
" \n ";
320 outputFile <<
"Image to Probe transform matrix = \n ";
321 for (
int m = 0; m < 4; m++)
323 for (
int n = 0; n < 4; n++)
325 outputFile << imageToProbeTransformMatrix(m, n) <<
" ";
337 sequenceTrackedFrameList->Clear();
341 std::cout <<
"Exit success!!!" << std::endl;
349 LOG_INFO(
"Create a sub sequence using" << selectedFrames.size() <<
" frames");
350 std::sort(selectedFrames.begin(), selectedFrames.end());
351 unsigned int FirstFrameIndex = selectedFrames.at(0);
352 unsigned int LastFrameIndex = selectedFrames.at(selectedFrames.size() - 1);
353 if (LastFrameIndex >= aTrackedFrameList->GetNumberOfTrackedFrames() || FirstFrameIndex > LastFrameIndex)
355 LOG_ERROR(
"Invalid input range: (" << FirstFrameIndex <<
", " << LastFrameIndex <<
")" <<
" Permitted range within (0, " << aTrackedFrameList->GetNumberOfTrackedFrames() - 1 <<
")");
359 if (LastFrameIndex != aTrackedFrameList->GetNumberOfTrackedFrames() - 1)
361 aTrackedFrameList->RemoveTrackedFrameRange(LastFrameIndex + 1, aTrackedFrameList->GetNumberOfTrackedFrames() - 1);
364 for (
int i = selectedFrames.size() - 2;
i > 0;
i--)
366 aTrackedFrameList->RemoveTrackedFrameRange(selectedFrames.at(
i) + 1, selectedFrames.at(
i + 1) - 1);
369 if (FirstFrameIndex != 0)
371 aTrackedFrameList->RemoveTrackedFrameRange(0, FirstFrameIndex - 1);
381 if (STRCASECMP(method.c_str(),
"NO_OPTIMIZATION") == 0)
385 else if (STRCASECMP(method.c_str(),
"7param2D") == 0)
390 else if (STRCASECMP(method.c_str(),
"7param3D") == 0)
395 else if (STRCASECMP(method.c_str(),
"8param2D") == 0)
400 else if (STRCASECMP(method.c_str(),
"8param3D") == 0)
Refines the image to probe transform using non-linear optimization.
PlusFidSegmentation * GetFidSegmentation()
PlusStatus SetOptimizationMethod(vtkPlusProbeCalibrationAlgo *freehandCalibration, std::string method)
void SetIsotropicPixelSpacing(bool isotropicPixelSpacing)
static vtkPlusConfig * GetInstance()
std::vector< PlusNWire > GetNWires()
vtkPlusProbeCalibrationOptimizerAlgo * GetOptimizer()
int main(int argc, char *argv[])
PhidgetLCD_Font int * width
void SetDebugOutput(bool value)
static vtkIGSIOLogger * Instance()
PlusFidLineFinder * GetFidLineFinder()
PlusStatus SubSequenceMetafile(vtkIGSIOTrackedFrameList *aTrackedFrameList, std::vector< unsigned int > selectedFrames)
void SetOptimizationMethod(OptimizationMethodType optimizationMethod)
PlusStatus RecognizePattern(vtkIGSIOTrackedFrameList *trackedFrameList, PatternRecognitionError &patternRecognitionError, int *numberOfSuccessfullySegmentedImages=NULL, std::vector< unsigned int > *segmentedFramesIndices=NULL)
void SetDeviceSetConfigurationData(vtkXMLDataElement *deviceSetConfigurationData)
PlusStatus ReadConfiguration(vtkXMLDataElement *rootConfigElement)
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Probe calibration algorithm class.