diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a9dbfbc02..1ddbeeb3eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -598,6 +598,7 @@ option(BUILD_UNIT_TESTS "Build realsense unit tests." ON) option(BUILD_EXAMPLES "Build realsense examples and tools." ON) option(ENFORCE_METADATA "Require WinSDK with Metadata support during compilation. Windows OS Only" OFF) option(BUILD_PYTHON_BINDINGS "Build Python bindings" OFF) +option(BUILD_CV_EXAMPLES "Build OpenCV examples" OFF) option(BUILD_NODEJS_BINDINGS "Build Node.js bindings" OFF) # This parameter is meant for disabling graphical examples when building for @@ -629,6 +630,10 @@ if (BUILD_NODEJS_BINDINGS) add_subdirectory(wrappers/nodejs) endif() +if (BUILD_CV_EXAMPLES) + add_subdirectory(wrappers/opencv) +endif() + # Check for unreferenced files FILE(GLOB_RECURSE AllSources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.c" "src/*.cpp" "src/*.cc" "src/*.c++" diff --git a/wrappers/opencv/CMakeLists.txt b/wrappers/opencv/CMakeLists.txt new file mode 100644 index 0000000000..e23c3465eb --- /dev/null +++ b/wrappers/opencv/CMakeLists.txt @@ -0,0 +1,27 @@ +# minimum required cmake version: 3.1.0 +cmake_minimum_required(VERSION 3.1.0) + +project(RealsenseCVExamples) + +# Save the command line compile commands in the build output +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) +# View the makefile commands during build +#set(CMAKE_VERBOSE_MAKEFILE on) + +include(CheckCXXCompilerFlag) +CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) +CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) +if(COMPILER_SUPPORTS_CXX11) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +elseif(COMPILER_SUPPORTS_CXX0X) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") +endif() + +find_package(OpenCV REQUIRED) +if(NOT OPENCV_FOUND) + message(FATAL_ERROR "\n\n OpenCV package is missing!\n\n") +endif() + +set(DEPENDENCIES realsense2 ${OpenCV_LIBS}) + +add_subdirectory(imshow) diff --git a/wrappers/opencv/imshow/CMakeLists.txt b/wrappers/opencv/imshow/CMakeLists.txt new file mode 100644 index 0000000000..60df72d7bd --- /dev/null +++ b/wrappers/opencv/imshow/CMakeLists.txt @@ -0,0 +1,34 @@ +# minimum required cmake version: 3.1.0 +cmake_minimum_required(VERSION 3.1.0) + +project(RealSenseImShowExample) + +# Save the command line compile commands in the build output +set(CMAKE_EXPORT_COMPILE_COMMANDS 1) + +include(CheckCXXCompilerFlag) +CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) +CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) +if(COMPILER_SUPPORTS_CXX11) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +elseif(COMPILER_SUPPORTS_CXX0X) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") +endif() + +if(BUILD_CV_EXAMPLES) + add_executable(rs-imshow rs-imshow.cpp) + target_link_libraries(rs-imshow ${DEPENDENCIES}) + set_target_properties (rs-imshow PROPERTIES + FOLDER "Examples/CV" + ) + + install( + TARGETS + + rs-imshow + + RUNTIME DESTINATION + ${CMAKE_INSTALL_PREFIX}/bin + ) +endif() diff --git a/wrappers/opencv/imshow/rs-imshow.cpp b/wrappers/opencv/imshow/rs-imshow.cpp new file mode 100644 index 0000000000..c9e0619e3c --- /dev/null +++ b/wrappers/opencv/imshow/rs-imshow.cpp @@ -0,0 +1,54 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2017 Intel Corporation. All Rights Reserved. + +#include // Include RealSense Cross Platform API +#include // Include OpenCV API + +// Capture Example demonstrates how to +// capture depth and color video streams and render them to the screen +int main(int argc, char * argv[]) try +{ + // Declare depth colorizer for pretty visualization of depth data + rs2::colorizer color_map; + + // Declare RealSense pipeline, encapsulating the actual device and sensors + rs2::pipeline pipe; + // Start streaming with default recommended configuration + pipe.start(); + + using namespace cv; + const auto window_name = "Display Image"; + namedWindow(window_name, WINDOW_AUTOSIZE); + + while (waitKey(1) < 0 && cvGetWindowHandle(window_name)) + { + rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera + + rs2::frame depth = color_map(data.get_depth_frame()); // Find and colorize the depth data + + // Query frame size (width and height) + auto w = depth.as().get_width(); + auto h = depth.as().get_height(); + + // Create OpenCV matrix of size (w,h) from the colorized depth data + Mat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP); + + // Update the window with new data + imshow(window_name, image); + } + + return EXIT_SUCCESS; +} +catch (const rs2::error & e) +{ + std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl; + return EXIT_FAILURE; +} +catch (const std::exception& e) +{ + std::cerr << e.what() << std::endl; + return EXIT_FAILURE; +} + + + diff --git a/wrappers/opencv/readme.md b/wrappers/opencv/readme.md new file mode 100644 index 0000000000..31e31449b6 --- /dev/null +++ b/wrappers/opencv/readme.md @@ -0,0 +1,46 @@ +# OpenCV Samples for IntelĀ® RealSenseā„¢ cameras +**Code Examples to start prototyping quickly:** These simple examples demonstrate how to easily use the SDK to include code snippets that access the camera into your applications. + +For a detailed explanations and API documentation see our [Documentation](../doc) section + +## List of Samples: +1. [ImShow](./imshow) - Minimal OpenCV application for visualizing depth data + +## Getting Started: +This page is certainly **not** a comprehensive guide to getting started with OpenCV and CMake, but it can help get on the right track. + +### Windows +1. Download and install `CMake` from [cmake.org/download](https://cmake.org/download/) +2. Clone or download OpenCV sources from [github.com/opencv/opencv](https://github.com/opencv/opencv) into a local directory (`C:/git/opencv-master`) +3. Run `cmake-gui`, input source code and binaries locations: + +![1](res/1.PNG) + +4. Click `Configure` +> When working behind a firewall, you might want to consider unchecking `WITH_FFMPEG` and `WITH_IPP` to avoid additional downloads +5. Uncheck `BUILD_SHARED_LIBS`: + +![2](res/2.PNG) + +6. Click `Generate` +7. Click `Open Project` to open Visual Studio +8. Press `Ctrl+Shift+B` to build solution +9. Clone or download librealsense sources from [github.com/IntelRealSense/librealsense](https://github.com/IntelRealSense/librealsense) into a local directory (`C:/git/librealsense`) +10. Run `cmake-gui` and fill source code and binaries locations and press `Configure` +11. Make sure you check the `BUILD_CV_EXAMPLES` flag and click `Configure` again: + +![3](res/3.PNG) + +12. Specify CMake binaries folder for OpenCV as `OpenCV_DIR` (`c:/git/opencv-master`) + +![4](res/4.PNG) + +13. Click `Generate` and `Open Project` +14. Locate CV solution-folder under Examples + +![5](res/5.PNG) + +15. Right-click on one of the examples to `Set as StartUp Project` +16. Press `F5` to compile and run the example: + +![6](res/6.PNG) diff --git a/wrappers/opencv/res/1.PNG b/wrappers/opencv/res/1.PNG new file mode 100644 index 0000000000..72d7ec4d0b Binary files /dev/null and b/wrappers/opencv/res/1.PNG differ diff --git a/wrappers/opencv/res/2.PNG b/wrappers/opencv/res/2.PNG new file mode 100644 index 0000000000..dbd02300ed Binary files /dev/null and b/wrappers/opencv/res/2.PNG differ diff --git a/wrappers/opencv/res/3.PNG b/wrappers/opencv/res/3.PNG new file mode 100644 index 0000000000..d5f8002de6 Binary files /dev/null and b/wrappers/opencv/res/3.PNG differ diff --git a/wrappers/opencv/res/4.PNG b/wrappers/opencv/res/4.PNG new file mode 100644 index 0000000000..789cc30f6b Binary files /dev/null and b/wrappers/opencv/res/4.PNG differ diff --git a/wrappers/opencv/res/5.PNG b/wrappers/opencv/res/5.PNG new file mode 100644 index 0000000000..4cbd851c29 Binary files /dev/null and b/wrappers/opencv/res/5.PNG differ