leerform recipe subsystem created
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using LindtLeerformPlugin.Models;
|
||||
using LindtLeerformPlugin.Services;
|
||||
using OpenCvSharp;
|
||||
using VisionBuilder.UI.Common.Processing;
|
||||
@@ -14,12 +13,15 @@ public partial class CalibrationWindowViewModel : ObservableObject, IDisposable
|
||||
private readonly LeerformSettings _settings;
|
||||
private CancellationTokenSource? _previewCts;
|
||||
private Mat? _lastFrame;
|
||||
private Mat? _calibCameraMatrix;
|
||||
private Mat? _calibDistCoeffs;
|
||||
|
||||
[ObservableProperty] private Avalonia.Media.Imaging.Bitmap? _previewImage;
|
||||
[ObservableProperty] private string _statusText = "Ready";
|
||||
[ObservableProperty] private int _capturedImageCount;
|
||||
[ObservableProperty] private bool _isPreviewRunning;
|
||||
[ObservableProperty] private bool _isCalibrated;
|
||||
[ObservableProperty] private bool _applyCalibration;
|
||||
[ObservableProperty] private string _calibrationImageDirectory;
|
||||
[ObservableProperty] private double _rmsError;
|
||||
|
||||
@@ -29,8 +31,13 @@ public partial class CalibrationWindowViewModel : ObservableObject, IDisposable
|
||||
_settings = settings;
|
||||
|
||||
_calibrationImageDirectory = settings.CalibrationImageDirectory;
|
||||
_isCalibrated = settings.CalibrationData is { CameraMatrix.Length: > 0 };
|
||||
_rmsError = settings.CalibrationData?.RmsError ?? 0;
|
||||
|
||||
var calibrationData = CalibrationDataStore.Load();
|
||||
_isCalibrated = calibrationData is { CameraMatrix.Length: > 0 };
|
||||
_rmsError = calibrationData?.RmsError ?? 0;
|
||||
|
||||
if (_isCalibrated)
|
||||
LoadCalibrationMats(calibrationData!);
|
||||
|
||||
UpdateCapturedImageCount();
|
||||
}
|
||||
@@ -86,9 +93,10 @@ public partial class CalibrationWindowViewModel : ObservableObject, IDisposable
|
||||
var patternSize = new Size(_settings.CheckerboardCols, _settings.CheckerboardRows);
|
||||
var data = CameraCalibrationService.Calibrate(CalibrationImageDirectory, patternSize);
|
||||
|
||||
_settings.CalibrationData = data;
|
||||
CalibrationDataStore.Save(data);
|
||||
_settings.CalibrationImageDirectory = CalibrationImageDirectory;
|
||||
|
||||
LoadCalibrationMats(data);
|
||||
IsCalibrated = true;
|
||||
RmsError = data.RmsError;
|
||||
StatusText = $"Calibrated (RMS: {data.RmsError:F4})";
|
||||
@@ -122,7 +130,18 @@ public partial class CalibrationWindowViewModel : ObservableObject, IDisposable
|
||||
_lastFrame?.Dispose();
|
||||
_lastFrame = frame;
|
||||
|
||||
var bitmap = ImageConverter.MatToAvaloniaBitmap(frame);
|
||||
var displayFrame = frame;
|
||||
if (ApplyCalibration && _calibCameraMatrix != null && _calibDistCoeffs != null)
|
||||
{
|
||||
displayFrame = new Mat();
|
||||
Cv2.Undistort(frame, displayFrame, _calibCameraMatrix, _calibDistCoeffs);
|
||||
}
|
||||
|
||||
var bitmap = ImageConverter.MatToAvaloniaBitmap(displayFrame);
|
||||
|
||||
if (displayFrame != frame)
|
||||
displayFrame.Dispose();
|
||||
|
||||
await Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
var old = PreviewImage;
|
||||
@@ -143,9 +162,18 @@ public partial class CalibrationWindowViewModel : ObservableObject, IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadCalibrationMats(Models.CalibrationData data)
|
||||
{
|
||||
_calibCameraMatrix?.Dispose();
|
||||
_calibDistCoeffs?.Dispose();
|
||||
(_calibCameraMatrix, _calibDistCoeffs) = CameraCalibrationService.LoadCalibrationMats(data);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
StopPreview();
|
||||
_lastFrame?.Dispose();
|
||||
_calibCameraMatrix?.Dispose();
|
||||
_calibDistCoeffs?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user