|
|
@@ -5,6 +5,7 @@ using OxyPlot.Axes;
|
|
|
using OxyPlot.Series;
|
|
|
using Prism.Commands;
|
|
|
using Prism.Mvvm;
|
|
|
+using SqlSugar;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Collections.ObjectModel;
|
|
|
@@ -13,9 +14,12 @@ using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows.Input;
|
|
|
using System.Windows.Media;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
+using System.Windows.Threading;
|
|
|
using TeamAAS_VP.Core;
|
|
|
using TeamAAS_VP.Enums;
|
|
|
using TeamAAS_VP.Models;
|
|
|
+using TeamAAS_VP.Services;
|
|
|
|
|
|
namespace TeamAAS_VP.Controls
|
|
|
{
|
|
|
@@ -24,588 +28,449 @@ namespace TeamAAS_VP.Controls
|
|
|
/// </summary>
|
|
|
public class FocusAnalyzerViewModel : BindableBase
|
|
|
{
|
|
|
- #region 可绑定属性
|
|
|
+ private readonly FileDialogService _fileDialogService;
|
|
|
+ private readonly ImageAnalysisService _imageAnalysisService;
|
|
|
+ private readonly CheckerboardDetectionService _checkerboardService;
|
|
|
+ private readonly DispatcherTimer _analysisTimer;
|
|
|
+
|
|
|
+ private string _title = "棋盘格清晰度分析工具";
|
|
|
+ private BitmapSource _currentImage;
|
|
|
+ private Mat _currentMat;
|
|
|
+ private AnalysisParameters _parameters;
|
|
|
+ private AnalysisResult _currentResult;
|
|
|
+ private RoiModel _currentRoi;
|
|
|
+ private ChessboardInfo _chessboardInfo;
|
|
|
+ private RoiOperationMode _roiMode;
|
|
|
+ private string _statusText;
|
|
|
+ private bool _isAnalyzing;
|
|
|
+ private double _bestSharpness;
|
|
|
+ private int _frameCount;
|
|
|
+ private PlotModel _plotModel;
|
|
|
+ private List<AnalysisResult> _resultHistory;
|
|
|
+
|
|
|
+ // function to convert ROI from canvas coords to image pixel Rect (set by view)
|
|
|
+ public Func<RoiModel, OpenCvSharp.Rect?> RoiToImageRectConverter { get; set; }
|
|
|
+
|
|
|
+ public string Title
|
|
|
+ {
|
|
|
+ get => _title;
|
|
|
+ set => SetProperty(ref _title, value);
|
|
|
+ }
|
|
|
|
|
|
- // 图像源
|
|
|
- private ImageSource _currentImage;
|
|
|
- public ImageSource CurrentImage
|
|
|
+ public BitmapSource CurrentImage
|
|
|
{
|
|
|
get => _currentImage;
|
|
|
set => SetProperty(ref _currentImage, value);
|
|
|
}
|
|
|
|
|
|
- // 分析参数
|
|
|
- private AnalysisParameters _parameters = new AnalysisParameters();
|
|
|
public AnalysisParameters Parameters
|
|
|
{
|
|
|
get => _parameters;
|
|
|
set => SetProperty(ref _parameters, value);
|
|
|
}
|
|
|
|
|
|
- // 当前结果
|
|
|
- private AnalysisResult _currentResult = new AnalysisResult();
|
|
|
public AnalysisResult CurrentResult
|
|
|
{
|
|
|
get => _currentResult;
|
|
|
set => SetProperty(ref _currentResult, value);
|
|
|
}
|
|
|
|
|
|
- // 最终报告
|
|
|
- private AnalysisReport _finalReport = new AnalysisReport();
|
|
|
- public AnalysisReport FinalReport
|
|
|
- {
|
|
|
- get => _finalReport;
|
|
|
- set => SetProperty(ref _finalReport, value);
|
|
|
- }
|
|
|
-
|
|
|
- // 分析状态
|
|
|
- private bool _isAnalyzing;
|
|
|
- public bool IsAnalyzing
|
|
|
- {
|
|
|
- get => _isAnalyzing;
|
|
|
- set => SetProperty(ref _isAnalyzing, value);
|
|
|
- }
|
|
|
-
|
|
|
- // ROI区域
|
|
|
- private RotatedRect _currentRoi;
|
|
|
- public RotatedRect CurrentRoi
|
|
|
+ public RoiModel CurrentRoi
|
|
|
{
|
|
|
get => _currentRoi;
|
|
|
set => SetProperty(ref _currentRoi, value);
|
|
|
}
|
|
|
|
|
|
- // 是否显示ROI
|
|
|
- private bool _showRoi = true;
|
|
|
- public bool ShowRoi
|
|
|
+ public ChessboardInfo ChessboardInfo
|
|
|
{
|
|
|
- get => _showRoi;
|
|
|
- set => SetProperty(ref _showRoi, value);
|
|
|
+ get => _chessboardInfo;
|
|
|
+ set => SetProperty(ref _chessboardInfo, value);
|
|
|
}
|
|
|
|
|
|
- // ROI操作模式
|
|
|
- private RoiOperationMode _roiMode = RoiOperationMode.Draw;
|
|
|
public RoiOperationMode RoiMode
|
|
|
{
|
|
|
get => _roiMode;
|
|
|
set => SetProperty(ref _roiMode, value);
|
|
|
}
|
|
|
|
|
|
- // 图表模型
|
|
|
- private PlotModel _plotModel;
|
|
|
- public PlotModel PlotModel
|
|
|
- {
|
|
|
- get => _plotModel;
|
|
|
- set => SetProperty(ref _plotModel, value);
|
|
|
- }
|
|
|
-
|
|
|
- // 状态文本
|
|
|
- private string _statusText = "就绪";
|
|
|
public string StatusText
|
|
|
{
|
|
|
get => _statusText;
|
|
|
set => SetProperty(ref _statusText, value);
|
|
|
}
|
|
|
|
|
|
- // 帧数文本
|
|
|
- private string _frameCountText = "帧数: 0";
|
|
|
- public string FrameCountText
|
|
|
+ public bool IsAnalyzing
|
|
|
{
|
|
|
- get => _frameCountText;
|
|
|
- set => SetProperty(ref _frameCountText, value);
|
|
|
+ get => _isAnalyzing;
|
|
|
+ set => SetProperty(ref _isAnalyzing, value);
|
|
|
}
|
|
|
|
|
|
- // 最佳清晰度文本
|
|
|
- private string _bestSharpnessText = "最佳: 0.00";
|
|
|
- public string BestSharpnessText
|
|
|
+ public PlotModel PlotModel
|
|
|
{
|
|
|
- get => _bestSharpnessText;
|
|
|
- set => SetProperty(ref _bestSharpnessText, value);
|
|
|
+ get => _plotModel;
|
|
|
+ set => SetProperty(ref _plotModel, value);
|
|
|
}
|
|
|
|
|
|
- // 枚举列表
|
|
|
- public Array FocusMethods => Enum.GetValues(typeof(FocusMethod));
|
|
|
- public Array AnalysisModes => Enum.GetValues(typeof(AnalysisMode));
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
- #region 命令
|
|
|
-
|
|
|
- // 控制命令(绑定到外部)
|
|
|
- public ICommand StartAnalysisCommand { get; set; }
|
|
|
- public ICommand StopAnalysisCommand { get; set; }
|
|
|
- public ICommand ResetCommand { get; set; }
|
|
|
- public ICommand ExportCommand { get; set; }
|
|
|
-
|
|
|
- // 内部命令
|
|
|
- public DelegateCommand<object> SetRoiModeCommand { get; private set; }
|
|
|
- public DelegateCommand ClearRoiCommand { get; private set; }
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
- #region 事件
|
|
|
-
|
|
|
- // 控件事件(供外部订阅)
|
|
|
- public event EventHandler AnalysisStarted;
|
|
|
- public event EventHandler AnalysisStopped;
|
|
|
- public event EventHandler RoiChanged;
|
|
|
- public event EventHandler<ErrorEventArgs> ErrorOccurred;
|
|
|
+ public string BestSharpnessText => _bestSharpness > 0 ? $"{_bestSharpness:F2}" : "N/A";
|
|
|
+ public string FrameCountText => $"帧数: {_frameCount}";
|
|
|
|
|
|
- #endregion
|
|
|
+ public ObservableCollection<FocusMethod> FocusMethods { get; }
|
|
|
+ public ObservableCollection<AnalysisMode> AnalysisModes { get; }
|
|
|
|
|
|
- #region 私有字段
|
|
|
-
|
|
|
- private int _frameCounter = 0;
|
|
|
- private double _bestSharpness = 0;
|
|
|
- private DateTime _startTime;
|
|
|
- private ObservableCollection<Models.DataPoint> _historyData = new ObservableCollection<Models.DataPoint>();
|
|
|
-
|
|
|
- // ROI绘制状态
|
|
|
- private System.Windows.Point _roiStartPoint;
|
|
|
- private System.Windows.Point _roiDragStartPoint;
|
|
|
- private RotatedRect _roiBeforeDrag;
|
|
|
- private DragHandleType _currentDragHandle = DragHandleType.None;
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
- #region 构造函数
|
|
|
+ public DelegateCommand LoadImageCommand { get; }
|
|
|
+ public DelegateCommand StartAnalysisCommand { get; }
|
|
|
+ public DelegateCommand StopAnalysisCommand { get; }
|
|
|
+ public DelegateCommand ResetCommand { get; }
|
|
|
+ public DelegateCommand ExportCommand { get; }
|
|
|
+ public DelegateCommand ClearRoiCommand { get; }
|
|
|
+ public DelegateCommand DetectChessboardCommand { get; }
|
|
|
|
|
|
public FocusAnalyzerViewModel()
|
|
|
{
|
|
|
- InitializeCommands();
|
|
|
- InitializeProperties();
|
|
|
- InitializePlot();
|
|
|
- }
|
|
|
+ _fileDialogService = new FileDialogService();
|
|
|
+ _imageAnalysisService = new ImageAnalysisService();
|
|
|
+ _checkerboardService = new CheckerboardDetectionService();
|
|
|
|
|
|
- #endregion
|
|
|
+ _parameters = new AnalysisParameters();
|
|
|
+ _currentResult = new AnalysisResult();
|
|
|
+ _currentRoi = new RoiModel();
|
|
|
+ _resultHistory = new List<AnalysisResult>();
|
|
|
+ _roiMode = RoiOperationMode.Draw; // 默认为绘制模式
|
|
|
|
|
|
- #region 初始化
|
|
|
+ FocusMethods = new ObservableCollection<FocusMethod>(Enum.GetValues(typeof(FocusMethod)).Cast<FocusMethod>());
|
|
|
+ AnalysisModes = new ObservableCollection<AnalysisMode>(Enum.GetValues(typeof(AnalysisMode)).Cast<AnalysisMode>());
|
|
|
|
|
|
- private void InitializeCommands()
|
|
|
- {
|
|
|
- // ROI模式切换命令
|
|
|
- SetRoiModeCommand = new DelegateCommand<object>(mode =>
|
|
|
- {
|
|
|
- RoiMode = (RoiOperationMode)mode;
|
|
|
- });
|
|
|
+ LoadImageCommand = new DelegateCommand(OnLoadImage);
|
|
|
+ StartAnalysisCommand = new DelegateCommand(OnStartAnalysis, CanStartAnalysis).ObservesProperty(() => CurrentImage);
|
|
|
+ StopAnalysisCommand = new DelegateCommand(OnStopAnalysis, () => IsAnalyzing).ObservesProperty(() => IsAnalyzing);
|
|
|
+ ResetCommand = new DelegateCommand(OnReset);
|
|
|
+ ExportCommand = new DelegateCommand(OnExport, () => _resultHistory.Count > 0);
|
|
|
+ ClearRoiCommand = new DelegateCommand(OnClearRoi);
|
|
|
+ DetectChessboardCommand = new DelegateCommand(OnDetectChessboard, () => _currentMat != null);
|
|
|
|
|
|
- // 清除ROI命令
|
|
|
- ClearRoiCommand = new DelegateCommand(() =>
|
|
|
- {
|
|
|
- CurrentRoi = null;
|
|
|
- OnRoiChanged();
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- private void InitializeProperties()
|
|
|
- {
|
|
|
- Parameters = new AnalysisParameters();
|
|
|
- CurrentResult = new AnalysisResult();
|
|
|
- FinalReport = new AnalysisReport();
|
|
|
-
|
|
|
- // 初始化默认ROI
|
|
|
- CurrentRoi = new RotatedRect(100, 100, 200, 200, 0);
|
|
|
- }
|
|
|
+ InitializePlot();
|
|
|
|
|
|
- private void InitializePlot()
|
|
|
- {
|
|
|
- PlotModel = new PlotModel
|
|
|
+ _analysisTimer = new DispatcherTimer
|
|
|
{
|
|
|
- Title = "清晰度变化曲线",
|
|
|
- TitleFontSize = 12,
|
|
|
- PlotMargins = new OxyThickness(40, 10, 10, 40)
|
|
|
+ Interval = TimeSpan.FromMilliseconds(100)
|
|
|
};
|
|
|
+ _analysisTimer.Tick += OnAnalysisTick;
|
|
|
|
|
|
- PlotModel.Axes.Add(new LinearAxis
|
|
|
- {
|
|
|
- Position = AxisPosition.Bottom,
|
|
|
- Title = "帧序列",
|
|
|
- MajorGridlineStyle = LineStyle.Solid,
|
|
|
- MinorGridlineStyle = LineStyle.Dot
|
|
|
- });
|
|
|
+ StatusText = "就绪";
|
|
|
+ }
|
|
|
|
|
|
- PlotModel.Axes.Add(new LinearAxis
|
|
|
+ private void OnLoadImage()
|
|
|
+ {
|
|
|
+ try
|
|
|
{
|
|
|
- Position = AxisPosition.Left,
|
|
|
- Title = "清晰度",
|
|
|
- MajorGridlineStyle = LineStyle.Solid,
|
|
|
- MinorGridlineStyle = LineStyle.Dot
|
|
|
- });
|
|
|
+ string filePath = _fileDialogService.OpenImageFile();
|
|
|
+ if (string.IsNullOrEmpty(filePath))
|
|
|
+ return;
|
|
|
|
|
|
- PlotModel.Series.Add(new LineSeries
|
|
|
- {
|
|
|
- Title = "清晰度",
|
|
|
- Color = OxyColors.DodgerBlue,
|
|
|
- StrokeThickness = 2
|
|
|
- });
|
|
|
- }
|
|
|
+ // 释放之前的图像
|
|
|
+ _currentMat?.Dispose();
|
|
|
|
|
|
- #endregion
|
|
|
+ // 加载图像
|
|
|
+ _currentMat = Cv2.ImRead(filePath, ImreadModes.Color);
|
|
|
+ if (_currentMat == null || _currentMat.Empty())
|
|
|
+ {
|
|
|
+ StatusText = "无法加载图像";
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- #region 公共方法
|
|
|
+ // 转换为WPF图像
|
|
|
+ CurrentImage = _currentMat.ToWriteableBitmap();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 处理图像帧
|
|
|
- /// </summary>
|
|
|
- public async Task ProcessImageFrameAsync(Mat frame)
|
|
|
- {
|
|
|
- if (!IsAnalyzing || frame == null || frame.Empty())
|
|
|
- return;
|
|
|
+ // 自动检测棋盘格
|
|
|
+ OnDetectChessboard();
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
- // 更新图像
|
|
|
- await App.Current.Dispatcher.InvokeAsync(() =>
|
|
|
+ // 如果启用了实时分析,立即分析
|
|
|
+ if (Parameters.EnableRealtimeAnalysis)
|
|
|
{
|
|
|
- CurrentImage = BitmapSourceConverter.ToBitmapSource(frame);
|
|
|
- });
|
|
|
-
|
|
|
- // 执行分析
|
|
|
- await Task.Run(() => PerformAnalysis(frame));
|
|
|
- _frameCounter++;
|
|
|
+ AnalyzeCurrentImage();
|
|
|
+ }
|
|
|
|
|
|
- // 更新UI
|
|
|
- UpdateStatusText();
|
|
|
+ StatusText = $"图像已加载: {_currentMat.Width}x{_currentMat.Height}";
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- OnErrorOccurred(new ErrorEventArgs($"处理图像帧失败: {ex.Message}", ex));
|
|
|
+ StatusText = $"加载图像失败: {ex.Message}";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 执行单次分析
|
|
|
- /// </summary>
|
|
|
- public void PerformAnalysis(Mat frame)
|
|
|
+ private void OnDetectChessboard()
|
|
|
{
|
|
|
- if (frame == null || frame.Empty())
|
|
|
+ if (_currentMat == null || _currentMat.Empty())
|
|
|
return;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- // 将RotatedRect转换为Rect(简化处理)
|
|
|
- Rect? roiRect = null;
|
|
|
- if (CurrentRoi != null)
|
|
|
+ StatusText = "正在检测棋盘格...";
|
|
|
+
|
|
|
+ Task.Run(() =>
|
|
|
{
|
|
|
- roiRect = new Rect((int)CurrentRoi.X, (int)CurrentRoi.Y, (int)CurrentRoi.Width, (int)CurrentRoi.Height);
|
|
|
- }
|
|
|
+ ChessboardInfo info = null;
|
|
|
|
|
|
- // 计算清晰度
|
|
|
- double sharpness = FocusAnalysisEngine.CalculateImageSharpness(
|
|
|
- frame, Parameters.FocusMethod, roiRect);
|
|
|
+ if (RoiToImageRectConverter != null && CurrentRoi?.IsValid == true)
|
|
|
+ {
|
|
|
+ var imgRoi = RoiToImageRectConverter(CurrentRoi);
|
|
|
+ if (imgRoi.HasValue)
|
|
|
+ {
|
|
|
+ using (var sub = new Mat(_currentMat, imgRoi.Value))
|
|
|
+ {
|
|
|
+ info = _checkerboardService.DetectChessboard(sub);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (info != null && info.IsDetected)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < info.Corners.Length; i++)
|
|
|
+ {
|
|
|
+ info.Corners[i].X += imgRoi.Value.X;
|
|
|
+ info.Corners[i].Y += imgRoi.Value.Y;
|
|
|
+ }
|
|
|
+ var br = info.BoundingRect;
|
|
|
+ info.BoundingRect = new OpenCvSharp.Rect(br.X + imgRoi.Value.X, br.Y + imgRoi.Value.Y, br.Width, br.Height);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 分析棋盘格
|
|
|
- CheckerboardResult checkerboard;
|
|
|
- if (Parameters.AnalysisMode == AnalysisMode.AutoCheckerboard)
|
|
|
- {
|
|
|
- checkerboard = FocusAnalysisEngine.AnalyzeCheckerboardContrast(frame, roiRect);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- checkerboard = new CheckerboardResult();
|
|
|
- }
|
|
|
+ if (info == null || !info.IsDetected)
|
|
|
+ {
|
|
|
+ info = _checkerboardService.DetectChessboard(_currentMat);
|
|
|
+ }
|
|
|
|
|
|
- // 更新结果
|
|
|
- App.Current.Dispatcher.Invoke(() =>
|
|
|
- {
|
|
|
- UpdateAnalysisResult(sharpness, checkerboard);
|
|
|
- UpdateHistoryData(sharpness, checkerboard);
|
|
|
- UpdatePlot();
|
|
|
+ System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
|
|
+ {
|
|
|
+ ChessboardInfo = info;
|
|
|
+
|
|
|
+ if (info.IsDetected)
|
|
|
+ {
|
|
|
+ StatusText = $"检测成功: {info.CornersWidth}x{info.CornersHeight} 角点";
|
|
|
+
|
|
|
+ // 绘制棋盘格
|
|
|
+ var displayMat = _currentMat.Clone();
|
|
|
+ _checkerboardService.DrawChessboard(displayMat, info);
|
|
|
+ CurrentImage = displayMat.ToWriteableBitmap();
|
|
|
+ displayMat.Dispose();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ StatusText = "未检测到棋盘格";
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- OnErrorOccurred(new ErrorEventArgs($"图像分析失败: {ex.Message}", ex));
|
|
|
+ StatusText = $"检测失败: {ex.Message}";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
-
|
|
|
- #region ROI绘制方法
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 开始ROI绘制
|
|
|
- /// </summary>
|
|
|
- public void StartRoiDrawing(System.Windows.Point startPoint)
|
|
|
+ private bool CanStartAnalysis()
|
|
|
{
|
|
|
- if (RoiMode != RoiOperationMode.Draw)
|
|
|
- return;
|
|
|
-
|
|
|
- _roiStartPoint = startPoint;
|
|
|
- CurrentRoi = new RotatedRect(startPoint.X, startPoint.Y, 0, 0, 0);
|
|
|
+ return CurrentImage != null && !IsAnalyzing;
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 更新ROI绘制
|
|
|
- /// </summary>
|
|
|
- public void UpdateRoiDrawing(System.Windows.Point currentPoint)
|
|
|
+ private void OnStartAnalysis()
|
|
|
{
|
|
|
- if (RoiMode != RoiOperationMode.Draw || CurrentRoi == null)
|
|
|
- return;
|
|
|
-
|
|
|
- double x = Math.Min(_roiStartPoint.X, currentPoint.X);
|
|
|
- double y = Math.Min(_roiStartPoint.Y, currentPoint.Y);
|
|
|
- double width = Math.Abs(currentPoint.X - _roiStartPoint.X);
|
|
|
- double height = Math.Abs(currentPoint.Y - _roiStartPoint.Y);
|
|
|
+ IsAnalyzing = true;
|
|
|
+ _frameCount = 0;
|
|
|
+ _bestSharpness = 0;
|
|
|
+ _resultHistory.Clear();
|
|
|
+ StatusText = "分析中...";
|
|
|
|
|
|
- CurrentRoi.X = x;
|
|
|
- CurrentRoi.Y = y;
|
|
|
- CurrentRoi.Width = width;
|
|
|
- CurrentRoi.Height = height;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 完成ROI绘制
|
|
|
- /// </summary>
|
|
|
- public void FinishRoiDrawing()
|
|
|
- {
|
|
|
- OnRoiChanged();
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 开始ROI拖动
|
|
|
- /// </summary>
|
|
|
- public bool StartRoiDrag(System.Windows.Point point)
|
|
|
- {
|
|
|
- if (CurrentRoi == null || !CurrentRoi.ContainsPoint(point))
|
|
|
- return false;
|
|
|
-
|
|
|
- // 检查是否点击了控制点
|
|
|
- _currentDragHandle = GetDragHandleAtPoint(point);
|
|
|
-
|
|
|
- if (_currentDragHandle != DragHandleType.None)
|
|
|
+ if (Parameters.EnableRealtimeAnalysis)
|
|
|
{
|
|
|
- _roiDragStartPoint = point;
|
|
|
- _roiBeforeDrag = new RotatedRect(
|
|
|
- CurrentRoi.X, CurrentRoi.Y,
|
|
|
- CurrentRoi.Width, CurrentRoi.Height,
|
|
|
- CurrentRoi.Angle);
|
|
|
- return true;
|
|
|
+ _analysisTimer.Start();
|
|
|
}
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 更新ROI拖动
|
|
|
- /// </summary>
|
|
|
- public void UpdateRoiDrag(System.Windows.Point currentPoint)
|
|
|
- {
|
|
|
- if (CurrentRoi == null || _roiBeforeDrag == null)
|
|
|
- return;
|
|
|
-
|
|
|
- double deltaX = currentPoint.X - _roiDragStartPoint.X;
|
|
|
- double deltaY = currentPoint.Y - _roiDragStartPoint.Y;
|
|
|
-
|
|
|
- switch (_currentDragHandle)
|
|
|
+ else
|
|
|
{
|
|
|
- case DragHandleType.TopLeft:
|
|
|
- CurrentRoi.X = _roiBeforeDrag.X + deltaX;
|
|
|
- CurrentRoi.Y = _roiBeforeDrag.Y + deltaY;
|
|
|
- CurrentRoi.Width = _roiBeforeDrag.Width - deltaX;
|
|
|
- CurrentRoi.Height = _roiBeforeDrag.Height - deltaY;
|
|
|
- break;
|
|
|
-
|
|
|
- case DragHandleType.TopRight:
|
|
|
- CurrentRoi.Y = _roiBeforeDrag.Y + deltaY;
|
|
|
- CurrentRoi.Width = _roiBeforeDrag.Width + deltaX;
|
|
|
- CurrentRoi.Height = _roiBeforeDrag.Height - deltaY;
|
|
|
- break;
|
|
|
-
|
|
|
- case DragHandleType.BottomLeft:
|
|
|
- CurrentRoi.X = _roiBeforeDrag.X + deltaX;
|
|
|
- CurrentRoi.Width = _roiBeforeDrag.Width - deltaX;
|
|
|
- CurrentRoi.Height = _roiBeforeDrag.Height + deltaY;
|
|
|
- break;
|
|
|
-
|
|
|
- case DragHandleType.BottomRight:
|
|
|
- CurrentRoi.Width = _roiBeforeDrag.Width + deltaX;
|
|
|
- CurrentRoi.Height = _roiBeforeDrag.Height + deltaY;
|
|
|
- break;
|
|
|
-
|
|
|
- case DragHandleType.Rotate:
|
|
|
- // 计算旋转角度
|
|
|
- var center = new System.Windows.Point(
|
|
|
- _roiBeforeDrag.CenterX,
|
|
|
- _roiBeforeDrag.CenterY);
|
|
|
- double startAngle = Math.Atan2(
|
|
|
- _roiDragStartPoint.Y - center.Y,
|
|
|
- _roiDragStartPoint.X - center.X);
|
|
|
- double currentAngle = Math.Atan2(
|
|
|
- currentPoint.Y - center.Y,
|
|
|
- currentPoint.X - center.X);
|
|
|
- double angleDelta = (currentAngle - startAngle) * 180 / Math.PI;
|
|
|
- CurrentRoi.Angle = (_roiBeforeDrag.Angle + angleDelta) % 360;
|
|
|
- break;
|
|
|
-
|
|
|
- case DragHandleType.None:
|
|
|
- // 移动整个ROI
|
|
|
- CurrentRoi.X = _roiBeforeDrag.X + deltaX;
|
|
|
- CurrentRoi.Y = _roiBeforeDrag.Y + deltaY;
|
|
|
- break;
|
|
|
+ AnalyzeCurrentImage();
|
|
|
+ IsAnalyzing = false;
|
|
|
+ StatusText = "分析完成";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 结束ROI拖动
|
|
|
- /// </summary>
|
|
|
- public void FinishRoiDrag()
|
|
|
+ private void OnStopAnalysis()
|
|
|
{
|
|
|
- _currentDragHandle = DragHandleType.None;
|
|
|
- _roiBeforeDrag = null;
|
|
|
- OnRoiChanged();
|
|
|
+ _analysisTimer.Stop();
|
|
|
+ IsAnalyzing = false;
|
|
|
+ StatusText = "已停止";
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 获取指定点的控制点类型
|
|
|
- /// </summary>
|
|
|
- private DragHandleType GetDragHandleAtPoint(System.Windows.Point point)
|
|
|
+ private void OnAnalysisTick(object sender, EventArgs e)
|
|
|
{
|
|
|
- if (CurrentRoi == null) return DragHandleType.None;
|
|
|
-
|
|
|
- const double handleSize = 8;
|
|
|
- var center = new System.Windows.Point(CurrentRoi.CenterX, CurrentRoi.CenterY);
|
|
|
+ AnalyzeCurrentImage();
|
|
|
+ }
|
|
|
|
|
|
- // 检查旋转控制点(在矩形顶部中心上方)
|
|
|
- var rotateHandle = new System.Windows.Point(center.X, CurrentRoi.Y - 20);
|
|
|
- if (Distance(point, rotateHandle) <= handleSize)
|
|
|
- return DragHandleType.Rotate;
|
|
|
+ private void AnalyzeCurrentImage()
|
|
|
+ {
|
|
|
+ if (_currentMat == null || _currentMat.Empty())
|
|
|
+ return;
|
|
|
|
|
|
- // 检查角点
|
|
|
- var corners = new[]
|
|
|
+ try
|
|
|
{
|
|
|
- new System.Windows.Point(CurrentRoi.X, CurrentRoi.Y), // 左上
|
|
|
- new System.Windows.Point(CurrentRoi.X + CurrentRoi.Width, CurrentRoi.Y), // 右上
|
|
|
- new System.Windows.Point(CurrentRoi.X, CurrentRoi.Y + CurrentRoi.Height), // 左下
|
|
|
- new System.Windows.Point(CurrentRoi.X + CurrentRoi.Width, CurrentRoi.Y + CurrentRoi.Height) // 右下
|
|
|
- };
|
|
|
+ Rect? roi = null;
|
|
|
|
|
|
- // 替换 switch 表达式为 switch 语句以兼容 C# 7.3
|
|
|
- for (int i = 0; i < corners.Length; i++)
|
|
|
- {
|
|
|
- if (Distance(point, corners[i]) <= handleSize)
|
|
|
+ // 根据分析模式确定分析区域
|
|
|
+ switch (Parameters.AnalysisMode)
|
|
|
{
|
|
|
- switch (i)
|
|
|
- {
|
|
|
- case 0:
|
|
|
- return DragHandleType.TopLeft;
|
|
|
- case 1:
|
|
|
- return DragHandleType.TopRight;
|
|
|
- case 2:
|
|
|
- return DragHandleType.BottomLeft;
|
|
|
- case 3:
|
|
|
- return DragHandleType.BottomRight;
|
|
|
- default:
|
|
|
- return DragHandleType.None;
|
|
|
- }
|
|
|
+ case AnalysisMode.RoiOnly:
|
|
|
+ if (CurrentRoi.IsValid)
|
|
|
+ {
|
|
|
+ if (RoiToImageRectConverter != null)
|
|
|
+ {
|
|
|
+ roi = RoiToImageRectConverter(CurrentRoi);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ roi = new Rect(
|
|
|
+ (int)CurrentRoi.X,
|
|
|
+ (int)CurrentRoi.Y,
|
|
|
+ (int)CurrentRoi.Width,
|
|
|
+ (int)CurrentRoi.Height);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case AnalysisMode.ChessboardOnly:
|
|
|
+ if (ChessboardInfo?.IsDetected == true)
|
|
|
+ {
|
|
|
+ roi = ChessboardInfo.BoundingRect;
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- return DragHandleType.None;
|
|
|
- }
|
|
|
+ // 执行分析
|
|
|
+ var result = _imageAnalysisService.AnalyzeImage(_currentMat, Parameters.FocusMethod, roi);
|
|
|
|
|
|
- private double Distance(System.Windows.Point p1, System.Windows.Point p2)
|
|
|
- {
|
|
|
- return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
|
|
|
- }
|
|
|
+ CurrentResult = result;
|
|
|
+ _resultHistory.Add(result);
|
|
|
+ _frameCount++;
|
|
|
|
|
|
- #endregion
|
|
|
+ // 更新最佳清晰度
|
|
|
+ if (result.Sharpness > _bestSharpness)
|
|
|
+ {
|
|
|
+ _bestSharpness = result.Sharpness;
|
|
|
+ RaisePropertyChanged(nameof(BestSharpnessText));
|
|
|
+ }
|
|
|
|
|
|
- #region 私有方法
|
|
|
+ RaisePropertyChanged(nameof(FrameCountText));
|
|
|
|
|
|
- private void UpdateAnalysisResult(double sharpness, CheckerboardResult checkerboard)
|
|
|
- {
|
|
|
- double qualityScore = FocusAnalysisEngine.CalculateQualityScore(sharpness, checkerboard);
|
|
|
- var suggestions = FocusAnalysisEngine.GenerateSuggestions(
|
|
|
- sharpness, checkerboard,
|
|
|
- Parameters.SharpnessThreshold,
|
|
|
- Parameters.ContrastThreshold);
|
|
|
-
|
|
|
- if (sharpness > _bestSharpness)
|
|
|
- _bestSharpness = sharpness;
|
|
|
-
|
|
|
- CurrentResult.Sharpness = sharpness;
|
|
|
- CurrentResult.Checkerboard = checkerboard;
|
|
|
- CurrentResult.QualityScore = qualityScore;
|
|
|
- CurrentResult.FrameCount = _frameCounter + 1;
|
|
|
- CurrentResult.BestSharpness = _bestSharpness;
|
|
|
- CurrentResult.AnalysisTime = DateTime.Now;
|
|
|
- CurrentResult.Suggestions = new ObservableCollection<string>(suggestions);
|
|
|
+ // 更新图表
|
|
|
+ UpdatePlot();
|
|
|
+
|
|
|
+ StatusText = $"清晰度: {result.Sharpness:F2}, 对比度: {result.Contrast:F2}";
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ StatusText = $"分析错误: {ex.Message}";
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void UpdateHistoryData(double sharpness, CheckerboardResult checkerboard)
|
|
|
+ private void OnReset()
|
|
|
{
|
|
|
- var dataPoint = new Models.DataPoint(_frameCounter, sharpness,
|
|
|
- checkerboard.Detected ? checkerboard.Contrast : 0);
|
|
|
- _historyData.Add(dataPoint);
|
|
|
-
|
|
|
- // 添加到最终报告
|
|
|
- FinalReport.HistoryData.Add(dataPoint);
|
|
|
+ OnStopAnalysis();
|
|
|
+ _frameCount = 0;
|
|
|
+ _bestSharpness = 0;
|
|
|
+ _resultHistory.Clear();
|
|
|
+ CurrentResult = new AnalysisResult();
|
|
|
+ InitializePlot();
|
|
|
+ StatusText = "已重置";
|
|
|
+ RaisePropertyChanged(nameof(BestSharpnessText));
|
|
|
+ RaisePropertyChanged(nameof(FrameCountText));
|
|
|
}
|
|
|
|
|
|
- private void UpdatePlot()
|
|
|
+ private void OnExport()
|
|
|
{
|
|
|
- if (PlotModel.Series[0] is LineSeries series)
|
|
|
+ try
|
|
|
{
|
|
|
- series.Points.Clear();
|
|
|
+ string filePath = _fileDialogService.SaveFile(
|
|
|
+ $"分析报告_{DateTime.Now:yyyyMMdd_HHmmss}.csv",
|
|
|
+ "CSV文件|*.csv|所有文件|*.*");
|
|
|
|
|
|
- for (int i = 0; i < _historyData.Count; i++)
|
|
|
- {
|
|
|
- series.Points.Add(new OxyPlot.DataPoint(i, _historyData[i].Sharpness));
|
|
|
- }
|
|
|
+ if (string.IsNullOrEmpty(filePath))
|
|
|
+ return;
|
|
|
|
|
|
- // 限制显示点数
|
|
|
- if (series.Points.Count > 200)
|
|
|
+ using (var writer = new System.IO.StreamWriter(filePath))
|
|
|
{
|
|
|
- series.Points.RemoveRange(0, series.Points.Count - 200);
|
|
|
+ writer.WriteLine("时间,清晰度,对比度,质量评分");
|
|
|
+ foreach (var result in _resultHistory)
|
|
|
+ {
|
|
|
+ writer.WriteLine($"{result.Timestamp:yyyy-MM-dd HH:mm:ss.fff},{result.Sharpness:F2},{result.Contrast:F2},{result.QualityScore:F2}");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- PlotModel.InvalidatePlot(true);
|
|
|
+ StatusText = $"报告已导出: {filePath}";
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ StatusText = $"导出失败: {ex.Message}";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void UpdateStatusText()
|
|
|
+ private void OnClearRoi()
|
|
|
{
|
|
|
- StatusText = $"分析中... 第 {_frameCounter + 1} 帧";
|
|
|
- FrameCountText = $"帧数: {_frameCounter + 1}";
|
|
|
- BestSharpnessText = $"最佳: {_bestSharpness:F2}";
|
|
|
+ CurrentRoi = new RoiModel();
|
|
|
+ StatusText = "ROI已清除";
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ private void InitializePlot()
|
|
|
+ {
|
|
|
+ PlotModel = new PlotModel
|
|
|
+ {
|
|
|
+ Title = "清晰度趋势",
|
|
|
+ Background = OxyColors.White
|
|
|
+ };
|
|
|
|
|
|
- #region 事件触发方法
|
|
|
+ PlotModel.Axes.Add(new LinearAxis
|
|
|
+ {
|
|
|
+ Position = AxisPosition.Bottom,
|
|
|
+ Title = "帧数",
|
|
|
+ MajorGridlineStyle = LineStyle.Solid,
|
|
|
+ MinorGridlineStyle = LineStyle.Dot,
|
|
|
+ MajorGridlineColor = OxyColor.FromRgb(230, 230, 230)
|
|
|
+ });
|
|
|
|
|
|
- protected virtual void OnAnalysisStarted()
|
|
|
- {
|
|
|
- AnalysisStarted?.Invoke(this, EventArgs.Empty);
|
|
|
- }
|
|
|
+ PlotModel.Axes.Add(new LinearAxis
|
|
|
+ {
|
|
|
+ Position = AxisPosition.Left,
|
|
|
+ Title = "清晰度",
|
|
|
+ MajorGridlineStyle = LineStyle.Solid,
|
|
|
+ MinorGridlineStyle = LineStyle.Dot,
|
|
|
+ MajorGridlineColor = OxyColor.FromRgb(230, 230, 230)
|
|
|
+ });
|
|
|
|
|
|
- protected virtual void OnAnalysisStopped()
|
|
|
- {
|
|
|
- AnalysisStopped?.Invoke(this, EventArgs.Empty);
|
|
|
- }
|
|
|
+ var series = new LineSeries
|
|
|
+ {
|
|
|
+ Title = "清晰度",
|
|
|
+ Color = OxyColor.FromRgb(0, 122, 204),
|
|
|
+ StrokeThickness = 2,
|
|
|
+ MarkerType = MarkerType.Circle,
|
|
|
+ MarkerSize = 3,
|
|
|
+ MarkerFill = OxyColor.FromRgb(0, 122, 204)
|
|
|
+ };
|
|
|
|
|
|
- protected virtual void OnRoiChanged()
|
|
|
- {
|
|
|
- RoiChanged?.Invoke(this, EventArgs.Empty);
|
|
|
+ PlotModel.Series.Add(series);
|
|
|
}
|
|
|
|
|
|
- protected virtual void OnErrorOccurred(ErrorEventArgs e)
|
|
|
+ private void UpdatePlot()
|
|
|
{
|
|
|
- ErrorOccurred?.Invoke(this, e);
|
|
|
- }
|
|
|
+ if (PlotModel.Series.Count == 0)
|
|
|
+ return;
|
|
|
|
|
|
- #endregion
|
|
|
- }
|
|
|
+ var series = PlotModel.Series[0] as LineSeries;
|
|
|
+ if (series == null)
|
|
|
+ return;
|
|
|
|
|
|
- public class ErrorEventArgs : EventArgs
|
|
|
- {
|
|
|
- public string Message { get; }
|
|
|
- public Exception Exception { get; }
|
|
|
+ series.Points.Clear();
|
|
|
|
|
|
- public ErrorEventArgs(string message, Exception exception = null)
|
|
|
- {
|
|
|
- Message = message;
|
|
|
- Exception = exception;
|
|
|
+ for (int i = 0; i < _resultHistory.Count; i++)
|
|
|
+ {
|
|
|
+ series.Points.Add(new OxyPlot.DataPoint(i + 1, _resultHistory[i].Sharpness));
|
|
|
+ }
|
|
|
+
|
|
|
+ PlotModel.InvalidatePlot(true);
|
|
|
}
|
|
|
}
|
|
|
}
|