toinsson / pyrealsense

Cross-platform ctypes/Cython wrapper to the librealsense library (v1.x)
http://pyrealsense.readthedocs.io
Apache License 2.0
121 stars 46 forks source link

Depth map of black pixels in an image is not coming #57

Closed kishankedia closed 7 years ago

kishankedia commented 7 years ago

Camera Model: SR300 Firmware Version: firmware': '3.21.0.0' Operating System & Version: macOS sierra 10.12.6 librealsense version: 1.12.1 pyrealsense version: 2.1

Required info:

I am trying to get the depth map using intel realsense SR300. However i am not getting the depth map for the black pixels of an image. example: for my hair i am getting 0 value in the depth map.

screen shot 2017-09-13 at 7 09 59 pm

I am wearing a stripes t-shirt having grey and red colour. Depth map for red stripe is generated however the grey colour depth map is not created. How to solve this problem? Even the depth of my hair, beard or anything black is not coming.

CODE:

import os
import sys
import cv2
import pyrealsense as pyrs
import numpy as np

def create_directory(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)
        return 1
    else:
        print(directory, '- already exists')
        return 0

def check_directories():
    try:
        create_directory('./data')
        create_directory('./data/depth')
        create_directory('./data/color')
        create_directory('./data/cad')
        create_directory('./data/dac')
    except:
        print("Unexpected error:", sys.exc_info()[0])
       return -1
    return 0

def main():
    file_structure = check_directories()
    if file_structure == -1:
        print('\nERROR: Directories can\'t be created, error thrown')
        return -1
    else:
        print('\nDirectories created successfully...\nLaunching camera module...')

    # Fire camera & launch streams
    serv = pyrs.Service()
    serv.start()
    cam = serv.Device(device_id = 0, streams = [pyrs.stream.ColorStream(fps=10),
                                            pyrs.stream.DepthStream(fps=10),
                                            pyrs.stream.CADStream(fps=10),
                                            pyrs.stream.DACStream(fps=10),
                                            pyrs.stream.InfraredStream(fps=10),
                                            pyrs.stream.PointStream(fps=10)])
    scale = cam.depth_scale * 1000
    print "scale = ", scale 
    # Some important variables
    flag_save_frames = True
    file_num = 0

    # Start fetching Buffer
    print('Starting Buffer...')
    while(True):
        cam.wait_for_frames()
        current_color = cam.color[:,:,::-1]
        # print current_color.shape
        current_depth = cam.depth * scale
        current_cad = cam.cad[:,:,::-1]
        current_dac = cam.dac * scale
        print "max depth", np.max(current_dac)
        print "print depth val = ",np.max(cam.dac)

        cv2.imshow('Color',current_color)
        cv2.imshow('Depth',current_depth/1000)
        cv2.imshow('CAD',current_cad)
        cv2.imshow('DAC',current_dac/1000)

        if flag_save_frames:
            num = format(file_num, '08')
            cv2.imwrite('./data/depth/' + str(num) + '.png', cam.depth)
            cv2.imwrite('./data/color/' + str(num) + '.png', current_color)
            cv2.imwrite('./data/dac/' + str(num) + '.png', cam.dac)
            cv2.imwrite('./data/cad/' + str(num) + '.png', current_cad)
            file_num += 1

        k = cv2.waitKey(1)
        if k == ord('q'):
            print('Q Pressed...\nEnding execution')
            break
        if k == ord('f'):
            if flag_save_frames:
                print('F Pressed...\nStopped fetching frames...')
                flag_save_frames = False
            else:
                print('F Pressed...\nStarted fetching frames...')
                flag_save_frames = True

    cam.stop()
    serv.stop()

    return 0

if __name__ == '__main__':
    print(__doc__)
    main()
toinsson commented 7 years ago

Due to the way the SR300 camera work (by shining IR structured light onto the scene, recording it with an IR sensor and doing some processing on the recorded pattern to infer the depth), if a material in the scene does not reflect light - might be because it is reflective (glass, water, ...) or because it absorbs the light at that wavelength (red/black colour), it will show as either undefined or infinite depth.

This is what you see in your pictures. Nothing you can do here due to the nature of the sensor you are using.

kishankedia commented 7 years ago

@toinsson can you suggest some depth camera that does not have such limitation and work in a sub-meter range. My friends tried using intel R200 camera and though the technology is almost same for both SR300 and R200, the results were drastically better for him than what i had

papr commented 7 years ago

@kishankedia Before changing the devices, try to recalibrate your camera with the Intel RealSense Camera Calibrator.

kishankedia commented 7 years ago

@papr the link says that the camera calibrator is for R200 camera and not for SR300.