About conversion of "unsigned short *" "unsigned char" in KinectV 2 program

When executing the following program with the KinectV 2 program, an exception handling error will appear.

	unsigned short* rawdepth = kinect.getDepthSource()->getPixels().getData();

	int rawindex = 0;
	int displayindex = 0;


	for (int y = 0; y < DEPTH_HEIGHT; y++) {
		for (int x = 0; x < DEPTH_WIDTH; x++) {
			
			unsigned char greyValue = depthLookupTable[rawdepth[rawindex++]];

			depthDisplay.getPixels()[displayindex++] = greyValue;
			depthDisplay.getPixels()[displayindex++] = greyValue;
			depthDisplay.getPixels()[displayindex++] = greyValue;

There was an error on this line

unsigned char greyValue = depthLookupTable [rawdepth [rawindex ++]];

%E3%82%AD%E3%83%A3%E3%83%97%E3%83%81%E3%83%A3

How should we write this?

Hi, it is not really about the conversion between unsigned short and unsigned char but about trying to access an array (or vector) at an index that is larger than the allowed one. Which means that rawindex is larger than rawdepth's size.
So, I’d rather do a few things.
I would guess that DEPTH_HEIGHT and DEPTH_WIDTH are some hardcoded #define.
Instead of this request the width and height to the depthsource object. It should look something like the following:

unsigned short* rawdepth = kinect.getDepthSource()->getPixels().getData();

	int w = kinect.getDepthSource()->getPixels().getWidth();
	int h = kinect.getDepthSource()->getPixels().getHeight();
	int rawindex = 0;
	int displayindex = 0;

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			
			unsigned char greyValue = depthLookupTable[rawdepth[rawindex++]];

			depthDisplay.getPixels()[displayindex++] = greyValue;
			depthDisplay.getPixels()[displayindex++] = greyValue;
			depthDisplay.getPixels()[displayindex++] = greyValue;
		}
	}

if you want to double check you would check rawindex each time before accessing the rawdepth array.

So, instead of

unsigned char greyValue = depthLookupTable[rawdepth[rawindex++]];

put

if(rawindex < kinect.getDepthSource()->getPixels().size()){
unsigned char greyValue = depthLookupTable[rawdepth[rawindex++]];
}

btw, from your post title, notice that unsigned short* is a pointer which in this case points to the first element of an array while unsigned char is just a single instance of it. you shouldn’t be trying to convert from one type to the other unless you really understand what’s going on.
Hope this helps.

Thank you! !
The program was compiled and started!
However, it will freeze with the following error, but is there something in mind?
asca

By the way, it is this project that is going to move.

Help me.
I am using Windows 10, oF 0.9.8.

And another question, is ofxKinectWindows 2 can not get the distance of the specified coordinate depth?

Hi,
in the generateDepthDisplayImage() method before the for loops you should check if the depthDisplay image has the same size as the rawdepth. This is what is probably giving you problems.
It would look something like the following.

unsigned short* rawdepth = kinect.getDepthSource()->getPixels().getData();

	int w = kinect.getDepthSource()->getPixels().getWidth();
	int h = kinect.getDepthSource()->getPixels().getHeight();
	int rawindex = 0;
	int displayindex = 0;
	if(depthDisplay.getWidth() != w || depthDisplay.getHeight() != h){
		depthDisplay.allocate(w, h, OF_IMAGE_COLOR);

	}

	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			
			unsigned char greyValue = depthLookupTable[rawdepth[rawindex++]];

			depthDisplay.getPixels()[displayindex++] = greyValue;
			depthDisplay.getPixels()[displayindex++] = greyValue;
			depthDisplay.getPixels()[displayindex++] = greyValue;
		}
	}