joshuajnoble / ofxKinectV2

of addon for Kinect V2
47 stars 22 forks source link

speedup frame mappings #7

Closed Kj1 closed 9 years ago

Kj1 commented 9 years ago

you can speed up the frame mapping functions by using KCBMapDepthFrameToColorSpace
instead of KCBMapDepthPointsToColorSpace

on my test system, i've noticed a sixfold increase in speed (from 35-40 ms to 5-6 ms).

I'm not forking this because lack of time, but i'm using the following code:

void ofxKinectCommonBridge::mapDepthFrameToColorFrame(ofPixels& dstColorPixels) {
int depthArraySize = depthFrameDescription.width * depthFrameDescription.height;
vector<UINT16> depths; //input
depths.resize(depthArraySize);
const ofShortPixelsRef depthImage = getRawDepthPixelsRef();
for(int y = 0; y < depthFrameDescription.height; y++){
    for(int x = 0; x < depthFrameDescription.width; x++) {
        int i = y*depthFrameDescription.width+x;
        depths[i] = (UINT16)depthImage.getPixels()[i];
    }
}

//output
vector<ColorSpacePoint> colorPoints;
colorPoints.resize(depthArraySize);

HRESULT mapResult;
mapResult = KCBMapDepthFrameToColorSpace(
    hKinect,
    depthArraySize, &depths[0],
    depthArraySize, &colorPoints[0]);

if(!dstColorPixels.isAllocated() || 
    dstColorPixels.getWidth() != depthFrameDescription.width || dstColorPixels.getWidth() != depthFrameDescription.height)
{
    dstColorPixels.allocate(depthFrameDescription.width,depthFrameDescription.height, OF_IMAGE_COLOR);
}

memset(dstColorPixels.getPixels(), 0, dstColorPixels.getWidth()*dstColorPixels.getHeight()*dstColorPixels.getBytesPerPixel());

for(int y = 0; y < depthFrameDescription.height; y++){
    for(int x = 0; x < depthFrameDescription.width; x++) {      
        int depthFrameIndex = y * depthFrameDescription.width + x;
        ColorSpacePoint& p = colorPoints[depthFrameIndex]; 
        if(p.X >= 0 && p.X < colorFrameDescription.width && p.Y >= 0 && p.Y < colorFrameDescription.height)
        {
            dstColorPixels.setColor(x,y, videoPixels.getColor(p.X,p.Y));
        }
    }
}
}
joshuajnoble commented 9 years ago

added. thank you very much.