키넥트의 Depth Map 은 명암으로 제공됩니다.
실제 사진상 위치의 거리를 알고 싶어 예제를 수정해 봤습니다.
마우스를 클릭 한 곳의 거리를 메세지박스로 보여줍니다.
실행 화면:
중요 코드는 다음과 같습니다.
1. depthPixels 변수 아래 실제 거리가 들어갈 변수를 선언해 줍니다.
1
2
3
4
5 |
/// <summary>
/// Intermediate storage for frame data converted to color
/// </summary>
private byte[] depthPixels = null;
private int[] realDepthPixels = null; |
cs |
2. MainWindows() 에서 초기화 해 줍니다.
1
2
3
4
5
6
7
8
9 |
/// <summary>
/// Initializes a new instance of the MainWindow class.
/// </summary>
public MainWindow()
{
// allocate space to put the pixels being received and converted
this.depthPixels = new byte[this.depthFrameDescription.Width * this.depthFrameDescription.Height];
this.realDepthPixels = new int[this.depthFrameDescription.Width * this.depthFrameDescription.Height];
} |
cs |
3. ProcessDepthFrameData 함수에 다음과 같이 한 줄을 추가해 줍니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
private unsafe void ProcessDepthFrameData(IntPtr depthFrameData, uint depthFrameDataSize, ushort minDepth, ushort maxDepth)
{
// depth frame data is a 16 bit value
ushort* frameData = (ushort*)depthFrameData;
// convert depth to a visual representation
for (int i = 0; i < (int)(depthFrameDataSize / this.depthFrameDescription.BytesPerPixel); ++i)
{
// Get the depth for this pixel
ushort depth = frameData[i];
// To convert to a byte, we're mapping the depth value to the byte range.
// Values outside the reliable depth range are mapped to 0 (black).
this.depthPixels[i] = (byte)(depth >= minDepth && depth <= maxDepth ? (depth / MapDepthToByte) : 0);
// added line
this.realDepthPixels[i] = (int)(depth >= minDepth && depth <= maxDepth ? (depth) : 0);
}
} |
cs |
4. 클릭을 하면 거리를 보여주는 이벤트 함수를 추가합니다.
1
2
3
4
5
6
7
8 |
private void imgDepth_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Point point = e.GetPosition(imgDepth);
int distance = realDepthPixels[((int)point.Y) * this.depthBitmap.PixelWidth + ((int)point.X)];
MessageBox.Show(distance + "cm", "Distance of Cursor");
} |
cs |
ps. Depth Map 이 아닌 실제 이미지에서 Depth Map 의 거리값을 알고 싶을경우 다음과 같이 비율로 변환하여 값을 얻어시면 됩니다.
1
2
3
4
5
6
7
8
9
10
11 |
private void imgColor_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Point p = e.GetPosition(imgColor);
cvtPixelToDepthX = Convert.ToInt32((depthBitmap.PixelWidth * p.X) / this.displayWidth);
cvtPixelToDepthY = Convert.ToInt32((depthBitmap.PixelHeight * p.Y) / this.displayHeight);
int tmpFaceCenterDistance = realDepthPixels[cvtPixelToDepthY * this.depthBitmap.PixelWidth + cvtPixelToDepthX];
MessageBox.Show(tmpFaceCenterDistance + "cm");
} |
cs |
'DEVELOP > Image Processing' 카테고리의 다른 글
Intel RealSense Camera HandDetect Example (0) | 2017.03.07 |
---|---|
OpenCV 3.1 개발환경 셋팅 (CPP, MFC) (0) | 2016.08.30 |