lingdudehe 2019-11-18 15:17 采纳率: 0%
浏览 653

halcon导出的cs里面有图像处理之后得到的数据,怎么把这个数据用C#、Winform的表格形式显示出来?

disp_message(hv_ExpDefaultWinHandle, hv_Row2.TupleConcat(hv_Column2), "image", hv_Row2, hv_Column2, "black", "true");
hv_Row2等数据怎么在VS中用表格形式展现出来

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 14:36
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要将Halcon导出的CSV文件中的数据转换为表格格式并使用C#和Windows Forms进行显示,你需要遵循以下步骤:

    1. 导入必要的库:首先确保你已经安装了System.IO.PortsSystem.Windows.Forms。如果你正在使用.NET Core,请确保你已正确配置环境变量以访问这些库。
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace HalconDataDisplay
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 加载你的Halcon CSV文件
                string csvFilePath = @"D:\your\halcon\output.csv";
                using (StreamReader reader = new StreamReader(csvFilePath))
                {
                    List<string> headers = reader.ReadLine().Split(';').ToList();
                    Dictionary<string, double[]> dataDict = new Dictionary<string, double[]>();
    
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();
                        if (line == null) continue; // skip empty lines
    
                        var parts = line.Split(';');
                        if (parts.Length != headers.Count)
                        {
                            Console.WriteLine($"Error: Expected {headers.Count} columns but got {parts.Length}");
                            return;
                        }
    
                        string key = string.Join("", parts.Take(headers.Count - 1));
                        double[] values = parts.Skip(headers.Count).Select(part => Convert.ToDouble(part)).ToArray();
    
                        dataDict[key] = values;
                    }
    
                    // 创建一个新的窗口来展示数据
                    Form form = new Form()
                    {
                        Text = "Halcon Data Display",
                        Dock = DockStyle.Fill,
                        Size = new Size(600, 400),
                        Location = new Point(50, 50)
                    };
    
                    // 添加一个文本框来显示数据
                    DataGridView dataGridView = new DataGridView();
                    dataGridView.DataSource = dataDict;
                    dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    
                    form.Controls.Add(dataGridView);
    
                    // 设置窗体大小
                    form.ClientSize = new Size(form.Width, form.Height);
                    form.Show();
    
                    // 显示窗体
                    Application.Run(form);
                }
            }
        }
    }
    

    这个程序会打开一个新的窗口,并在那里显示从指定CSV文件读取的Halcon数据。它会自动检测并填充所有列标题,然后显示每个数据项。如果数据不匹配预期的格式或有缺失值,则会在控制台输出错误消息。

    注意:

    • 这个示例仅适用于简单的数据可视化。对于更复杂的数据结构(例如,可能包含日期或其他非数值类型的字段),你可能需要修改数据处理逻辑。
    • 还需根据实际应用调整窗体样式和其他细节。
    评论

报告相同问题?