Kinect虚拟试衣间开发(4)-2D衣物试穿

衣物试穿是虚拟试衣间关键部分,可以分为两部分实现,一个是把现实中的人体坐标转换为屏幕坐标,二是根据骨骼宽和高调整衣物大小,并把前面获取的人体屏幕坐标赋给衣物位置坐标

  • 显示衣物的image控件
 <Image x:Name="clth" Source="FDress/d8.png" HorizontalAlignment="Left"  Visibility="Hidden" Width="Auto" Height="Auto" Margin="396,279,0,0" VerticalAlignment="Top" Grid.Column="1" Grid.RowSpan="2" />
  • 转换到屏幕坐标
        /// 
        /// 转换骨骼坐标到彩色坐标,再到屏幕坐标
        /// 
        /// 关节点
        /// 返回屏幕点
        public Point getJointScreenPoint(Joint joint)
        {
            //将body point转换为 color point
            ColorSpacePoint colorPoint = this.kinectSensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position);
            //将color point 变为 屏幕坐标点,数字是包含彩图image的Grid的长宽
            double X =(colorPoint.X / colorFrameDescription.Width) * 1357;
            double Y =( colorPoint.Y / colorFrameDescription.Height) * 735;

            Point point = new Point(X, Y);
            return point;
        }
  • 调整图片大小
        /// 
        /// 调整图片大小
        /// 
        /// 身体
        /// 
        public void changeSize(Body body)
        {
            double shoulderwidth = Math.Abs(getJointScreenPoint(body.Joints[JointType.ShoulderLeft]).X - getJointScreenPoint(body.Joints[JointType.ShoulderRight]).X);

            double width = 3 * shoulderwidth ;
            double height = 3 * shoulderwidth ;
            //贴图左肩与图的padding
            double x = getJointScreenPoint(body.Joints[JointType.ShoulderLeft]).X * shoulderwidth / 200;
            double y = getJointScreenPoint(body.Joints[JointType.ShoulderLeft]).Y * shoulderwidth / 200;

            double x1 = getJointScreenPoint(body.Joints[JointType.ShoulderLeft]).X-0.3*width;
            double y1 = getJointScreenPoint(body.Joints[JointType.ShoulderLeft]).Y-0.17*height;
            try
            {
                clth.Width = width;
                clth.Height = height;
                clth.Margin = new Thickness(x1, y1, 0, 0);
            }catch(ArgumentException e)
            {
            }

        }
  • 调用changeSize函数
    在Reader_multiSourceFrameArrived中
 using (BodyFrame bodyFrame = multiSourceFrame.BodyFrameReference.AcquireFrame())
            {
                if (bodyFrame != null)
                {
                    if (this.bodies == null) bodies = new Body[bodyFrame.BodyCount];
                    //return bodies to a list
                    bodyFrame.GetAndRefreshBodyData(this.bodies);
                    Body thebody = bodies[0];
                    foreach (Body body in this.bodies)
                    {
                    //多个人在屏幕前时,选择最近的那一个
                        if (body.IsTracked)
                        {
                            if (thebody.Joints[JointType.SpineBase].Position.Z == 0)    thebody = body;
                            else if (thebody.Joints[JointType.SpineBase].Position.Z > body.Joints[JointType.SpineBase].Position.Z) thebody = body;

                            changeSize(thebody);
                        }
                    }
                }
            }

        }

你可能感兴趣的:(kinect开发)