ofxKinectForWindows2 depth threshold for blob tracking

Hi,

I’d like to do some blob detection with the kinect v2 (utilising the near and far thresholds like in the ofxKinect example that comes with OF). I have managed to get ofxKinectForWindows2 working and (like it says in the example app) the depth output is darker than the ofxKinext example so I am a little confused about how I get the thresholds working.

Has anyone done this with ofxKinectForWindows2? @elliotwoods?

Thanks!

Hi csw

I recently did this for a project. I just used a shader to boost the visibility of the depthmap and clip the front, back, floor, ceiling, left and right. Worked perfectly for my purposes. Here’s the C++:

    depthFbo.begin();
	depthShader.begin();
	depthShader.setUniform1i("isClipping", (isClipping) ? 1 : 0);
	depthShader.setUniform2f("resolution", 512, 424);
	depthShader.setUniform1f("nearClip", liveNearClip);
	depthShader.setUniform1f("farClip", liveFarClip);
	depthShader.setUniform1f("nearFloorClip", liveNearFloorClip);
	depthShader.setUniform1f("farFloorClip", liveFarFloorClip);
	depthShader.setUniform1f("nearCeilingClip", liveNearCeilingClip);
	depthShader.setUniform1f("farCeilingClip", liveFarCeilingClip);
	depthShader.setUniform1f("nearLeftClip", liveNearLeftClip);
	depthShader.setUniform1f("farLeftClip", liveFarLeftClip);
	depthShader.setUniform1f("nearRightClip", liveNearRightClip);
	depthShader.setUniform1f("farRightClip", liveFarRightClip);
	depth.draw(0, 0, 512, 424);
	depthShader.end();
	depthFbo.end();

And the shaders:

Vert:

void main(void) 
{
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = ftransform();
}

Frag:

    uniform int isClipping;
uniform vec2 resolution;
uniform sampler2DRect tex0;
uniform float farClip;
uniform float nearClip;
uniform float nearFloorClip;
uniform float farFloorClip;
uniform float nearCeilingClip;
uniform float farCeilingClip;
uniform float nearLeftClip;
uniform float farLeftClip;
uniform float nearRightClip;
uniform float farRightClip;

float map(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool clamp)
{
	if (abs(inputMin - inputMax) < 0.000000001){
		return outputMin;
	} else {
		float outVal = ((value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin);
        
		if(clamp){
			if(outputMax < outputMin){
				if( outVal < outputMax )outVal = outputMax;
				else if( outVal > outputMin )outVal = outputMin;
			}else{
				if( outVal > outputMax )outVal = outputMax;
				else if( outVal < outputMin )outVal = outputMin;
			}
		}
		return outVal;
	}
}

void main(void) 
{
	vec4 baseColor = texture2DRect( tex0, gl_TexCoord[0].st );
	
	vec4 col = baseColor;
	col.rgb *= 10.0;
	
	vec2 normPos = gl_TexCoord[0].st / resolution;
	
	float grey = (col.r + col.g + col.b) / 3.0;
	float z = grey;
	
	if (isClipping == 1)
	{
		if (z > farClip) // clip back
			z = 0.0;
		else if (z < nearClip) // clip front
			z = 0.0;
		else
		{	
			// clip floor
			float floor = map(grey, nearClip, farClip, nearFloorClip, farFloorClip, true);
			if (normPos.y > floor)
				z = 0.0;
			
			// clip ceiling
			float ceiling = map(grey, nearClip, farClip, nearCeilingClip, farCeilingClip, true);
			if (normPos.y < ceiling)
				z = 0.0;
				
			// clip left
			float left = map(grey, nearClip, farClip, nearLeftClip, farLeftClip, true);
			if (normPos.x < left)
				z = 0.0;
				
			// clip right
			float right = map(grey, nearClip, farClip, nearRightClip, farRightClip, true);
			if (normPos.x > right)
				z = 0.0;
		}
	}		
	
	col.rgb = vec3(z, z, z);
	
	gl_FragColor = col;
}

Thanks James,

I ended up ripping the logic our from the ofKinect addon in the end which seems to do the job too. I’ll look into your method a bit more if I have time.

Interestingly, the size of the depth image I am getting back is 512 x 424 which had me stuck for a while (thinking it would be 1920x1080).

No problem. Yeah I was a little confused by the smaller depth map. I haven’t investigated properly look yet but I’m hoping there’s another way to get more depth info somewhere.