Averaging depth pixels && simply printing millimetre depth to console (C++)

Unanswered Averaging depth pixels && simply printing millimetre depth to console (C++)

  • Monday, June 11, 2012 8:55 AM
     
      Has Code

    Hi guys,

    I am trying to do two things here:

    1. Do a moving average of the last 10 frames (perhaps a bit too many, given how much processing power it consumes)
    2. Simply print out the integer value of a single pixel (because #1 isn't working! All I get is a beige colour.)

    Here is my code, which doesn't work:

    QByteArray QDepthAverager::getAverageDepthImage()
    {
    	//For each pixel
    	for (int pixel = 0; pixel < 614400; pixel=pixel+2)
    		{
    			pAverageImage[pixel] = 0;
    			//And for each image
    			for (int i = 0; i < pImageHistory.size(); i++)
    			{
    				//Get the average for that pixel.
    				pAverageImage[pixel] = pAverageImage[pixel] + pImageHistory.at(i)[pixel];
    
    			}
    			pAverageImage[pixel] = pAverageImage[pixel] / pHistoryLength;
    		}
    
    	/*const char *data = pImageHistory.at(0).data();
    	while (*data) {
    		std::cout << "[" << *data << "]\n";
    		++data;
    	}*/
    	std::cout << (pImageHistory.at(0).at(1000)>>3) << "\n";
    	//std::cout << "Hello!\n";
    
    	
    
    	return pAverageImage;
    
    }

    My first question is, how do I just get each pixel to be the integer, without the player cruft? I am using "NUI_INITIALIZE_FLAG_USES_DEPTH", and I am using a commercial K4W sensor, so I should be able to get 640x480 pixel in the following format:

    "If your application included NUI_INITIALIZE_FLAG_USES_DEPTH in the dwFlags argument to NuiInitialize, depth data is returned as a 16-bit value in which the low-order 12 bits (bits 0–11) contain the depth value in millimeters."

    I think I am being a bit naieve here, and that some more thinking is involved. Clearly, the numbers I am getting out on the console (which sometimes go into the negatives) indicate that I need to do some bit shifting to turn it into a number which is usable. Perhaps shifting it in one direction to remove the player data, then shifting it back to a workable position would be fine.

    I wish I had an easy way to see the binary numbers which it is trying to print, so I could figure out where I'm going wrong.


    Clever, witty forum signature.

All Replies

  • Tuesday, June 12, 2012 9:45 PM
    Owner
     
     

    Regardless of using, NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX or NUI_INITIALIZE_FLAG_USES_DEPTH, to get the depth value you have to bit shift by 3.

  • Wednesday, June 13, 2012 3:09 PM
     
      Has Code

    This code:

    std::cout << (((unsigned short int)depthFrame.at(30000))>>3) << "\n";

    Which is being called every time I receive a frame is outputting the following types of results when I rotate the kinect on a tripod:

    http://ideone.com/9oa5k

    Not really sure why it jumps down suddenly to near zero, then back up to the 8000s. I definitely do not have 8m between my wall and the camera.


    Clever, witty forum signature.

  • Wednesday, June 13, 2012 6:33 PM
     
      Has Code

    I'm not precisely sure why you're seeing the values you're seeing (it looks like this might be after the averaging or similar?), but do note that there are sentinel values:

    #define NUI_IMAGE_DEPTH_NO_VALUE              0
    #define NUI_IMAGE_DEPTH_TOO_FAR_VALUE         (0x0fff << NUI_IMAGE_PLAYER_INDEX_SHIFT)
    #define NUI_DEPTH_DEPTH_UNKNOWN_VALUE         (0x1fff << NUI_IMAGE_PLAYER_INDEX_SHIFT)
    

    In particular, Unknown >> 3 is 8191, perhaps that's what you're hitting?  If you don't wish to see these sentinel values, you should be able to just not pass

    NUI_IMAGE_STREAM_FLAG_DISTINCT_OVERFLOW_DEPTH_VALUES

    ... which I'm assuming you are passing today?



    -Adam Smith [MSFT]

  • Thursday, June 14, 2012 11:14 AM
     
     
    Funnily enough, I haven't passed that flag anywhere.

    Clever, witty forum signature.

  • Thursday, June 14, 2012 1:51 PM
     
     

    So now I am just doing:

    std::cout << (int)((unsigned short int)castedBuffer[307000]>>3) << "\n";

    But the numbers it is giving me are in the range of 0-30. I think I have something wrong somewhere, I think that I am only getting half of the 16 bits promised?



    Clever, witty forum signature.