How to connect xBox kinect to opencv in ubuntu?

I want to work with kinect with openCV in ubuntu (14.04) using C ++. I am installing openni and libfreenect.

when I type lsusb in the terminal, the system response text matches me.

Bus 003 Device 005: ID 045e:02ae Microsoft Corp. Xbox NUI Camera
Bus 003 Device 003: ID 045e:02b0 Microsoft Corp. Xbox NUI Motor
Bus 003 Device 004: ID 045e:02ad Microsoft Corp. Xbox NUI Audio

      

when I type freenect-glview in terminal, the system response text matches me.

Kinect camera test
Number of devices found: 1

      

and the system shows RGB and depth.

also, I activated openni when cmake opencv (-D WITH_OPENNI: ON), and after the system shows:

openni: yes
prime-sensor-kinect : yes

      

I am compiling the code:

g++ -o test1   test1.cpp `pkg-config opencv --cflags --libs` 

      

but when i run the code the system error is:

CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate production trees: Can't create 

      

any node of the requested type!

code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/opencv.hpp"
#include <vector>
#include <stdio.h>
using namespace cv;
int main(int, char**)
{
    VideoCapture cap(CV_CAP_OPENNI); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

      

and when running python code system error:

CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate production trees: Can't create any node of the requested type!
0.0
Unable to Retrieve Disparity Map from camera

      

python code:

import cv2
import cv2.cv as cv

capture = cv2.VideoCapture(cv.CV_CAP_OPENNI)
capture.set(cv.CV_CAP_OPENNI_IMAGE_GENERATOR_OUTPUT_MODE, cv.CV_CAP_OPENNI_VGA_30HZ)

print capture.get(cv.CV_CAP_PROP_OPENNI_REGISTRATION)

while True:
    if not capture.grab():
        print "Unable to Grab Frames from camera"
        break
    okay1, depth_map = capture.retrieve(0,cv.CV_CAP_OPENNI_DEPTH_MAP)
    if not okay1:
        print "Unable to Retrieve Disparity Map from camera"
        break
    okay2, gray_image = capture.retrieve(0,cv.CV_CAP_OPENNI_GRAY_IMAGE)
    if not okay2:
        print "Unable to retrieve Gray Image from device"
        break
    cv2.imshow("depth camera", depth_map)
    cv2.imshow("rgb camera", gray_image)
    if cv2.waitKey(10) == 27:
        break
cv2.destroyAllWindows()
capture.release()

      

opencv does not recognize kinect as an input device. How to solve this problem?

I apologize for the bad writing because my English is bad.

+3


source to share


2 answers


If you've installed libfreenect and opencv, you should be able to run the following python script:



import freenect
import cv2
import numpy as np
from functions import *

def nothing(x):
    pass   
kernel = np.ones((5, 5), np.uint8)     

def pretty_depth(depth):
    np.clip(depth, 0, 2**10 - 1, depth)
    depth >>= 2
    depth = depth.astype(np.uint8)
    return depth

while 1:
    orig = freenect.sync_get_video()[0]
    orig = cv2.cvtColor(orig,cv2.COLOR_BGR2RGB)
    dst = pretty_depth(freenect.sync_get_depth()[0])#input from kinect
    cv2.imshow('Disparity', dst)
    cv2.imshow('RGB',orig)
    if cv2.waitKey(1) & 0xFF == ord('b'):
        break

      

0


source


I came across this thread while finding myself in a similar situation. I was only able to get sensor data from OpenCV after installing PrimeSense modules for OpenNI, which you can find here here . After following the instructions listed in the README for my system (Ubuntu 14.04.5), I was able to run this code:



#include <cstdio>
#include <opencv2/opencv.hpp>

int main(int argc, char **argv){
  cv::VideoCapture capture(CV_CAP_OPENNI);

  cv::Mat image;
  cv::Mat bgrImage;

  while(true){
    capture.grab();
    capture.retrieve(image, CV_CAP_OPENNI_DEPTH_MAP);
    capture.retrieve(bgrImage, CV_CAP_OPENNI_BGR_IMAGE);
    imshow("Image", image);
    imshow("Color", bgrImage);
    if(cv::waitKey(30) >= 0) break;
  }
  return 0;
}

      

+2


source







All Articles