qq_27468993 2015-09-28 11:36 采纳率: 50%
浏览 1545
已结题

新人拼凑的码,求解为什么D\\点云数据.txt中没有写入数据!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Kinect;
using System.IO;
using System.Windows.Forms;
namespace GetPointCloudFromKinectV2
{
class Program
{

    //************************ 初始化相关对象 *********************************
    public static KinectSensor sensor0 = null;

    static DepthFrameSource depthFrameSource0 = null;

    static DepthFrameReader depthFrameReader0 = null;

    static DepthFrame depthFrame = null;
    static ushort[] frameData_Shoot = new ushort[217088];

    static CoordinateMapper coordinateMapper0 = null;

    static int depthFrameWidth = 512;
    static int depthFrameHeight = 424;
    static CameraSpacePoint[] OriginalPointCloud0 = new CameraSpacePoint[depthFrameWidth * depthFrameHeight];
    static void Main(string[] args)
    {
        sensor0 = KinectSensor.GetDefault();
        sensor0.Open(); //打开传感器


        depthFrameSource0 = sensor0.DepthFrameSource;//初始化depthFrameSource0
        coordinateMapper0 = sensor0.CoordinateMapper;//初始化coordinateMapper0

        depthFrameReader0 = sensor0.DepthFrameSource.OpenReader();//打开深度数据流

        DepthFrame depthFrame = null;
        depthFrame = depthFrameReader0.AcquireLatestFrame();
        if (depthFrame != null)
        {
            depthFrame.CopyFrameDataToArray(frameData_Shoot);

            coordinateMapper0.MapDepthFrameToCameraSpace(frameData_Shoot, OriginalPointCloud0);



        }
    }

    /// 将摄像机坐标系下的点云写入到文件

    static void WritePointCloud(CameraSpacePoint[] cameraSpacePoints, string FilePath = "D\\点云数据.txt")
    {
        //确保路径存在
        //如果不存在,则创建文件夹
        if (!Directory.Exists(FilePath))
        {
            Directory.CreateDirectory(FilePath);
        }

        FilePath = Path.Combine(FilePath );

        try
        {
            float x, y, z;
            int frameWidth = depthFrameWidth;//深度帧的宽度
            int frameHeight = depthFrameHeight;//深度帧的高度
            for (int row = 0; row < frameHeight; row++)
            {
                for (int col = 0; col < frameWidth; col++)
                {
                    x = cameraSpacePoints[row * frameWidth + col].X;
                    y = cameraSpacePoints[row * frameWidth + col].Y;
                    z = cameraSpacePoints[row * frameWidth + col].Z;
                    string path="D\\点云数据.txt";//
                    StreamWriter sw = new StreamWriter(path, true);
                    sw.WriteLine("{0} {1} {2}", x, y, z);
                    sw.Close();
                }
            }    
        }
        catch (IOException ex)
        {
            System.Windows.Forms.MessageBox.Show("An IO exception has been thrown!\n{0}",
                ex.ToString());
            return;
        }
    }
}

}
新人拼凑的码,求解为什么D\点云数据.txt中没有写入数据!!

  • 写回答

5条回答 默认 最新

  • Go 旅城通票 2015-09-28 12:08
    关注

    路径搞错了吧。。你少了冒号会在你exe所在目录写入文件,就是bin目录里面,而不是d盘根目录下

            string path = @"D:\点云数据.txt";//放外面来,不需要每次打开文件写入
           StreamWriter sw = new StreamWriter(path, true);
            for (int row = 0; row < frameHeight; row++)
            {
                for (int col = 0; col < frameWidth; col++)
                {
                    x = cameraSpacePoints[row * frameWidth + col].X;
                    y = cameraSpacePoints[row * frameWidth + col].Y;
                    z = cameraSpacePoints[row * frameWidth + col].Z;
                    sw.WriteLine("{0} {1} {2}", x, y, z);
                }
            }
            sw.Close();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊