Using the bodyIndex image, you can determin which pixel the person is in (the pixel whose value is not 255). Since the bodyIndex image is the coordinates of DepthSpace, it needs to be converted to the coordinates of ColorSpace to correspond the RGB image. We also get Depth image to use depth information in the conversion.
BodyIndex images can be acquired with a resolution of 512 x 424 as well as Depth images. Up to six people can be distinguished at the same time.
In NtKinect, the obtained BodyIndex image is represented by uchar or cv::Vector3b for each pixel.
type of return value型 | function name | descriptions |
---|---|---|
void | setBodyIndex(bool raw = true) | Set BodyIndex image to the member variable "bodyIndexImage" If the argument raw is "true" or there is no argument, the type of each pixel is uchar. If the argument raw is "false", the type of each pixel is cv::Vec3b. |
type | variable name | descriptions | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
cv::Mat | bodyIndexImage | BodyIndex image. Up to six people can be detected at the same time, and BodyIndex itself is a number from 0 to 5 assigned to each detected person. The type of each pixel of the BodyIndex image may be uchar or cv::Vec3b. The coordinates of the image are positions in the DepthSpace coordinate system.
In case of uchar data typeThe value of each pixel is 0 to 5 in the pixel where a person is detected, and 255 in the other pixels.
uchar pixel = bodyIndexImage.at<uchar>(y , x ) In case of cv::Vec3b data typeThe value of each pixel is the RGB value corresponding to the bodyIndex number.
cv::Mat pixel = bodyIndexImage.at<cv::Vec3b>(y , x )
|
Depth (distance) images can be acquired with a resolution of 512 x 424. The measurable distance range is from 500 mm to 8000 mm, but the range to recognize human beings is from 500 mm to 4500 mm.
In NtKinect, the obtained Depth image is represented by UINT16 (16 bit unsigned integer) for each pixel.
type of return value | function name | descriptions |
---|---|---|
void | setDepth(bool raw = true) |
Set the Depth image to the member variable "depthImage". When this function is called with no argument or "true" as first argument, the distance is set in mm for each pixel. When this function is called with "false", a value obtained by multiplying the distance by 65535/4500 is set for each pixel. That is, the image is mapped to the luminance of the black and white image of 0 (black) to 65535 (white) with the distance of 0 mm to 4500 mm. |
type | variable name | descriptions |
---|---|---|
cv::Mat | depthImage | Depth image.
The resolution is of 512 x 424 and each pixel is represented by UINT16. The coordinates of the image are the position in the DepthSpace coordinate system.
UINT16 depth = rgbImage.at<UINT16>(y , x ); |
Since the position and resolution of each sensor is different, the data is obtained as a value expressed in the coordinate system of each sensor. When using data obtained from different sensors at the same time, it is necessary to convert the coordinates to match.
Kinect V2 has 3 coordinate systems, ColorSpace, DepthSpace, and CameraSpace. There are 3 data types ColorSpacePoint, DepthSpacePoint, and CameraSpacePoint representing coordinates in each coordinate system.
Quoted from Kinect.h of Kinect for Windows SDK 2.0 |
---|
typedef struct _ColorSpacePoint { float X; float Y; } ColorSpacePoint; typedef struct _DepthSpacePoint { float X; float Y; } DepthSpacePoint; typedef struct _CameraSpacePoint { float X; float Y; float Z; } CameraSpacePoint; |
For the RGB image, Depth image, and skeleton information, the coordinate system is different. The coordinate system of the RGB image is ColorSpace, that of the Depth image is DepthSpace, and that of the skeleton information is CameraSpace.
Coordinate system | type of coordinates | Captured Data |
---|---|---|
ColorSpace | ColorSpacePoint | RGB image |
DepthSpace | DepthSpacePoint | depth image, bodyIndex image, infrared image |
CameraSpace | CameraSpacePoint | skeleton information |
CameraSpace coordinate system representing skeleton position |
---|
The CameraSpace is a 3-dimensional coordinate system with the following features.
(2016/11/12 figure changed, and description added). |
"Coordinate system conversion function" held by ICoordinateMapper class of Kinect V2 is as follows.
type of return value | function name | descriptions |
---|---|---|
HRESULT | MapCameraPointToColorSpace( CameraSpacePoint sp , ColorSpacePoint *cp ) |
Convert the coordinates sp in the CameraSpace to the coordinates cp in the ColorSpace. Return value is S_OK or error code. |
HRESULT | MapCameraPointToDepthSpace( CameraSpacePoint sp , DelpthSpacePoint *dp ) |
Convert the coordinates sp in the CameraSpace to the coordinates dp in DepthSpace. Return value is S_OK or error code. |
HRESULT | MapDepthPointToColorSpace( DepthSpacePoint dp , UINT16 depth , ColorSpacePoint *cp ) |
Convert the coordinates dp in DepthSpace and distance depth to the coordinates cp in ColorSpace. Return value is S_OK or error code. |
HRESULT | MapDepthPointToCameraSpace( DepthSpacePoint dp , UINT16 depth , CameraSpacePoint *sp ) |
Convert the coordinates dp in DepthSpace and distance depth to the coordinates sp in CameraSpace. Return value is S_OK or error code. |
An instance of ICoordinateMapper class used for mapping coordinate systems in Kinect V2 is held in NtKinect's member variable "coordinateMapper".
type | variable name | descriptions |
---|---|---|
CComPtr<ICoordinateMapper> | coordinateMapper | An instance of ICoordinateMapper used for mapping coordinate systems. |
In the following explanation, it is assumed that the folder is renamed to "KinectV2_bodyIndex2" after expanding the above zip file.
There is a folder called "KinectV2" in the project, and there are "main.cpp" and "Kinect.h" there.
KinectV2_bodyIndex2\KinectV2\main.cpp KinectV2_bodyIndex2\KinectV2\NtKinect.hPlease copy cat.jpg to that folder. this image file is downloaded from the free material site. I do not care about any image, but I think that it is easier to handle the same size as the RGB camera image.
KinectV2_bodyIndex2\KinectV2\cat.jpg
main.cpp |
|
First, we load the background image into the variable "cat".
Repeat the following process until 'q' is entered from the keyboard.
Get an RGB image from the camera. since the format of the RGB image is "BGRA" format, convert it to the "BGR" format according to the format of the jpeg image read into the variable "cat". Then, hold it in the variable "fgImg".
We holds the synthesized image in the variable "bgImg". Copy the contents of the variable "cat" as the background image.
Get the Depth image and the bodyIndexImage. When we find a pixel in which a human being appears in the bodyIndex image, we calculate the position of the corresponding ColorSpace, and paste the 4 x 4 area around the ColorSpace position from the RGB image "fgImg" on the composite image "bgImg". Since the coordinates of bodyIndex image are values in DepthSpace, they are converted to the values of ColorSpace using the depth information (the value of the Depth image). Since the resolution is different between the bodyIndex image (512 x 424) and the RGB image (1920 x 1080), a range of 4 x 4 RGB image is pasted per 1 pixel of bodyIndex image.
The copyRect(cv::Mat& src , cv::Mat& dst , int sx , int sy , int w , int h , int dx , int dy ) function defined in the program paste the partial image on src image to dst image. In the region on src, the upper left is (sx , sy ) and width and height are w and h respectively. In resion on dst/i>, the upper left is (dx , dy ). The size of the rectangular area is adjusted so that it does not extend beyond the range of image.
The programming code
copyRect(fgImg,bgImg,cx-2,cy-2,4,4,cx-2,cy-2); |
for (int y=cy-2; y < cy+2; y++) { for (int x=cx-2; x < cx+2; x++) { if (x, y is contained in both image) { bgImg.at<cv::Vec3b>(y,x) = fgImg.at<cv::Vec3b>(y,x); // copy BGR pixel at (line:y, col:x) } } } |
In OpenCV, the element in the cv::Mat is accessed by the "at<TYPE>(int line, int col)" member function. For the "group of 4 byte type data (ie. BGRA format pixels)", the TYPE is cv::Vec4b, and For the "group of 3 byte type data (ie. BGR format pixels)", the TYPE is cv::Vec3b.
Human images are extracted and displayed on the background image.
Since the above zip file may not include the latest "NtKinect.h", Download the latest version from here and replace old one with it.