支持零件标注图像滚轮缩放和右键拖动

This commit is contained in:
tfarcraw 2026-11-09 23:10:50 +08:00
parent 376188094f
commit 557ab871ed
4 changed files with 77 additions and 7 deletions

View File

@ -13,9 +13,42 @@ public sealed class AnnotationCanvas : Control
public double[]? Box {get=>GetValue(BoxProperty);set=>SetValue(BoxProperty,value);}
public event Action<double[]>? BoxDrawn;
private Point? _start;private Point _end;private IPointer? _pointer;
private double _zoom=1;
private Vector _offset;
private Point? _panStart;
static AnnotationCanvas(){AffectsRender<AnnotationCanvas>(SourceProperty,BoxProperty);}
public AnnotationCanvas(){ClipToBounds=true;Cursor=new Cursor(StandardCursorType.Cross);Focusable=true;}
private Rect ImageRect=>Source is null?default:VVMs.RecipeEditor.RegionCanvas.FitImage(Bounds.Size,new Size(Source.PixelWidth,Source.PixelHeight));
private Rect ImageRect
{
get
{
if(Source is null)return default;
var fit=VVMs.RecipeEditor.RegionCanvas.FitImage(Bounds.Size,new Size(Source.PixelWidth,Source.PixelHeight));
return new Rect(fit.X+_offset.X,fit.Y+_offset.Y,fit.Width*_zoom,fit.Height*_zoom);
}
}
public void ResetView()
{_zoom=1;_offset=default;EndGesture();InvalidateVisual();}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if(change.Property==SourceProperty)
{
EndGesture();
var previous=change.GetOldValue<BitmapSource?>();
if(Source is null||previous is null||Source.PixelWidth!=previous.PixelWidth||Source.PixelHeight!=previous.PixelHeight)ResetView();
}
}
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
{
if(!IsEnabled||Source is null||ImageRect.Width<=0)return;
e.Handled=true;
if(_pointer is not null||e.Delta.Y==0)return;
var zoom=Math.Clamp(_zoom*Math.Pow(1.2,e.Delta.Y),0.1,20);
var position=e.GetPosition(this);var image=ImageRect;var ratio=zoom/_zoom;
_offset+=new Vector((position.X-image.X)*(1-ratio),(position.Y-image.Y)*(1-ratio));
_zoom=zoom;InvalidateVisual();
}
public override void Render(DrawingContext dc)
{
dc.DrawRectangle(Brushes.Transparent,null,new Rect(Bounds.Size));var image=ImageRect;if(Source is null||image.Width<=0)return;
@ -26,18 +59,34 @@ public sealed class AnnotationCanvas : Control
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
if(!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed||!IsEnabled||Source is null||!ImageRect.Contains(e.GetPosition(this)))return;
if(!IsEnabled||Source is null||_pointer is not null)return;
if(e.GetCurrentPoint(this).Properties.PointerUpdateKind==PointerUpdateKind.RightButtonPressed)
{
Focus();_panStart=e.GetPosition(this);_pointer=e.Pointer;_pointer.Capture(this);
Cursor=new Cursor(StandardCursorType.SizeAll);e.Handled=true;return;
}
if(e.GetCurrentPoint(this).Properties.PointerUpdateKind!=PointerUpdateKind.LeftButtonPressed||!ImageRect.Contains(e.GetPosition(this)))return;
Focus();_start=_end=e.GetPosition(this);_pointer=e.Pointer;_pointer.Capture(this);InvalidateVisual();e.Handled=true;
}
protected override void OnPointerMoved(PointerEventArgs e)
{if(_start is null)return;var p=e.GetPosition(this);var image=ImageRect;_end=new Point(Math.Clamp(p.X,image.Left,image.Right),Math.Clamp(p.Y,image.Top,image.Bottom));InvalidateVisual();}
{
if(_panStart is {} pan)
{var position=e.GetPosition(this);_offset+=position-pan;_panStart=position;InvalidateVisual();e.Handled=true;return;}
if(_start is null)return;var p=e.GetPosition(this);var image=ImageRect;_end=new Point(Math.Clamp(p.X,image.Left,image.Right),Math.Clamp(p.Y,image.Top,image.Bottom));InvalidateVisual();
}
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
if(_start is not {} start||Source is null)return;
var rectangle=DragRect(start,_end);var image=ImageRect;_start=null;_pointer?.Capture(null);_pointer=null;InvalidateVisual();
if(_panStart is not null)
{if(e.InitialPressMouseButton==MouseButton.Right){EndGesture();e.Handled=true;}return;}
if(e.InitialPressMouseButton!=MouseButton.Left||_start is not {} start||Source is null)return;
var p=e.GetPosition(this);var image=ImageRect;
var rectangle=DragRect(start,new Point(Math.Clamp(p.X,image.Left,image.Right),Math.Clamp(p.Y,image.Top,image.Bottom)));
EndGesture();e.Handled=true;
if(rectangle.Width<3||rectangle.Height<3||image.Width<=0)return;
var scale=Source.PixelWidth/image.Width;BoxDrawn?.Invoke([(rectangle.Left-image.Left)*scale,(rectangle.Top-image.Top)*scale,Math.Min(Source.PixelWidth,(rectangle.Right-image.Left)*scale),Math.Min(Source.PixelHeight,(rectangle.Bottom-image.Top)*scale)]);
}
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e){_start=null;_pointer=null;InvalidateVisual();base.OnPointerCaptureLost(e);}
private void EndGesture()
{var pointer=_pointer;_start=null;_panStart=null;_pointer=null;pointer?.Capture(null);Cursor=new Cursor(StandardCursorType.Cross);InvalidateVisual();}
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e){EndGesture();base.OnPointerCaptureLost(e);}
private static Rect DragRect(Point a,Point b)=>new(Math.Min(a.X,b.X),Math.Min(a.Y,b.Y),Math.Abs(a.X-b.X),Math.Abs(a.Y-b.Y));
}

View File

@ -59,6 +59,7 @@
<local:AnnotationCanvas x:Name="Canvas" Source="{Binding Preview}" Box="{Binding CurrentBox}" IsEnabled="{Binding IsIdle}" />
</Border>
<WrapPanel Grid.Row="2" Margin="0,12,0,0">
<Button Content="适应窗口" Click="ResetImageViewClicked" />
<Button Content="← 上一帧" Command="{Binding PreviousCommand}" />
<Button Content="下一帧 →" Command="{Binding NextCommand}" />
<Button Content="确认当前框" Command="{Binding ApproveCommand}" />
@ -66,7 +67,7 @@
<Button Content="无目标" Command="{Binding AbsentCommand}" />
<Button Content="排除此帧" Command="{Binding ExcludeCommand}" />
</WrapPanel>
<TextBlock Grid.Row="3" Text="拖动画框或修正,自动保存为关键帧。← → 切帧,Enter 确认当前框并进入下一帧。" Classes="Hint" />
<TextBlock Grid.Row="3" Text="滚轮缩放,右键拖动图像,左键画框或修正并自动保存。← → 切帧,Enter 确认并进入下一帧。" Classes="Hint" TextWrapping="Wrap" />
</Grid>
<TabControl Grid.Column="4" IsEnabled="{Binding IsIdle}">
<TabItem Header="标注">

View File

@ -61,4 +61,5 @@ public partial class TrainingPage : UserControl
}
private void LogChanged(object? sender,TextChangedEventArgs e)
{if(sender is TextBox box)box.CaretIndex=box.Text?.Length??0;}
private void ResetImageViewClicked(object? sender,RoutedEventArgs e)=>Canvas.ResetView();
}

View File

@ -243,6 +243,25 @@ internal static class Smoke
double[]? drawn=null;var annotation=new AnnotationCanvas {Source=image};annotation.BoxDrawn+=box=>drawn=box;drawing.Content=annotation;await Task.Delay(60);
drawing.MouseDown(new Point(300,250),MouseButton.Left);drawing.MouseMove(new Point(100,150));drawing.MouseUp(new Point(100,150),MouseButton.Left);
Check(drawn is {Length:4}&&drawn.SequenceEqual(new double[]{50,25,150,75}),"Actual annotation drag preserves original-image box coordinates");
drawn=null;
drawing.MouseWheel(new Point(150,175),new Vector(0,1));
drawing.MouseDown(new Point(150,175),MouseButton.Right);drawing.MouseMove(new Point(170,185));drawing.MouseUp(new Point(170,185),MouseButton.Right);
Check(drawn is null,"Right-button panning never creates an annotation");
drawing.MouseDown(new Point(350,275),MouseButton.Left);drawing.MouseMove(new Point(110,155));drawing.MouseUp(new Point(110,155),MouseButton.Left);
Check(drawn is {Length:4}&&drawn.Zip(new double[]{50,25,150,75}).All(p=>Math.Abs(p.First-p.Second)<.001),"Cursor-centered wheel zoom and right-button pan preserve original-image box coordinates");
using(var nextImage=SopHelper.Presentation.Imaging.BitmapSource.Create(200,100,96,96,SopHelper.Presentation.Imaging.PixelFormats.Bgr24,null,new byte[200*100*3],600))
{
annotation.Source=nextImage;drawn=null;
drawing.MouseDown(new Point(110,155),MouseButton.Left);drawing.MouseUp(new Point(350,275),MouseButton.Left);
Check(drawn is {Length:4}&&drawn.Zip(new double[]{50,25,150,75}).All(p=>Math.Abs(p.First-p.Second)<.001),"Switching same-size frames retains zoom and pan");
annotation.ResetView();drawn=null;
drawing.MouseDown(new Point(100,150),MouseButton.Left);drawing.MouseUp(new Point(300,250),MouseButton.Left);
Check(drawn is {Length:4}&&drawn.SequenceEqual(new double[]{50,25,150,75}),"Fit-to-window resets both zoom and pan");
drawing.MouseDown(new Point(200,200),MouseButton.Right);drawing.MouseMove(new Point(220,210));
annotation.Source=null;drawing.MouseUp(new Point(220,210),MouseButton.Right);annotation.Source=image;drawn=null;
drawing.MouseDown(new Point(100,150),MouseButton.Left);drawing.MouseUp(new Point(300,250),MouseButton.Left);
Check(drawn is {Length:4}&&drawn.SequenceEqual(new double[]{50,25,150,75}),"Clearing the image cancels panning and restores the fitted view");
}
drawing.Close();
}
Check(userCameraBefore is null ? !File.Exists(userCameraPath) : File.Exists(userCameraPath)&&userCameraBefore.SequenceEqual(File.ReadAllBytes(userCameraPath)),"Smoke tests leave the real user's camera preference unchanged");