Build OpenCV from source and run examples - ashBabu/Utilities GitHub Wiki
Follow the below procedures.
git clone [email protected]:opencv/opencv.gitgit checkout 4.x- [optional]
git clone [email protected]:opencv/opencv_contrib.git git checkout 4.xcd ~/opencv && mkdir build && cd buildcmake -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 installif need to install else inCMakeLists.txt, add the followingset(OpenCV_DIR "/path_to_/opencv/build")find_package(OpenCV x.y REQUIRED )wherex.yis the version, for instance 4.6 orfind_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 Projectof theCMakeLists.txtunder thesamplesfolder - This will load
example_dnn_object_detectionbut 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 --rgbwhere the arguments are to be set inRun--> Program arguments
References
A simple color thresholding for object detection in ROS
- The example can be found here