how use kinect(openni) in opencv - linux(ubuntu14) - c++

I trying to use kinect on OPENCV and I realized that openni is the best library for kinect
but I could not find the latest version of it(Because the site is closed)
and i am using openni1.5.4 nite1.5.2 sensorkinect5.1.2.1 and opencv2.4.9 in ubuntu14.04LST
kinect installed correctly and i can run sample project from openni folder
now i want to use openni in opencv project, like this
VideoCapture sensor1;
sensor1.open(CV_CAP_OPENNI);
but It doesn't work and I noticed this error can be from cmake variable so i remake opencv build files with
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_OPENNI=ON ..
and in terminal i can see
OpenNI: YES (ver 1.5.4, build 0)
-- OpenNI PrimeSensor Modules: YES (/usr/lib/libXnCore.so)
but when i use
cout << cv::getBuildInformation();
in my project , i get this
OpenNI: NO
OpenNI PrimeSensor Modules: NO
Where is the problem and What is the solution?
I thank you in advance

Finally the problem was solved
i installed libfreenect like this
mkdir -p ~/Developer/Work
cd ~/Developer/Work
git clone https://github.com/OpenKinect/libfreenect
cd libfreenect
git checkout tags/v0.3.0
NOTE: Tested with libfreenect stable release v0.3.0
Build and install:
cd ~/Developer/Work/libfreenect mkdir build cd build cmake .. -D BUILD_OPENNI2_DRIVER=ON make sudo make install
cd ~/Developer/Work/libfreenect
mkdir build
cd build
cmake .. -D BUILD_OPENNI2_DRIVER=ON
make
sudo make install
Add to library path:
sudo nano /etc/ld.so.conf.d/custom.conf
Add
/usr/local/lib/ /usr/local/lib/OpenNI2-FreenectDriver/
And
sudo ldconfig
Test with glview:
sudo glview
in cmake i use BUILD_OPENNI2_DRIVER=ON but i didn't install openni2 , i use openni1.5.4
and Be sure to use a computer not VMware (i couldn't get stream on VMware)

Related

What is a correct way to setup protobuf and grpc for cpp project?

I have a cpp project, and I want to build everything from source, to get latest things likned in. So under my project root I've created a 3rd_party folder and assemble following script:
sudo apt-get install autoconf automake libtool curl make g++ unzip -y
git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig
cd ..
echo installing grpc
git clone --recurse-submodules -b v1.43.0 https://github.com/grpc/grpc
export MY_INSTALL_DIR=$HOME/.local
be sure that its exists
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR ../..
make -j
make install
popd
i've encountered following flaws:
grpc v1.43.0 clones 3rd party submodule protoc v3.18, which after build has problems - protoc binary says "cpp plugin not found", when trying to generate
To overcome that I've copied sources obtained in the first part of script, to 3rd party subfolder of second part, to gurantee it to be 3.19 - then after compilation protoc working great, plugins are in place, and grpc is linked against latest version.
Very weird issue, needs understanding why it clones outdated version, and why 3.18 has no plugins under same build parameters as 3.19
in cmakelists find_package(Protobuf REQUIRED) fails at all
find_package( gRPC REQUIRED ) is not operating properly:
Found package configuration file:
[cmake]
[cmake] /home/a/.local/lib/cmake/grpc/gRPCConfig.cmake
[cmake]
[cmake] but it set gRPC_FOUND to FALSE so package "gRPC" is considered to be NOT
[cmake] FOUND. Reason given by package:
[cmake]
[cmake] The following imported targets are referenced, but are missing:
[cmake] protobuf::libprotobuf protobuf::libprotoc
Question
How can I write script, that in an empty folder will get me everything, related to grpc 3rd party libs and tools, that I need for project?
Here are the steps I took:
$ mkdir .local
$ export INSTALL_PREFIX="$PWD/.local"
$ export CMAKE_GENERATOR=Ninja # optional, but recommended
$ git clone --recurse-submodules -b v3.19.3 --depth 1 https://github.com/google/protobuf.git
$ pushd protobuf
$ ./autogen.sh
$ ./configure --prefix=$INSTALL_PREFIX
$ make -j$(nproc)
$ make install
$ popd
$ git clone --recurse-submodules -b v1.43.0 --depth 1 https://github.com/grpc/grpc.git
$ cmake -S grpc -B .build/grpc \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
-DCMAKE_PREFIX_PATH=$INSTALL_PREFIX \
-DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
-DgRPC_PROTOBUF_PROVIDER=package \
-DABSL_PROPAGATE_CXX_STD=ON
$ cmake --build .build/grpc --target install
...
$ mkdir example
$ echo "int main () { return 0; }" > example/main.cpp
$ vim example/CMakeLists.txt
$ cat example/CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(example)
find_package(gRPC REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE gRPC::grpc++)
$ cmake -S example -B .build/example \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=$INSTALL_PREFIX
$ cmake --build .build/example
...
I build protobuf separately because gRPC doesn't set up its dependencies in CMake correctly when using its internal protobuf build (I tried both ways). This involves passing --prefix to protobuf's ./configure script and passing -DgRPC_PROTOBUF_PROVIDER=package to gRPC's CMake build. The latter is a gRPC-specific variable that tells it not to build protobuf, but to search for it instead. I tell gRPC where to find protobuf by setting CMAKE_PREFIX_PATH, which is a standard variable.

Opencv 3.2.0 python install

I using python opencv3.2 in ubuntu14.04,install follow open-python 3.2.0.7 : Python Package Index, and installed by pip but still get error when call cv2.imshow()
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /io/opencv/modules/highgui/src/window.cpp, line 583
, so I try build opencv 3.2.0 followed Install OpenCV 3.0 and Python 2.7+ on Ubuntu,but i didn't see the cv2.so after make install
CMake: cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
where i can find cv2.so ?
Sounds like CMake never found your Python dev libararies and/or numpy libraries.
Be sure to sudo apt install libpython2.7-dev and sudo pip install numpy then specify to CMake where these libraries are using the flags:
-D PYTHON2_INCLUDE_DIR=/usr/include/python2.7 \
-D PYTHON2_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so \
-D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include \
-D PYTHON2_PACKAGES_PATH=/usr/lib/python2.7/dist-packages
The above is for python 2.7 only.
then build and install:
make -j$(nproc)
sudo make -j$(nproc) install
The cv2.so file should now reside in your python site-packages or dist-packages folder.

How do I cross compile OpenCV for the Raspberry Pi with additional modules (highgui...)

My goal is to cross-compile OpenCV for Raspberry Pi with CMake on an Ubuntu 16.04 Host. The Problem lies within the CMake toolchain file which is unable to handle the compilation process correctly. As soon as pkg-config comes into play, a lot of "fatal error: No such file or directory" errors arise (e.g. "fatal error: gtk/gtk.h: No such file or directory).
I figured out that further "-isystem" flags with the directories containing the missing files seem to help in some way, but I doubt that this is the right way to address these errors.
The toolchain file works, as long as the "PKG_CONFIG"-stuff is commented out. This causes pkg-config not to find "libgtk2.0", which is needed for OpenCV's highgui module and builds without highgui-support.
Some ideas for the toolchain are from here:
Cmake cross compile cant find library
Raspberry Pi Beginners Guide
Cross compile opencv project on Ubuntu for Raspberry Pi
build-toolchain.sh
#!/bin/bash
# apt -y install lib32z1 libstdc++6:i386 git qemu-user proot cmake pkg-config
MOUNT_DIR=/mnt/rasp-pi-rootfs
TEMP_DIR=/tmp/build
RASPBIAN_IMG_ZIP=raspbian-jessie.zip
TOOLCHAIN=gcc-linaro-arm-linux-gnueabihf-raspbian-x64
CMAKE_TOOLCHAIN_FILE=rpi-toolchain.cmake
############################################################################
RASPBIAN_IMG=${TEMP_DIR}/$(unzip -Z1 $RASPBIAN_IMG_ZIP)
rm -rf $TEMP_DIR
mkdir $TEMP_DIR
cd $TEMP_DIR
#unzip raspbian-img
unzip $RASPBIAN_IMG_ZIP
#resize raspbian-img
LOOP_DEVICE=$(losetup -f)
RASPBIAN_PARTITION_BOOT=${LOOP_DEVICE}p1
RASPBIAN_PARTITION_SYSTEM=${LOOP_DEVICE}p2
truncate -s +500M $RASPBIAN_IMG
losetup $LOOP_DEVICE $RASPBIAN_IMG
parted $LOOP_DEVICE resizepart 2 100%
#wait until auto-mounted
umount $RASPBIAN_PARTITION_BOOT
umount $RASPBIAN_PARTITION_SYSTEM
e2fsck -f $RASPBIAN_PARTITION_SYSTEM
resize2fs $RASPBIAN_PARTITION_SYSTEM
#mount raspbian-img
mkdir -p $MOUNT_DIR
mount $RASPBIAN_PARTITION_SYSTEM $MOUNT_DIR
#fix absolute links
proot -q qemu-arm -S $MOUNT_DIR<<EOF
apt update
apt -y install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev \
libgtk2.0-dev libatlas-base-dev gfortran
apt -y install symlinks
symlinks -cr /usr/lib
EOF
wget -O opencv.zip https://codeload.github.com/Itseez/opencv/zip/2.4.13
unzip opencv.zip
cd opencv-*
mkdir build
cd build
cmake -D CMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE -D PIROOT=$MOUNT_DIR -D TOOLROOT=$TOOLCHAIN -D CMAKE_BUILD_TYPE=Release -D WITH_V4L=ON -D WITH_OPENGL=ON ..
make -j $(nproc)
make install
sync; umount $MOUNT_DIR
losetup -d $LOOP_DEVICE
rpi-toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-g++)
set(CMAKE_SYSROOT ${PIROOT})
set(CMAKE_FIND_ROOT_PATH ${PIROOT})
unset(ENV{PKG_CONFIG_DIR})
set(ENV{PKG_CONFIG_LIBDIR} "$ENV{PKG_CONFIG_LIBDIR}:${PIROOT}/usr/lib/arm-linux-gnueabihf/pkgconfig:${PIROOT}/usr/share/pkgconfig:${PIROOT}/usr/lib/pkgconfig")
set(ENV{PKG_CONFIG_SYSROOT_DIR} "${PIROOT}")
set(FLAGS "-isystem ${PIROOT}/usr/include/arm-linux-gnueabihf")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_INSTALL_PREFIX ${PIROOT}/usr/local)
Got it. The Problem was the setting of PKG_CONFIG-vars within the rpi-toolchain.cmake file, this does not work .They have to be added to the environment with export:
...
export PKG_CONFIG_SYSROOT_DIR=${MOUNT_DIR}
export PKG_CONFIG_LIBDIR=${MOUNT_DIR}/usr/lib/arm-linux-gnueabihf/pkgconfig:${MOUNT_DIR}/usr/share/pkgconfig:${MOUNT_DIR}/usr/lib/pkgconfig
export PKG_CONFIG_DIR=
cmake -D CMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE -D PIROOT=$MOUNT_DIR -D TOOLROOT=$TOOLCHAIN -D CMAKE_BUILD_TYPE=Release -D WITH_V4L=ON -D WITH_OPENGL=ON ..
make -j $(nproc)
edit: I don't remember what the actual problem was here, but setting pkg-config variables with ENV{} works and was not the culprit.

Linux, Eclipse C++, how to connect ffmpeg libs to your project?

So I downloaded FFmpeg from svn. Compiled and installed into system. in /usr/local it created ./lib and ./include. one with includes and one with libs in form of *.a files.
I downloaded eclipse Helios for C++ and created new simple project. I included ffmpeg headers into my C++ file. in project properties in C/C++Build in settings I declared ffmpeg libs (Library search path was declared)
I wrote some simple code that calls functions from headers.
But for all ffmpeg functions I used it gave me errors of eclipse not seeing ffmpeg.
So I wonder - how to connect ffmpeg libs to my project (or may be there is any way to compile ffmpeg not into .a and it would work with .so)?
I had the same problem on my ubuntu 12.04 and I fixed it by compiling ffmpeg and x264 in this way:
I removed the folders of ffmpeg and x264 and then typed this in the terminal:
sudo apt-get remove ffmpeg x264
sudo apt-get autoremove
and then
sudo apt-get update
sudo apt-get upgrade
then
sudo apt-get install make automake g++ bzip2 python unzip patch subversion ruby build-essential git-core checkinstall yasm texi2html libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libvdpau-dev libvorbis-dev libvpx-dev libx11-dev libxfixes-dev libxvidcore-dev zlib1g-dev
installing x264
sudo git clone git://git.videolan.org/x264.git
cd x264
sudo ./configure --enable-shared --prefix=/usr/local
sudo make
sudo make install
cd ..
installing libvpx
sudo wget http://webm.googlecode.com/files/libvpx-v0.9.7-p1.tar.bz2
sudo tar xvjf libvpx-v0.9.7-p1.tar.bz2
cd libvpx-v0.9.7-p1
sudo ./configure --enable-shared --prefix=/usr/local
sudo make
sudo make install
cd ..
check if /etc/ld.so.conf contains the lines /usr/lib and /usr/local/lib. if not then add them and save it
then run
sudo ldconfig
installing ffmpeg
sudo wget http://ffmpeg.org/releases/ffmpeg-0.8.10.tar.bz2
sudo tar xvjf ffmpeg-0.8.10.tar.bz2
cd ffmpeg-0.8.10
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-shared --prefix=/usr/local
sudo make
sudo make install
cd ..
sudo ldconfig
in your project you should include the headers in this way:
#include<libavformat/avformat.h>
#include<libavcodec/avcodec.h>
In eclipse you should add the ffmpeg libraries to your project: avutil, avformat, avcodec, m, z ... by going to project->properties->C/C++->Build->Settings->GCC C++-> Linker->Libraries
I hope this works for you
You need to link them with your code. Use -l and -L gcc command line parameters in your make file, or edit the properties for your Eclipse project (go to the project properties->C/C++->Build->Settings->GCC C++-> Linker->Libraries.
if the includes and libraries that you want are in /usr/local/include and /usr/local/lib respectively, seems that you are covered with include and library paths (these directories are in the default search path usually).
Verify that you didn't use lib... when specifying the library that you want to link to, this is prepended by default, e.g. to use libabc.a just specify abc in the library list of the project settings.
If you are getting compile errors (not linker errors), make sure libav* folders reside in /usr/local/include and you include ffmpeg headers with their folder names.
#include libavformat/avformat.h
#include libavcodec/avcodec.h
// ...
You have to define /usr/local/include as an include directory and not libav* folders inside. This is needed because ffmpeg headers themselves reference other headers this way.
This is dependent on platform.
You have to properly configure the ffmpeg libraries with all the right flags mingw/or cygwin. you would get .a files recognizable by eclipse.
as littleadv says you must configure the path to the ffmeg libraries. in the project ->build properties- include(ffmpeg folderand your mingw/cygwin local include ) and linker settings avformat avcodec avfilter avdevice swscale postproc .
Do not forget to set the path variable in your os. Eclipse takes this. Path should point to lib and include /local mingw/cygwin.

opencv contrib repo Installation

I am using linux and want to install opencv contrib repo to include "opencv2/xfeatures2d.hpp" file. I have read the readme file from the link : https://github.com/Itseez/opencv_contrib , but still can't understand how to use the commands in the terminal . Can anyone help ? I have installed opencv in home (~/opencv) and the folder opencv_contrib-master in Downloads.
Just clone opencv_contrib to the any directory you like then build it as:
git clone (opencv_contrib)
cd opencv (main directory) and mkdir build
cd build
Here u have to provide the linkage to the
opencv_contrib. I used the following command.
cmake -D CMAKE_BUILD_TYPE=DEBUG -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D OPENCV_EXTRA_MODULES_PATH../opencv_contrib/modules ..
I have found the solution. I had to uninstall the library and install it again, like described in this link : http://embedonix.com/articles/image-processing/installing-opencv-3-1-0-on-ubuntu/
Cut short the best way is to do
sudo apt-get install cmake-gui
go to the root of opencv folder
mkdir build
cmake-gui ..
then put the path of the opencv_extra modules
build it and install it and enjoy.