Skip to content

Commit

Permalink
NEW: Option to set orientation of right-handed coordinate system to a…
Browse files Browse the repository at this point in the history
…rbitrary angle.

BUGFIX: Prevent crash if video properties could not be called with DirectShow.
BUGFIX: Correct position in video, if double click on data point row in data window.
  • Loading branch information
avosskuehler committed Nov 14, 2022
1 parent 0e61090 commit 56abd4e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 45 deletions.
33 changes: 31 additions & 2 deletions VianaNET/Data/VideoData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public void UpdatePoint(int frameIndex, int objectIndex, Point newLocation)
//Point origin = App.Project.CalibrationData.OriginInPixel;
Point transformedPoint = newLocation;
//transformedPoint.Offset(-origin.X, -origin.Y);
transformedPoint = App.Project.CalibrationData.CoordinateTransform.Transform(transformedPoint);
//transformedPoint = App.Project.CalibrationData.CoordinateTransform.Transform(transformedPoint);
sample.Object[objectIndex].PixelX = transformedPoint.X;
sample.Object[objectIndex].PixelY = transformedPoint.Y;
}
Expand Down Expand Up @@ -520,20 +520,49 @@ protected virtual void OnPropertyChanged(string propertyName)
/// </returns>
private static Point CalibrateSample(DataSample value)
{
if (!App.Project.CalibrationData.IsVideoCalibrated)
if (!App.Project.CalibrationData.IsVideoCalibrated && App.Project.CalibrationData.CoordinateTransform.IsIdentity)
{
return new Point(value.PixelX, value.PixelY);
}

Point origin = App.Project.CalibrationData.OriginInPixel;
Point calibratedPoint = new Point(value.PixelX - origin.X, value.PixelY - origin.Y);

calibratedPoint = App.Project.CalibrationData.CoordinateTransform.Transform(calibratedPoint);

calibratedPoint.X = calibratedPoint.X * App.Project.CalibrationData.ScalePixelToUnit;
calibratedPoint.Y = calibratedPoint.Y * App.Project.CalibrationData.ScalePixelToUnit;

return calibratedPoint;
}

///// <summary>
///// The calibrate sample.
///// </summary>
///// <param name="value">
///// The value.
///// </param>
///// <returns>
///// The <see cref="Point"/> .
///// </returns>
//private static DataSample RevertSampleCalibration(Point value)
//{
// if (!App.Project.CalibrationData.IsVideoCalibrated && App.Project.CalibrationData.CoordinateTransform.IsIdentity)
// {
// return new Point(value.PixelX, value.PixelY);
// }

// Point origin = App.Project.CalibrationData.OriginInPixel;
// Point calibratedPoint = new Point(value.PixelX - origin.X, value.PixelY - origin.Y);

// calibratedPoint = App.Project.CalibrationData.CoordinateTransform.Transform(calibratedPoint);

// calibratedPoint.X = calibratedPoint.X * App.Project.CalibrationData.ScalePixelToUnit;
// calibratedPoint.Y = calibratedPoint.Y * App.Project.CalibrationData.ScalePixelToUnit;

// return calibratedPoint;
//}

/// <summary>
/// Returns the factor to convert sample with milliseconds in the current time unit;
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions VianaNET/MainWindow/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@
ToolTipTitle="{lex:Loc VianaNET:Labels:ButtonRotateVideoToolTipTitle}"
ToolTipDescription="{lex:Loc VianaNET:Labels:ButtonRotateVideoToolTipDescription}"
IsEnabled="{Binding Path=HasVideo, Source={x:Static control:Video.Instance}}"/>
<!--<RibbonButton Click="CoordinateSystemButtonClick"
<RibbonButton Click="CoordinateSystemButtonClick"
Label="{lex:Loc VianaNET:Labels:ButtonCoordinateSystemLabelTitle}"
LargeImageSource="/VianaNET;component/Images/CoordinateSystem32.png"
SmallImageSource="/VianaNET;component/Images/CoordinateSystem16.png"
ToolTipTitle="{lex:Loc VianaNET:Labels:ButtonCoordinateSystemToolTipTitle}"
ToolTipDescription="{lex:Loc VianaNET:Labels:ButtonCoordinateSystemToolTipDescription}"
IsEnabled="{Binding Path=HasVideo, Source={x:Static control:Video.Instance}}"/>-->
IsEnabled="{Binding Path=HasVideo, Source={x:Static control:Video.Instance}}"/>
<RibbonButton Name="SelectNumberOfObjectsButton"
Click="SelectNumberOfObjectsButtonClick"
Label="{lex:Loc VianaNET:Labels:ButtonSelectNumberOfObjectsLabelTitle}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,14 @@ private void PlayerMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
double scaledX = e.GetPosition(this.VideoImage).X;
double scaledY = e.GetPosition(this.VideoImage).Y;
double factorX = Video.Instance.VideoElement.NaturalVideoWidth / this.VideoImage.ActualWidth;
double factorY = Video.Instance.VideoElement.NaturalVideoHeight / this.VideoImage.ActualHeight;
double spaceX = 0; // (this.videoSurfaceClone.ActualWidth - this.videoSurfaceClone.UniformWidth) / 2;
double spaceY = 0; // (this.videoSurfaceClone.ActualHeight - this.videoSurfaceClone.UniformHeight) / 2;
double originalX = factorX * (scaledX - spaceX);
double originalY = factorY * (scaledY - spaceY);

App.Project.VideoData.AddPoint(this.IndexOfTrackedObject - 1, new Point(originalX, Video.Instance.VideoElement.NaturalVideoHeight - originalY));
double posX = e.GetPosition(this.VideoImage).X;
double posY = e.GetPosition(this.VideoImage).Y;
double scaleX = Video.Instance.VideoElement.NaturalVideoWidth / this.VideoImage.ActualWidth;
double scaleY = Video.Instance.VideoElement.NaturalVideoHeight / this.VideoImage.ActualHeight;
double pixelX = scaleX * posX;
double pixelY = scaleY * posY;

App.Project.VideoData.AddPoint(this.IndexOfTrackedObject - 1, new Point(pixelX, Video.Instance.VideoElement.NaturalVideoHeight - pixelY));

if (this.IndexOfTrackedObject == App.Project.ProcessingData.NumberOfTrackedObjects)
{
Expand Down
33 changes: 16 additions & 17 deletions VianaNET/Modules/DataAcquisition/ModifyDataWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public int IndexOfTrackedObject
/// <param name="frameIndex">Index of the frame.</param>
public void MoveToFrame(int frameIndex)
{
var newMediaPosition = Video.Instance.VideoPlayerElement.FrameTimeInMS * frameIndex;
var newMediaPosition = Video.Instance.VideoPlayerElement.FrameTimeInMS * (frameIndex - 1);
Video.Instance.VideoPlayerElement.MediaPositionInMS = newMediaPosition;
this.TimelineSlider.Value = newMediaPosition;
Video.Instance.VideoPlayerElement.UpdateFrameIndex();
Expand Down Expand Up @@ -317,14 +317,14 @@ private void PlayerMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
double scaledX = e.GetPosition(this.VideoImage).X;
double scaledY = e.GetPosition(this.VideoImage).Y;
var factorX = Video.Instance.VideoElement.NaturalVideoWidth / this.VideoImage.ActualWidth;
var factorY = Video.Instance.VideoElement.NaturalVideoHeight / this.VideoImage.ActualHeight;
var originalX = factorX * scaledX;
var originalY = factorY * scaledY;
double posX = e.GetPosition(this.VideoImage).X;
double posY = e.GetPosition(this.VideoImage).Y;
double scaleX = Video.Instance.VideoElement.NaturalVideoWidth / this.VideoImage.ActualWidth;
double scaleY = Video.Instance.VideoElement.NaturalVideoHeight / this.VideoImage.ActualHeight;
double pixelX = scaleX * posX;
double pixelY = scaleY * posY;

App.Project.VideoData.AddPoint(this.IndexOfTrackedObject - 1, new Point(originalX, Video.Instance.VideoElement.NaturalVideoHeight - originalY));
App.Project.VideoData.AddPoint(this.IndexOfTrackedObject - 1, new Point(pixelX, Video.Instance.VideoElement.NaturalVideoHeight - pixelY));

if (this.IndexOfTrackedObject == App.Project.ProcessingData.NumberOfTrackedObjects)
{
Expand Down Expand Up @@ -404,18 +404,17 @@ private void CursorEllipse_OnMouseLeftButtonUp(object sender, MouseButtonEventAr
this.SetVisibilityOfSharpCursor(false);
this.Cursor = Cursors.Arrow;

double scaledX = e.GetPosition(this.VideoImage).X;
double scaledY = e.GetPosition(this.VideoImage).Y;
double factorX = Video.Instance.VideoElement.NaturalVideoWidth / this.VideoImage.ActualWidth;
double factorY = Video.Instance.VideoElement.NaturalVideoHeight / this.VideoImage.ActualHeight;
double originalX = factorX * scaledX;
double originalY = factorY * scaledY;
double posX = e.GetPosition(this.VideoImage).X;
double posY = e.GetPosition(this.VideoImage).Y;
double scaleX = Video.Instance.VideoElement.NaturalVideoWidth / this.VideoImage.ActualWidth;
double scaleY = Video.Instance.VideoElement.NaturalVideoHeight / this.VideoImage.ActualHeight;
double pixelX = scaleX * posX;
double pixelY = scaleY * posY;

App.Project.VideoData.UpdatePoint(
Video.Instance.FrameIndex,
this.IndexOfTrackedObject - 1,
new Point(originalX, Video.Instance.VideoElement.NaturalVideoHeight - originalY));

new Point(pixelX, Video.Instance.VideoElement.NaturalVideoHeight - pixelY));
}
}

Expand Down Expand Up @@ -524,7 +523,7 @@ private void UpdateDataPointLocation()
if (sample.Object != null && sample.Object[i - 1] != null)
{
Point location = new Point(sample.Object[i - 1].PixelX, Video.Instance.VideoElement.NaturalVideoHeight - sample.Object[i - 1].PixelY);
location = App.Project.CalibrationData.CoordinateTransform.Transform(location);
//location = App.Project.CalibrationData.CoordinateTransform.Transform(location);
//Point origin = App.Project.CalibrationData.OriginInPixel;
//location.Offset(origin.X, origin.Y);
var scaledX = location.X / factorX;
Expand Down
9 changes: 8 additions & 1 deletion VianaNET/Modules/Video/Control/VideoCapturer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ public void ShowPropertyPageOfVideoDevice()
//{
// DShowUtils.DisplayPropertyPage(IntPtr.Zero, this.VideoDeviceIndex);
//}
this.OpenCVObject.Set(VideoCaptureProperties.Settings, 1);
try
{
this.OpenCVObject.Set(VideoCaptureProperties.Settings, 1);
}
catch (System.Exception)
{
InformationDialog.Show("Fehler", "Eigenschaftsseite kann nicht angezeigt werden", false);
}
}


Expand Down
30 changes: 17 additions & 13 deletions VianaNET/Modules/Video/Dialogs/CoordinateSystemWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace VianaNET.Modules.Video.Dialogs
{
using SharpDX.Direct3D9;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand Down Expand Up @@ -148,14 +149,17 @@ protected override void ContainerMouseLeftButtonDown(object sender, MouseButtonE
{
this.DialogResult = true;
Vector vectorDefault = new Vector(1, 0);
Vector vectorXAxis = new Vector(this.directionX.X2 - this.directionX.X1, this.directionX.Y2 - this.directionX.Y1);
Vector vectorYAxis = new Vector(this.directionY.X2 - this.directionY.X1, this.directionY.Y2 - this.directionY.Y1);
Vector vectorXAxis = new Vector(this.directionX.X2 - this.directionX.X1, this.directionX.Y1 - this.directionX.Y2);
Vector vectorYAxis = new Vector(this.directionY.X2 - this.directionY.X1, this.directionY.Y1 - this.directionY.Y2);
double angle = Vector.AngleBetween(vectorXAxis, vectorYAxis);
int scaleY = angle < 0 ? 1 : -1;

// angle = 90 is CCW right handed
// angle = -90 is CW left hand
int scaleY = angle < 0 ? -1 : 1;
Matrix matrix = new Matrix();
double angleX = Vector.AngleBetween(vectorDefault, vectorXAxis);
matrix.Scale(1, scaleY);
matrix.Rotate(angleX);
matrix.Rotate(-angleX);

App.Project.CalibrationData.CoordinateTransform = matrix;
this.Close();
Expand Down Expand Up @@ -236,7 +240,7 @@ private void CoordinateSystemWindowLoaded(object sender, RoutedEventArgs e)
if (this.GetScales(out double scaleX, out double scaleY))
{
double originXInScreenPixel = App.Project.CalibrationData.OriginInPixel.X * scaleX;
double originYInScreenPixel = App.Project.CalibrationData.OriginInPixel.Y * scaleY;
double originYInScreenPixel = (Video.Control.Video.Instance.VideoElement.NaturalVideoHeight - App.Project.CalibrationData.OriginInPixel.Y) * scaleY;
Canvas.SetLeft(this.originPath, originXInScreenPixel - this.originPath.ActualWidth / 2);
Canvas.SetTop(this.originPath, originYInScreenPixel - this.originPath.ActualHeight / 2);
this.directionX.X1 = originXInScreenPixel;
Expand Down Expand Up @@ -312,20 +316,20 @@ private void SetYAxis(Point mouseLocation)
if (this.GetScales(out double scaleX, out double scaleY))
{
double originXInScreenPixel = App.Project.CalibrationData.OriginInPixel.X * scaleX;
double originYInScreenPixel = App.Project.CalibrationData.OriginInPixel.Y * scaleY;
double originYInScreenPixel = (Video.Control.Video.Instance.VideoElement.NaturalVideoHeight - App.Project.CalibrationData.OriginInPixel.Y) * scaleY;

Vector vectorXAxis = new Vector(this.directionX.X2 - this.directionX.X1, this.directionX.Y2 - this.directionX.Y1);
Vector vectorMouse = new Vector(mouseLocation.X - originXInScreenPixel, mouseLocation.Y - originYInScreenPixel);
double angleMouseXAxis = Vector.AngleBetween(vectorXAxis, vectorMouse);
Vector vectorY;
if (angleMouseXAxis >= 0 && angleMouseXAxis <= 180)
{
vectorY = new Vector(-vectorXAxis.Y, vectorXAxis.X);
}
else
{
//if (angleMouseXAxis >= 0 && angleMouseXAxis <= 180)
//{
// vectorY = new Vector(-vectorXAxis.Y, vectorXAxis.X);
//}
//else
//{
vectorY = new Vector(vectorXAxis.Y, -vectorXAxis.X);
}
//}

this.directionY.X2 = originXInScreenPixel + vectorY.X;
this.directionY.Y2 = originYInScreenPixel + vectorY.Y;
Expand Down

0 comments on commit 56abd4e

Please sign in to comment.