using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Intel.RealSense;
using Emgu.CV.Structure;
using Emgu.CV;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using Stream = Intel.RealSense.Stream;
using System.Threading;
using WindowsFormsApp1; // 根据你的项目命名空间进行修改
using ABB.Robotics.Controllers.IOSystemDomain;
using yolov5_onnx.Mod;
using static WindowsFormsApp1.ExtrinsicsExtensions;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using NetTopologySuite.GeometriesGraph;
using System.Windows.Media.Media3D;
namespace RealSence_PathMove
{
public class IntelRealSense
{
private readonly Form1 form;
public IntelRealSense(Form1 form)
{
this.form = form;
}
///
/// 相機連結狀態
///
enum CameraState
{
Disconnected = 0,
Connected = 1,
Opened = 2
}
Colorizer colorizer = new Colorizer();
Align align = new Align(Intel.RealSense.Stream.Color); // 深度影像MAP彩色影像
CameraState CamState = CameraState.Disconnected; // 初始狀態設為斷線
public AdvancedDevice DeviceName;
private Pipeline pipeline = new Pipeline();
internal PictureBox pictureBox2;
internal PictureBox pictureBox3;
internal PictureBox pictureBox4;
internal PictureBox pictureBox5;
private CustomProcessingBlock block;
internal CancellationTokenSource cts;
private System.Threading.Tasks.Task dataProcessingTask = System.Threading.Tasks.Task.CompletedTask; // 初始化为已完成的任务。
VideoStreamProfile depthStream;
VideoStreamProfile colorStream;
public Intrinsics depthIntrinsics; // 定義成員變數,以便在不同方法中訪問
public Extrinsics depthExtrinsics;
public Intrinsics colorIntrinsics;
public Extrinsics colorExtrinsics;
public Bitmap bmpColor; // 請確保這裡有定義 bmpColor 成員
public Bitmap bmpDepth;
public List yoloFeaturePoints = new List();
// 假设您已经获得了特徵點在相機座標系中的坐标列表 cameraPointsList
List> cameraPointsList = new List>();
public int objectCounter = 1; // 初始化物體編號為1
StringBuilder coordTextBuilder = new StringBuilder();
private readonly object frameLock = new object();
public int deepestX, deepestY, shallowestX, shallowestY, Goldsamplepointcloudnumber;
public double X, Y, u, v, percentageInsideCrop;
public float Z, x, y, z, Rx, Ry, Rz, threshold, deepestZ, shallowestZ, GolddeepestZ, GoldshallowestZ;
public string folderPath, fileName, folderPath1, fileName1;
public ListViewItem Item { get; set; }
public bool camclose;
public Extrinsics SharedExtrinsics { get; private set; }
public ushort[] DepthData { get; private set; }
public Bitmap yoloimage { get; private set; }
public float MaxDepthValue { get; private set; }
public System.Numerics.Vector3 cameraPoint { get; private set; }
public float scale { get; private set; }
public Image ColorImg { get; private set; }
public Image DepthImg { get; private set; }
public Image ROIImg;
public Bitmap bitmap { get; private set; }
public ushort[] croppedDepthData;
// 在類別中添加一個 List 來儲存三維座標
public List storedCoordinates = new List();
public Bitmap DepthBitmap { get; private set; }
public Rectangle CroppingRect { get; private set; }
public byte[] ColorData { get; private set; }
public Bitmap DepthImg1Bitmap { get; private set; }
public bool frameReceived = false;
public bool captureThread1bool = true;
public bool TakeImage;
public int i = 0;
public Thread uiThread; // UI 线程
public CancellationTokenSource tokenSource = new CancellationTokenSource();
public delegate void NewFrameEventHandler(VideoFrame colorFrame, DepthFrame depthFrame);
public event NewFrameEventHandler NewFrameArrived;
public event NewFrameEventHandler NewFrameArrived11;
///
/// 初始化設定並連接RealSenseL515相機
///
internal void Connect()
{
var ctx = new Context();
// 查找L515设备
var devices = ctx.QueryDevices();
var adev = devices.FirstOrDefault(d => d.Info[CameraInfo.Name] == "Intel RealSense L515");
if (adev == null)
{
throw new Exception("No RealSense L515 detected");
}
// 连接成功
Console.WriteLine("Connected to: " + adev.Info[CameraInfo.Name]);
}
///
/// 開RealSenseL515相機
///
[Obsolete]
internal void OpenCamera() //開啟相機
{
Connect();
var cfg = new Config(); // 通過序列號啟用設備
cfg.EnableStream(Stream.Depth, 640, 480, Format.Z16, 30);
cfg.EnableStream(Stream.Color, 640, 480, Format.Rgb8, 30);
pipeline = new Pipeline(); // 建立管線
var pp = pipeline.Start(cfg);// 啟動管線
// 獲取深度流和彩色流的配置
depthStream = pp.GetStream(Stream.Depth);
colorStream = pp.GetStream(Stream.Color);
SharedExtrinsics = depthStream.GetExtrinsicsTo(colorStream);// 獲取深度流到彩色流的外部參數 // extrinsics.rotation 是列主要的旋轉矩陣 // extrinsics.translation 是以米為單位的 xyz 位移
//獲取並應用深度到彩色的外部參數
var p1 = SharedExtrinsics.Transform(new Intel.RealSense.Math.Vector());
Console.WriteLine($"將外部參數應用到原點:({p1.x}, {p1.y}, {p1.z})");
InternalAndExternalGinseng();
var sensor = pp.Device.QuerySensors().First(s => s.Is(Extension.DepthSensor)); // 取得深度感測器
var blocks = sensor.ProcessingBlocks.ToList(); // 取得處理區塊
Sensor sensor1 = pp.Device.Sensors[0];
scale = sensor1.DepthScale;
Console.WriteLine("取得深度單位" + scale + "m");
try
{
// 建立自定義處理區塊
block = new CustomProcessingBlock((f, src) =>
{
using (var releaser = new FramesReleaser())
{
// 對每個框架執行處理區塊
foreach (ProcessingBlock p in blocks)
{
f = p.Process(f).DisposeWith(releaser);
// 對齊
f = f.ApplyFilter(align).DisposeWith(releaser);
// 使用 colorizer 處理區塊上色
f = f.ApplyFilter(colorizer).DisposeWith(releaser);
// 取得影格
var frames = f.As