Skip to content

Build OpenCV from source and run examples

Ash Babu edited this page Sep 14, 2022 · 9 revisions

Follow the below procedures.

  • git clone [email protected]:opencv/opencv.git
  • git checkout 4.x
  • [optional] git clone [email protected]:opencv/opencv_contrib.git
  • git checkout 4.x
  • cd ~/opencv && mkdir build && cd build
  • cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_GENERATE_PKGCONFIG=YES OPENCV_EXTRA_MODULES_PATH=~/.../opencv_contrib/modules/ BUILD_EXAMPLES=true ..
  • make -j$(nproc)
  • sudo make install if need to install else in CMakeLists.txt, add the following set(OpenCV_DIR "/path_to_/opencv/build") find_package(OpenCV x.y REQUIRED ) where x.y is the version, for instance 4.6 or find_package(OpenCV REQUIRED ) should also work.

To build OpenCV with CUDA

  • cmake -D WITH_CUDA=ON OPENCV_EXTRA_MODULES_PATH=~/.../opencv_contrib/modules/ .. # dont enable other flags

Note: There is a high chance that if there's any existing OpenCV installations (maybe ros specific), make sure to uninstall with sudo apt-get remove opencv* libopencv* sudo apt remove libopencv-dev python3-opencv. Check again if there are existing installations using dpkg -l | grep libopencv and pkg-config --libs opencv/opencv4 and pkg-config --modversion opencv/opencv4 . All the above should return none. After the installation pkg-config --modversion opencv/opencv4 will show opencv 3/4.4.16. Then compile librealsense. Since by now, ROS would have stopped working, so go to catkin_ws and use rosdep install --from-paths src --ignore-src -r -y from catkin_ws to re-install all ros dependencies

Running object detection example

After building opencv,

  • cd opencv/samples
  • Open with CLion by Load CMake Project of the CMakeLists.txt under the samples folder
  • This will load example_dnn_object_detection but requires certain command line arguments
  • Run example_dnn_object_detection --config=/home/ash/Ash/repo/librealsense/wrappers/opencv/ash-yolov4/models/yolov4.cfg --model=/home/ash/Ash/repo/librealsense/wrappers/opencv/ash-yolov4/models/yolov4.weights --classes=/home/ash/Ash/repo/librealsense/wrappers/opencv/ash-yolov4/coco.names --width=416 --height=416 --scale=0.00392 --rgb where the arguments are to be set in Run--> Program arguments

References

A simple color thresholding for object detection in ROS

  • The example can be found here
Clone this wiki locally