NifTK  16.4.1 - 0798f20
CMIC's Translational Medical Imaging Platform
mitkProjectPointsOnStereoVideo.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  NifTK: A software platform for medical image computing.
4 
5  Copyright (c) University College London (UCL). All rights reserved.
6 
7  This software is distributed WITHOUT ANY WARRANTY; without even
8  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9  PURPOSE.
10 
11  See LICENSE.txt in the top level directory for details.
12 
13 =============================================================================*/
14 
15 #ifndef mitkProjectPointsOnStereoVideo_h
16 #define mitkProjectPointsOnStereoVideo_h
17 
18 #include "niftkOpenCVExports.h"
19 #include <mitkOpenCVPointTypes.h>
20 #include <string>
21 #include <fstream>
22 #include <itkObject.h>
23 #include <itkObjectFactory.h>
24 #include <mitkCommon.h>
25 #include <cv.h>
26 #include <highgui.h>
28 
29 namespace mitk {
30 
44 {
45 
46 public:
47 
48  mitkClassMacroItkParent(ProjectPointsOnStereoVideo, itk::Object)
49  itkNewMacro(ProjectPointsOnStereoVideo)
50 
51 
56  void Initialise (std::string directory, std::string calibrationParameterDirectory);
62  void Initialise (std::string directory);
63 
68  void Project(mitk::VideoTrackerMatching::Pointer matcher, std::vector<double> * perturbation = NULL);
69 
75  void SetMatcherCameraToTracker(mitk::VideoTrackerMatching::Pointer matcher);
76 
80  void SaveProjectedCoordinates (std::string filename);
81 
85  void SavePointsInLeftLensCoordinates (std::string filename);
86 
91  void AppendWorldPointsByTriangulation
92  (std::vector< mitk::ProjectedPointPair > onScreenPointPairs,
93  std::vector < unsigned int> frameNumber , mitk::VideoTrackerMatching::Pointer matcher,
94  std::vector <double> * perturbation = NULL);
95 
96  void SetVisualise( bool) ;
97  void SetSaveVideo( bool state );
98  itkSetMacro ( CorrectVideoAspectRatioByHalvingWidth, bool );
99  itkSetMacro ( TrackerIndex, int);
100  itkSetMacro ( ReferenceIndex, int);
101  itkSetMacro ( DrawAxes, bool);
102  itkSetMacro ( HaltOnVideoReadFail, bool);
103  itkSetMacro ( DontProject, bool);
104  itkSetMacro ( VisualiseTrackingStatus, bool);
105  itkSetMacro ( AnnotateWithGoldStandards, bool );
106  itkSetMacro ( WriteAnnotatedGoldStandards, bool );
107  itkSetMacro ( WriteTrackingPositionData, bool );
108  itkSetMacro ( WriteTrackingMatrixFilesPerFrame, bool);
109  itkSetMacro ( AllowablePointMatchingRatio, double);
110  itkSetMacro ( AllowableTimingError, long long);
111  void SetLeftGoldStandardPoints ( std::vector <GoldStandardPoint> points , mitk::VideoTrackerMatching::Pointer matcher );
112  void SetRightGoldStandardPoints ( std::vector <GoldStandardPoint > points, mitk::VideoTrackerMatching::Pointer matcher );
113  void SetGoldStandardObjects ( std::vector < mitk::PickedObject > pickedObjects );
114 
119  void AppendWorldPoints ( std::vector< mitk::WorldPoint > points );
120 
124  void AppendClassifierWorldPoints ( std::vector < mitk::WorldPoint > points );
128  void ClearWorldPoints ();
129 
130  std::vector < mitk::PickedPointList::Pointer > GetPointsInLeftLensCS ();
131  std::vector < mitk::ProjectedPointPairsWithTimingError > GetProjectedPoints ();
132  itkGetMacro ( InitOK, bool);
133  itkGetMacro ( ProjectOK, bool);
134  itkGetMacro ( WorldToLeftCameraMatrices, std::vector < cv::Mat > );
135  itkGetMacro ( LeftProjectionErrors, std::vector < cv::Point2d > );
136  itkGetMacro ( RightProjectionErrors, std::vector < cv::Point2d > );
137  itkGetMacro ( LeftReProjectionErrors, std::vector < cv::Point3d > );
138  itkGetMacro ( RightReProjectionErrors, std::vector < cv::Point3d > );
139  itkGetMacro ( TriangulationErrors, std::vector < cv::Point3d > );
140 
144  void CalculateProjectionErrors (std::string outPrefix);
145 
149  void CalculateTriangulationErrors (std::string outPrefix);
150 
154  void TriangulateGoldStandardPoints (std::string outPrefix, mitk::VideoTrackerMatching::Pointer trackerMatcher);
155 
159  itkSetMacro ( ProjectorScreenBuffer, double);
160 
161  itkSetMacro ( ClassifierScreenBuffer, double);
162  itkSetMacro ( TriangulatedPointsOutName, std::string );
163  itkSetMacro ( WorldPoints, mitk::PickedPointList::Pointer );
164  itkSetMacro ( ClassifierWorldPoints, mitk::PickedPointList::Pointer );
165 protected:
166 
168  virtual ~ProjectPointsOnStereoVideo();
169 
170  ProjectPointsOnStereoVideo(const ProjectPointsOnStereoVideo&); // Purposefully not implemented.
171  ProjectPointsOnStereoVideo& operator=(const ProjectPointsOnStereoVideo&); // Purposefully not implemented.
172 
173 private:
174  bool m_Visualise; //if true the project function attempts to open a couple of windows to show projection in real time
175  bool m_SaveVideo; //if true the project function will buffer frames into a object to write out.
176  bool m_CorrectVideoAspectRatioByHalvingWidth; //most of our video is very wide, if true this halves the width of he save video file
177  std::string m_VideoIn; //the video in file
178  std::string m_VideoOut; //video needs to be saved on the fly
179  std::string m_Directory; //the directory containing the data
180  std::string m_OutDirectory; //where to write out any video
181  std::string m_TriangulatedPointsOutName; // where to write out triangulated points
182  mitk::PickedPointList::Pointer
183  m_WorldPoints; //the world points to project, and their accompanying scalar values
184 
185  int m_TrackerIndex; //the tracker index to use for frame matching
186  int m_ReferenceIndex; //the reference index to use for frame matching, not used by default
187 
188  bool m_InitOK;
189  bool m_ProjectOK;
190  bool m_GoldStandardPointsClassifiedOK;
191  bool m_TriangulateOK;
192  bool m_DrawAxes;
193  bool m_LeftGSFramesAreEven; // true if the left GS frame numbers are even
194  bool m_RightGSFramesAreEven; // true if the right GS frame numbers are even
195  bool m_HaltOnVideoReadFail; //stop processing if video read fails
196  bool m_DontProject; //don't project anything, useful for just reviewing video data
197  bool m_VisualiseTrackingStatus; //draw something on screen to indicate whether tracking was working got frame
198  bool m_AnnotateWithGoldStandards; //Annotate images with the gold standard picked points
199  bool m_WriteAnnotatedGoldStandards; //Save the annotated gold standard images as png
200  bool m_WriteTrackingPositionData; //Write Out a data file describing the motion of the tracked objects for each frame
201  bool m_WriteTrackingMatrixFilesPerFrame; //Write Out tracking matrix files for each video frame
202  bool m_CorrectTrackingMatrixFileNamesForSequentialChannelSplitVideo; //when writing out tracking matrices we halve the frame nuber and only write out every second frame.
203  int m_RightGSFrameOffset; //0 if right and left gold standard points have the same frame number
204  int m_MaxGoldStandardPointIndex; //useful if we're just triangulating gold standard points
205  int m_MaxGoldStandardLineIndex; //useful if we're just triangulating gold standard points
206 
207  unsigned int m_StartFrame; //you can exclude some frames at the start
208  unsigned int m_EndFrame; // and at the end
209 
210  double m_ProjectorScreenBuffer; // A buffer around the screen beyond which projected points will be set to infinity
211  double m_ClassifierScreenBuffer; // A buffer around the screen, beyond which projected classifier points will be set to infinity
212 
213  double m_ReprojectionErrorZLimit; // The method for reprojecting lines can leave a residual error in the z direction, if greater than this we reject it
214  //the camera calibration parameters
215  cv::Mat* m_LeftIntrinsicMatrix;
216  cv::Mat* m_LeftDistortionVector;
217  cv::Mat* m_RightIntrinsicMatrix;
218  cv::Mat* m_RightDistortionVector;
219  cv::Mat* m_RightToLeftRotationMatrix;
220  cv::Mat* m_RightToLeftTranslationVector;
221  cv::Mat* m_LeftCameraToTracker;
222 
223  //the dimensions of the video screen in pixels
224  double m_VideoWidth;
225  double m_VideoHeight;
226 
227  std::vector < mitk::PickedPointList::Pointer > m_ProjectedPointLists; // the projected objects
228  std::vector < mitk::PickedPointList::Pointer > m_PointsInLeftLensCS; // the points in left lens coordinates.
230  m_ScreenAxesPoints; // the projected axes points
231 
232  std::vector < cv::Mat > m_WorldToLeftCameraMatrices; // the saved camera positions
233 
234  // a bunch of stuff for calculating errors
235  std::vector < mitk::PickedObject > m_GoldStandardPoints; //for calculating errors, the gold standard screen points
236  std::vector < mitk::PickedObject > m_TriangulatedGoldStandardPoints; //for calculating errors, triangulated into left lens coordinates, where possible.
237  mitk::PickedPointList::Pointer m_ClassifierWorldPoints; //the world points to project, to classify the gold standard screen points
238  std::vector < mitk::PickedPointList::Pointer >
239  m_ClassifierProjectedPointLists; // the projected points used for classifying the gold standard screen points
240 
241  std::vector < cv::Point2d > m_LeftProjectionErrors; //the projection errors in pixels
242  std::vector < cv::Point2d > m_RightProjectionErrors; //the projection errors in pixels
243  std::vector < cv::Point3d > m_LeftReProjectionErrors; // the projection errors in mm reprojected onto a plane normal to the camera lens
244  std::vector < cv::Point3d > m_RightReProjectionErrors; // the projection errors in mm reprojected onto a plane normal to the camera lens
245  std::vector < cv::Point3d > m_TriangulationErrors; // the triangulation errors
246 
247  cv::VideoCapture* m_Capture;
248  CvVideoWriter* m_LeftWriter;
249  CvVideoWriter* m_RightWriter;
250 
251  double m_AllowablePointMatchingRatio; // the minimum allowable ratio between the 2 nearest points when matching points on screen
252 
253  long long m_AllowableTimingError; // the maximum permisable timing error when setting points or calculating projection errors;
254  void ProjectAxes();
255 
256  /* \brief
257  * calculates the x and y errors between the passed point and the nearest point in
258  * m_ProjectedPoints, adds result to m_LeftProjectionErrors or m_RightProjectionErrors
259  */
260  void CalculateProjectionError ( mitk::PickedObject GSPoint );
261 
262  /* \brief
263  * calculates the x,y, and z error between the passed point and the nearest point in
264  * m_ProjectedPoints when projected onto a plane distant from the camera
265  * appends result to m_LeftReProjectionErrors or m_RightReProjectionErrors
266  */
267  void CalculateReProjectionError ( mitk::PickedObject GSPoint );
268 
269  /* \brief
270  * Finds the nearest point in
271  * m_ProjectedPoints, and assigns the index to the point if necessary. If a point is found it assigns the index
272  * to the point. Returns true if point is found.
273  */
274  bool FindNearestScreenPoint ( mitk::PickedObject& GSPoint );
275 
276  /* \brief
277  * goes through a picked point list and returns the object that corresponds to that passed.
278  */
279  mitk::PickedObject GetMatchingPickedObject ( const mitk::PickedObject& po, const mitk::PickedPointList& list );
280 
281  /* \brief
282  * Undistorts a picked object
283  */
284  mitk::PickedObject UndistortPickedObject ( const mitk::PickedObject& po );
285 
286  /* \brief
287  * Reprojects a picked object
288  */
289  mitk::PickedObject ReprojectPickedObject ( const mitk::PickedObject& po, const mitk::PickedObject& depthReference );
290 
291  /* \brief
292  * Projects a picked point list from left lens space to screen space. Uses the framenumber to
293  * determine whether to project to left or right screen (even is left screen)
294  */
295  mitk::PickedPointList::Pointer ProjectPickedPointList ( const mitk::PickedPointList::Pointer po_leftLens, const double& screenBuffer );
296 
297  /* \brief
298  * Triangulates gold standard picked objects, populating m_TriangulatedGoldStandardObjects
299  */
300  bool TriangulateGoldStandardObjectList ( );
301 
302  /* \brief
303  * Triangulates a pair of picked objects into the coordinates of the left lens
304  */
305  mitk::PickedObject TriangulatePickedObjects ( const mitk::PickedObject po_leftScreen, const mitk::PickedObject po_rightScreen );
306 
307  /* \brief
308  * Multiplies a picked point list by a matrix
309  */
310  mitk::PickedPointList::Pointer TransformPickedPointListToLeftLens ( const mitk::PickedPointList::Pointer po, const cv::Mat& transform, const unsigned long long& timestamp, const int& framenumber );
311 
312  /* \brief
313  * scans through the vector of gold standard points and classifies them (useful if un ordered picking was used),
314  * This must be run after project and before any error calculation
315  */
316  void ClassifyGoldStandardPoints ();
317  /* \brief use this this find video data, used m_Directory and set m_VideoIn
318  */
319  void FindVideoData (mitk::VideoTrackerMatching::Pointer trackerMatcher);
320 
321 }; // end class
322 
323 } // end namespace
324 
325 #endif
std::vector< std::string > FindVideoData(const std::string &directory)
Definition: niftkFileHelper.cxx:443
Definition: ReceptorMemberCommandTest.cxx:25
GLuint GLenum GLenum transform
Definition: glew.h:12775
Definition: mitkProjectPointsOnStereoVideo.h:43
Takes an input video file and tracking data. The video is split into right and left channels...
Definition: mitkOpenCVPointTypes.h:172
Derived point types to contain data for projection and analysis.
#define NIFTKOPENCV_EXPORT
Definition: niftkSequentialCpuQds.h:27
GLuint GLdouble GLdouble GLint GLint const GLdouble * points
Definition: glew.h:3085
Definition: mitkOpenCVPointTypes.h:219
Definition: mitkOpenCVPointTypes.h:129
GLsizei const GLcharARB ** string
Definition: glew.h:5194