none
GetPlayerSilhouette is repeating the silhouette RRS feed

  • Question

  • I'm trying to use the GetPlayerSilhouette method from the BasicInteractions - WPF sample but its duplicating the silhouette 4 times for some reason. So it looks like this:

    I'm in VB but here is the converted code from the sample that I'm using:

    Private Sub GetPlayerSilhouette(depthFrame As DepthImageFrame, playerIndex As Integer)
            If depthFrame IsNot Nothing Then
                Dim haveNewFormat As Boolean = Me.lastImageFormat <> depthFrame.Format
    
                If haveNewFormat Then
                    Me.pixelData = New Short(depthFrame.PixelDataLength - 1) {}
                    Me.depthFrame32 = New Byte(depthFrame.Width * depthFrame.Height * Bgra32BytesPerPixel - 1) {}
                    Me.convertedDepthBits = New Byte(Me.depthFrame32.Length - 1) {}
                End If
    
                depthFrame.CopyPixelDataTo(Me.pixelData)
    
                Dim i16 As Integer = 0, i32 As Integer = 0
                While i16 < pixelData.Length AndAlso i32 < Me.depthFrame32.Length
                    Dim player As Integer = pixelData(i16) And DepthImageFrame.PlayerIndexBitmask
                    If player = playerIndex Then
                        convertedDepthBits(i32 + RedIndex) = 0
                        convertedDepthBits(i32 + GreenIndex) = 0
                        convertedDepthBits(i32 + BlueIndex) = 0
                        convertedDepthBits(i32 + 3) = &H66
                    ElseIf player > 0 Then
                        convertedDepthBits(i32 + RedIndex) = 0
                        convertedDepthBits(i32 + GreenIndex) = 100
                        convertedDepthBits(i32 + BlueIndex) = 0
                        convertedDepthBits(i32 + 3) = &H66
                    Else 
                        convertedDepthBits(i32 + RedIndex) = 0
                        convertedDepthBits(i32 + GreenIndex) = 0
                        convertedDepthBits(i32 + BlueIndex) = 0
                        convertedDepthBits(i32 + 3) = &H66
                    End If
                    i16 += 1
                    i32 += 4
                End While
    
                If Me.silhouette Is Nothing OrElse haveNewFormat Then
                    Me.silhouette = New WriteableBitmap(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgra32, Nothing)
                End If
    
                Me.silhouette.WritePixels(New Int32Rect(0, 0, depthFrame.Width, depthFrame.Height), convertedDepthBits, depthFrame.Width * Bgra32BytesPerPixel, 0)
                Me.lastImageFormat = depthFrame.Format
                ImageSilhouette.Source = Me.silhouette
            End If
        End Sub

    Then here is my xaml just in case it helps

     <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="80"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="40"></RowDefinition>
            </Grid.RowDefinitions>
            <Viewbox Grid.Row="1" Stretch="Uniform">
                <Grid>
                    <Image Name="ImageSilhouette" Width="640" Height="480" Stretch="UniformToFill"/>
                    <Image Name="Image" Width="640" Height="480" Opacity=".7"/>
                </Grid>
            </Viewbox>
        </Grid>
    My goal is to only have 1 of the green silhouettes and be able to overlay the skeleton points on top of the single silhouette.

    Any help would be greatly appreciated, thank you.




    • Edited by heyheyyeah Thursday, December 27, 2012 6:03 PM
    Thursday, December 27, 2012 4:16 PM

Answers

  • Found the issue 

      Private Bgra32BytesPerPixel As Integer = (PixelFormats.Bgra32.BitsPerPixel + 7) / 8

    i added Bgra32BytesPerPixel -=1 in VB in the load event and didn't do that subtraction of 1 in the GetPlayerSilhouette code and now its works. So it seems like VB was rounding up to 5 while C# was rounding down to 4 which is what it should be.

    • Marked as answer by heyheyyeah Thursday, December 27, 2012 9:58 PM
    Thursday, December 27, 2012 9:58 PM

All replies

  • Now I created a C# project and convert what I have in VB back to C# and it works fine. Here is what my c# project looks like but I still need to get the VB equivalent to work the same way. I'll post the C# code I have because maybe there is something that isn't 100% correct when I converted the c# to VB. Thanks

    private void GetPlayerSilhouette(DepthImageFrame depthFrame, int playerIndex)
            {
                if (depthFrame != null)
                {
                    bool haveNewFormat = this.lastImageFormat != depthFrame.Format;
    
                    if (haveNewFormat)
                    {
                        this.pixelData = new short[depthFrame.PixelDataLength];
                        this.depthFrame32 = new byte[depthFrame.Width * depthFrame.Height * Bgra32BytesPerPixel];
                        this.convertedDepthBits = new byte[this.depthFrame32.Length];
                    }
    
                    depthFrame.CopyPixelDataTo(this.pixelData);
    
                    int i16 = 0;
                    int i32 = 0;
                    while (i16 < pixelData.Length && i32 < this.depthFrame32.Length)
                    {
                        int player = pixelData[i16] & DepthImageFrame.PlayerIndexBitmask;
                        if (player == playerIndex)
                        {
                            convertedDepthBits[i32 + RedIndex] = 0;
                            convertedDepthBits[i32 + GreenIndex] = 0;
                            convertedDepthBits[i32 + BlueIndex] = 0;
                            convertedDepthBits[i32 + 3] = 0;
                        }
                        else if (player > 0)
                        {
                            convertedDepthBits[i32 + RedIndex] = 0;
                            convertedDepthBits[i32 + GreenIndex] = 100;
                            convertedDepthBits[i32 + BlueIndex] = 0;
                            convertedDepthBits[i32 + 3] = 150;
                            //else the background color
                        }
                        else
                        {
                            convertedDepthBits[i32 + RedIndex] = 0;
                            convertedDepthBits[i32 + GreenIndex] = 0;
                            convertedDepthBits[i32 + BlueIndex] = 0;
                            convertedDepthBits[i32 + 3] = 0;
                        }
                        i16 += 1;
                        i32 += 4;
                    }
    
                    if (this.silhouette == null || haveNewFormat)
                    {
                        this.silhouette = new WriteableBitmap(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgra32, null);
                    }
    
                    this.silhouette.WritePixels(new Int32Rect(0, 0, depthFrame.Width, depthFrame.Height), convertedDepthBits, depthFrame.Width * Bgra32BytesPerPixel, 0);
                    this.lastImageFormat = depthFrame.Format;
                    ImageSilhouette.Source = this.silhouette;
                }
            }

    so just to clearify, the c# code works how I want it to. The VB code in the first post is still where I am having the issues. But as far as I can tell, the VB and the C# code should be pretty much the same so I'm not sure why I'm getting the different results as you can see in the screenshots I posted. Thanks

    • Edited by heyheyyeah Thursday, December 27, 2012 9:37 PM
    Thursday, December 27, 2012 9:35 PM
  • Found the issue 

      Private Bgra32BytesPerPixel As Integer = (PixelFormats.Bgra32.BitsPerPixel + 7) / 8

    i added Bgra32BytesPerPixel -=1 in VB in the load event and didn't do that subtraction of 1 in the GetPlayerSilhouette code and now its works. So it seems like VB was rounding up to 5 while C# was rounding down to 4 which is what it should be.

    • Marked as answer by heyheyyeah Thursday, December 27, 2012 9:58 PM
    Thursday, December 27, 2012 9:58 PM