Difference between revisions of "Documentation/Nightly/Extensions/SlicerOpenCV"

From Slicer Wiki
Jump to: navigation, search
 
(4 intermediate revisions by 3 users not shown)
Line 10: Line 10:
 
Extension: [[Documentation/{{documentation/version}}/Extensions/SlicerOpenCV|SlicerOpenCV]]<br>
 
Extension: [[Documentation/{{documentation/version}}/Extensions/SlicerOpenCV|SlicerOpenCV]]<br>
 
Acknowledgments:
 
Acknowledgments:
This work was supported by a supplement to the Quantitative Image Informatics project via the NIH-National Cancer Institute Grant U24 CA180918-03 as well as U24 CA180924 Tools to Analyze Morphology and Spatially Mapped Molecular Data with Stony Brook University.<br>
+
This work was supported by a supplement to the Quantitative Image Informatics for Cancer Research (QIICR) project via the NIH-National Cancer Institute Grant U24 CA180918-03 as well as U24 CA180924 Tools to Analyze Morphology and Spatially Mapped Molecular Data with Stony Brook University.<br>
 
Author: Nicole Aucoin ({{collaborator|name|spl}})<br>
 
Author: Nicole Aucoin ({{collaborator|name|spl}})<br>
 
Contributor1: Andrey Fedorov ({{collaborator|name|spl}})<br>
 
Contributor1: Andrey Fedorov ({{collaborator|name|spl}})<br>
Line 25: Line 25:
  
 
<!-- ---------------------------- -->
 
<!-- ---------------------------- -->
{{documentation/{{documentation/version}}/module-section|Module Description}}
+
{{documentation/{{documentation/version}}/module-section|Extension Description}}
 
[[File:SlicerOpenCV-logo.png|x200px]]
 
[[File:SlicerOpenCV-logo.png|x200px]]
  
This extension provides a way for a Slicer extension developer to build against the OpenCV pacakge, by depending on the SlicerOpenCV extension.  
+
This extension provides a way for a Slicer extension developer to build against the OpenCV package, by depending on the SlicerOpenCV extension.  
 
[http://opencv.org OpenCV] (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.
 
[http://opencv.org OpenCV] (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.
 
   
 
   
Line 34: Line 34:
 
{{documentation/{{documentation/version}}/module-section|Use Cases}}
 
{{documentation/{{documentation/version}}/module-section|Use Cases}}
  
* If an extension developer needs the algorithms provided by OpenCV, they can add a module dependency on SlicerOpenCV and obtain a Slicer style library to link against and use.
+
* If an extension developer needs the algorithms provided by OpenCV, they can add a module dependency on SlicerOpenCV and obtain a Slicer-style library to link against and use.
 +
** SlicerOpenCV also provides a python wrapped OpenCV library that can be used in scripted modules.
  
 
<!-- ---------------------------- -->
 
<!-- ---------------------------- -->
 
{{documentation/{{documentation/version}}/module-section|Tutorials}}
 
{{documentation/{{documentation/version}}/module-section|Tutorials}}
 +
 +
* A Python scripted module is provided in the SlicerOpenCV extension to provide an example on how to use OpenCV from within scripted modules in Slicer:
 +
** [https://github.com/SBU-BMI/SlicerOpenCV/blob/master/SlicerOpenCV/Testing/Python/SlicerOpenCVSelfTest.py SlicerOpenCVSelfTest.py] (note: this self test will run from inside the Slicer GUI when Developer Mode has been enabled, but it currently won't run as a ctest command line test)
  
 
<!-- ---------------------------- -->
 
<!-- ---------------------------- -->
Line 45: Line 49:
  
 
<!-- ---------------------------- -->
 
<!-- ---------------------------- -->
{{documentation/{{documentation/version}}/module-section|Similar Modules}}
+
{{documentation/{{documentation/version}}/module-section|Similar Extensions}}
  
 
* The [[Documentation/{{documentation/version}}/Extensions/OpenCVExample | OpenCVExample]] extension depends on this module and provides a template for linking and using OpenCV.
 
* The [[Documentation/{{documentation/version}}/Extensions/OpenCVExample | OpenCVExample]] extension depends on this module and provides a template for linking and using OpenCV.
Line 59: Line 63:
 
{{documentation/{{documentation/version}}/module-developerinfo}}
 
{{documentation/{{documentation/version}}/module-developerinfo}}
  
* While configuring your project that depends on a built version of SlicerOpenCV, you need to set SlicerOpenCV_DIR to point to SlicerOpenCV_BUILD_DIR/inner-build - this is the location of SlicerOpenCVConfig.cmake
+
While configuring your project that depends on a built version of SlicerOpenCV, you need to set SlicerOpenCV_DIR to point to SlicerOpenCV_BUILD_DIR/inner-build - this is the location of SlicerOpenCVConfig.cmake
* Include the SlicerOpenCV extension by calling find_package(SlicerOpenCV REQUIRED). This will set OpenCV_DIR in the scope of your extension.
+
 
* This extension also provides the ITK module Video Bridge OpenCV [http://insightsoftwareconsortium.github.io/ITKBarCamp-doc/ITK/VideoProcessingUsingOpenCVBridge/index.html Video Processing Using OpenCV Bridge]
+
Include the SlicerOpenCV extension by calling find_package(SlicerOpenCV REQUIRED). This will set OpenCV_DIR in the scope of your extension.
 +
 
 +
This extension also provides the ITK module Video Bridge OpenCV [http://insightsoftwareconsortium.github.io/ITKBarCamp-doc/ITK/VideoProcessingUsingOpenCVBridge/index.html Video Processing Using OpenCV Bridge]
 +
 
 +
===C++===
 +
 
 +
Once the library is available, in C++ you can include header files and use the video IO factory through ITK to read from file:
 +
 
 +
    itk::VideoIOBase::Pointer ioReadFile = itk::VideoIOFactory::CreateVideoIO(
 +
      itk::VideoIOFactory::ReadFileMode, input);
 +
    if (!ioReadFile)
 +
      {
 +
      std::cerr << "Did not create valid VideoIO for reading from file " << std::endl;
 +
      return;
 +
      }
 +
 
 +
You can also use the OpenCV library directly to access an attached camera:
 +
 
 +
    itk::SizeValueType cameraNumber = 0;
 +
    CvCapture* cameraCapture = cvCaptureFromCAM( cameraNumber );
 +
    if (cameraCapture == ITK_NULLPTR)
 +
      {
 +
      std::cerr << "Unable to create a camera to capture from" << std::endl;
 +
      return;
 +
      }
 +
    }
 +
 
 +
===Python===
 +
 
 +
SlicerOpenCV also provides the python wrapped OpenCV library. It's bundled in the extension in the lib/Slicer-X.Y directory. A Python scripted self test was added  to the SlicerOpenCV module, [https://github.com/SBU-BMI/SlicerOpenCV/tree/master/SlicerOpenCV/Testing/Python Python tests], to provide an example of how to use the python module:
 +
 
 +
  import cv2
 +
 
 +
Note: there's some work to be done on Slicer's Python infrastructure to ensure that this always works in installed extensions with third party libraries (PYTHONPATH needs to be set correctly). A work around as illustrated as a case in the SlicerOpenCVSelfTest script is to use an explicit path to the wrapped library, cv2File, adn load it directly:
 +
 
 +
  cv2 = imp.load_dynamic('cv2', cv2File)
  
 
<!-- ---------------------------- -->
 
<!-- ---------------------------- -->

Latest revision as of 05:07, 11 November 2016

Home < Documentation < Nightly < Extensions < SlicerOpenCV


For the latest Slicer documentation, visit the read-the-docs.


Introduction and Acknowledgements

Extension: SlicerOpenCV
Acknowledgments: This work was supported by a supplement to the Quantitative Image Informatics for Cancer Research (QIICR) project via the NIH-National Cancer Institute Grant U24 CA180918-03 as well as U24 CA180924 Tools to Analyze Morphology and Spatially Mapped Molecular Data with Stony Brook University.
Author: Nicole Aucoin (SPL)
Contributor1: Andrey Fedorov (SPL)
Contributor2: Jean-Christophe Fillion-Robin (Kitware)
Contact: Nicole Aucoin, <email>nicole@bwh.harvard.edu</email>

Quantitative Image Informatics for Cancer Research  
Surgical Planning Laboratory (SPL)  
Kitware, Inc.  

Extension Description

SlicerOpenCV-logo.png

This extension provides a way for a Slicer extension developer to build against the OpenCV package, by depending on the SlicerOpenCV extension. OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.

Use Cases

  • If an extension developer needs the algorithms provided by OpenCV, they can add a module dependency on SlicerOpenCV and obtain a Slicer-style library to link against and use.
    • SlicerOpenCV also provides a python wrapped OpenCV library that can be used in scripted modules.

Tutorials

  • A Python scripted module is provided in the SlicerOpenCV extension to provide an example on how to use OpenCV from within scripted modules in Slicer:
    • SlicerOpenCVSelfTest.py (note: this self test will run from inside the Slicer GUI when Developer Mode has been enabled, but it currently won't run as a ctest command line test)

Panels and their use

The OpenCV module doesn't provide a GUI, it's intended to be used at the library level by other modules.

Similar Extensions

  • The OpenCVExample extension depends on this module and provides a template for linking and using OpenCV.
  • The Slicer Pathology extension uses the SlicerOpenCV extension

References

Information for Developers


While configuring your project that depends on a built version of SlicerOpenCV, you need to set SlicerOpenCV_DIR to point to SlicerOpenCV_BUILD_DIR/inner-build - this is the location of SlicerOpenCVConfig.cmake

Include the SlicerOpenCV extension by calling find_package(SlicerOpenCV REQUIRED). This will set OpenCV_DIR in the scope of your extension.

This extension also provides the ITK module Video Bridge OpenCV Video Processing Using OpenCV Bridge

C++

Once the library is available, in C++ you can include header files and use the video IO factory through ITK to read from file:

   itk::VideoIOBase::Pointer ioReadFile = itk::VideoIOFactory::CreateVideoIO(
     itk::VideoIOFactory::ReadFileMode, input);
   if (!ioReadFile)
     {
     std::cerr << "Did not create valid VideoIO for reading from file " << std::endl;
     return;
     }

You can also use the OpenCV library directly to access an attached camera:

   itk::SizeValueType cameraNumber = 0;
   CvCapture* cameraCapture = cvCaptureFromCAM( cameraNumber );
   if (cameraCapture == ITK_NULLPTR)
     {
     std::cerr << "Unable to create a camera to capture from" << std::endl;
     return;
     }
   }
 

Python

SlicerOpenCV also provides the python wrapped OpenCV library. It's bundled in the extension in the lib/Slicer-X.Y directory. A Python scripted self test was added to the SlicerOpenCV module, Python tests, to provide an example of how to use the python module:

 import cv2

Note: there's some work to be done on Slicer's Python infrastructure to ensure that this always works in installed extensions with third party libraries (PYTHONPATH needs to be set correctly). A work around as illustrated as a case in the SlicerOpenCVSelfTest script is to use an explicit path to the wrapped library, cv2File, adn load it directly:

 cv2 = imp.load_dynamic('cv2', cv2File)