Skip to content

Commit

Permalink
Adding basic OpenCV example
Browse files Browse the repository at this point in the history
  • Loading branch information
dorodnic committed Nov 23, 2017
1 parent 13f3db2 commit 6e4774e
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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++"
Expand Down
27 changes: 27 additions & 0 deletions wrappers/opencv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions wrappers/opencv/imshow/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
54 changes: 54 additions & 0 deletions wrappers/opencv/imshow/rs-imshow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.

#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <opencv2/opencv.hpp> // 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<rs2::video_frame>().get_width();
auto h = depth.as<rs2::video_frame>().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;
}



46 changes: 46 additions & 0 deletions wrappers/opencv/readme.md
Original file line number Diff line number Diff line change
@@ -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)
Binary file added wrappers/opencv/res/1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wrappers/opencv/res/2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wrappers/opencv/res/3.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wrappers/opencv/res/4.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wrappers/opencv/res/5.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6e4774e

Please sign in to comment.