使用Pykinect和Open CV保存Kinect RGB和深度图像

2024-05-23 22:17:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在windows中使用带有OpenCV的PyKinect来保存RGB和kinect深度图像。 它可以很好地显示RGB和深度图像。RGB和深度图像的帧速率都是30FPS。在

我想将RGB图像保存为.jpg格式,深度图像以.xml格式保存。这也很好用。在

但我的问题是,当我开始保存RGB和深度图像时,我得到的帧速率不一样。就像如果我保存图像10秒,我会得到300个XML文件,但同时我会得到100个JPG图像。在

但是如果我执行同样的程序只保存RGB图像(注释掉深度图像部分)10秒,我会得到300个JPG文件。所以我认为这是一个性能问题,可以改进。如有任何改进性能的建议,我们将不胜感激。在

我的方法是:

from pykinect import nui
from pykinect.nui import JointId
import numpy
from numpy import * 
import cv2
import datetime


import os
import os.path
from os.path import join as pjoin
from pykinect.nui import SkeletonTrackingState

current_directory = os.getcwd()

kinect = nui.Runtime()
save_image = False


def getColorImage(frame):
    height,width = frame.image.height,frame.image.width #get width and height of the images 
    rgb = numpy.empty((height,width,4),numpy.uint8) 
    frame.image.copy_bits(rgb.ctypes.data) #copy the bit of the image to the aray   
    cv2.imshow('KINECT Video Stream', rgb) # display the image
    # save to Folder
    folder = 'RGB_images'+'\\s'+ subject_id +'_a' +activity_id
    if not os.path.exists(folder):
        os.makedirs(folder)
    path = current_directory+str('\\')+folder
    image_name = 'color_image_'+str(frame.frame_number)+'.jpg'
    if save_image:
        cv2.imwrite(os.path.join(path,image_name), rgb)


def getDepthImage(frame):
    height,width = frame.image.height,frame.image.width #get frame height and width
    depth_frame = saveDepthImageData(frame.frame_number)
    depth = numpy.empty((height,width,1),numpy.uint8)   
    arr2d = (depth >> 3) & 4095 
    arr2d >>= 4

    frame.image.copy_bits(arr2d.ctypes.data)

    cv2.imshow('KINECT depth Stream', arr2d)

    folder = 'Depth_data'+'\\s'+ subject_id +'_a' +activity_id
    if not os.path.exists(folder):
        os.makedirs(folder)     
    path = current_directory+str('\\')+folder
    file_name = 'depth_image_'+str(frame.frame_number)+'.xml'
    file_to_save = os.path.join(path,file_name)
    if save_image:      
        f = cv2.FileStorage(file_to_save,flags=1)
        f.write("depthImg",arr2d)
        f.release() #close the file


def main():
    global save_image
    global subject_id
    global activity_id


    subject_id = raw_input("Subject id : ")
    activity_id = raw_input("Activity id : ")
    print "Press t to start saving"
    print "Press Esc to quit"

    kinect.video_frame_ready += getColorImage
    kinect.video_stream.open(nui.ImageStreamType.Video, 2,nui.ImageResolution.Resolution640x480,nui.ImageType.Color)
    cv2.namedWindow('KINECT Video Stream', cv2.WINDOW_AUTOSIZE)

    kinect.depth_frame_ready += getDepthImage
    kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution320x240, nui.ImageType.Depth)
    cv2.namedWindow('KINECT depth Stream', cv2.WINDOW_AUTOSIZE)

    while True:
        key = cv2.waitKey(0)        
        if key == 116:
            save_image = not save_image
        if key == 27:           
            cv2.destroyAllWindows()
            kinect.close()
            break   


if __name__ == '__main__':
    main()

Tags: path图像imageimportidossavergb
1条回答
网友
1楼 · 发布于 2024-05-23 22:17:23

将深度图像转换为XML将大大增加其大小。Kinect的深度图像是640x480 IIRC,而不是每个像素一个11位的值,XML平均每像素使用10个字节,这意味着每帧大约3.5 MB,然后,仅就磁盘I/O而言,您的代码就必须写出接近100 MB/s的数据(不包括整个XML转换)。在

我建议使用另一种格式来保存深度图像,也许像PGM这样简单的方法会是个主意。在

相关问题 更多 >