Commit 8a28afd4 by liulongfei

添加滤波算法

parent f6ec046c
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Common
{
/// <summary>
/// 视频旋转模式
/// </summary>
public enum VideoRenderRotationEnum
{
/// <summary>
/// 不旋转
/// </summary>
None,
/// <summary>
/// 顺时针
/// </summary>
Clockwise,
/// <summary>
/// 逆时针
/// </summary>
Anticlockwise
}
}
...@@ -9,6 +9,7 @@ using System.Threading.Tasks; ...@@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Threading; using System.Threading;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using OpenCvSharp;
namespace VIZ.Framework.Common namespace VIZ.Framework.Common
{ {
...@@ -94,14 +95,42 @@ namespace VIZ.Framework.Common ...@@ -94,14 +95,42 @@ namespace VIZ.Framework.Common
} }
NDIStreamVideoFrame videoFrame = new NDIStreamVideoFrame(); NDIStreamVideoFrame videoFrame = new NDIStreamVideoFrame();
videoFrame.Width = frame.xres;
videoFrame.Height = frame.yres;
videoFrame.Length = frame.xres * frame.yres * 4; videoFrame.Length = frame.xres * frame.yres * 4;
videoFrame.TimeStamp = frame.timestamp; videoFrame.TimeStamp = frame.timestamp;
videoFrame.DataStream = new SharpDX.DataStream(videoFrame.Length, true, true); videoFrame.DataStream = new SharpDX.DataStream(videoFrame.Length, true, true);
unsafe unsafe
{ {
Buffer.MemoryCopy(frame.p_data.ToPointer(), videoFrame.DataStream.DataPointer.ToPointer(), videoFrame.Length, videoFrame.Length); if (this.Stream.Option.Rotation.HasValue)
{
if (this.Stream.Option.Rotation == RotateFlags.Rotate180)
{
videoFrame.Width = frame.xres;
videoFrame.Height = frame.yres;
}
else
{
videoFrame.Width = frame.yres;
videoFrame.Height = frame.xres;
}
Mat srcMat = new Mat(new OpenCvSharp.Size(frame.xres, frame.yres), MatType.CV_8UC4);
Buffer.MemoryCopy(frame.p_data.ToPointer(), srcMat.DataPointer, videoFrame.Length, videoFrame.Length);
Mat dstMat = new Mat();
Cv2.Rotate(srcMat, dstMat, this.Stream.Option.Rotation.Value);
Buffer.MemoryCopy(dstMat.DataPointer, videoFrame.DataStream.DataPointer.ToPointer(), videoFrame.Length, videoFrame.Length);
dstMat.Dispose();
srcMat.Dispose();
}
else
{
videoFrame.Width = frame.xres;
videoFrame.Height = frame.yres;
Buffer.MemoryCopy(frame.p_data.ToPointer(), videoFrame.DataStream.DataPointer.ToPointer(), videoFrame.Length, videoFrame.Length);
}
} }
this.Stream.VideoFrameQueue.Enqueue(videoFrame); this.Stream.VideoFrameQueue.Enqueue(videoFrame);
......
using System; using OpenCvSharp;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
...@@ -15,5 +16,10 @@ namespace VIZ.Framework.Common ...@@ -15,5 +16,10 @@ namespace VIZ.Framework.Common
/// 延时帧 /// 延时帧
/// </summary> /// </summary>
public int DelayFrame { get; set; } public int DelayFrame { get; set; }
/// <summary>
/// 旋转
/// </summary>
public RotateFlags? Rotation { get; set; }
} }
} }
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Altaxo.Calc.Regression;
namespace VIZ.Framework.Core
{
/// <summary>
/// Savitzky Golay 算法平滑 返回值
/// </summary>
public class Navigation3DSmoothWithSavitzkyGolayResult
{
/// <summary>
/// Savitzky Golay 算法平滑 返回值
/// </summary>
/// <param name="timeCode">时码</param>
/// <param name="value">值</param>
public Navigation3DSmoothWithSavitzkyGolayResult(long timeCode, double value)
{
this.TimeCode = timeCode;
this.Value = value;
}
/// <summary>
/// 时码
/// </summary>
public long TimeCode { get; private set; }
/// <summary>
/// 值
/// </summary>
public double Value { get; private set; }
}
/// <summary>
/// Savitzky Golay 算法平滑
/// </summary>
public class Navigation3DSmoothWithSavitzkyGolay
{
/// <summary>
/// 锁对象
/// </summary>
private object lock_object = new object();
/// <summary>
/// 时码队列
/// </summary>
public Queue<long> TimeCodeQueue { get; } = new Queue<long>();
/// <summary>
/// 值队列
/// </summary>
public Queue<double> ValueQueue { get; } = new Queue<double>();
/// <summary>
/// 队列长度
/// </summary>
public int QueueLength { get; private set; }
/// <summary>
/// 过滤器
/// </summary>
private SavitzkyGolay Filter;
/// <summary>
/// Savitzky Golay 算法平滑
/// </summary>
/// <param name="windowWidth">窗口宽度</param>
/// <param name="polynomialDegree">幂</param>
/// <param name="queueLength">队列长度</param>
public Navigation3DSmoothWithSavitzkyGolay(int windowWidth, int polynomialDegree, int queueLength)
{
this.Filter = new SavitzkyGolay(windowWidth, 0, polynomialDegree);
this.QueueLength = queueLength;
}
/// <summary>
/// 平滑
/// </summary>
/// <param name="timeCode">时码</param>
/// <param name="value">输入值</param>
/// <returns>平滑后的值</returns>
public Navigation3DSmoothWithSavitzkyGolayResult Smooth(long timeCode, double value)
{
lock (this.lock_object)
{
this.TimeCodeQueue.Enqueue(timeCode);
this.ValueQueue.Enqueue(value);
if (this.TimeCodeQueue.Count < this.QueueLength)
{
return new Navigation3DSmoothWithSavitzkyGolayResult(timeCode, value);
}
if (this.TimeCodeQueue.Count > this.QueueLength)
{
this.TimeCodeQueue.Dequeue();
this.ValueQueue.Dequeue();
}
}
double[] dst = new double[this.ValueQueue.Count];
this.Filter.Apply(this.ValueQueue.ToArray(), dst);
double dst_value = dst[this.QueueLength / 2];
long dst_timeCode = this.TimeCodeQueue.ToArray()[this.QueueLength / 2];
return new Navigation3DSmoothWithSavitzkyGolayResult(dst_timeCode, dst_value);
}
/// <summary>
/// 更新参数
/// </summary>
/// <param name="windowWidth">窗口宽度</param>
/// <param name="polynomialDegree">幂</param>
/// <param name="queueLength">队列长度</param>
public void UpdateOption(int windowWidth, int polynomialDegree, int queueLength)
{
lock (this.lock_object)
{
this.Filter = new SavitzkyGolay(windowWidth, 0, polynomialDegree);
this.QueueLength = queueLength;
this.TimeCodeQueue.Clear();
this.ValueQueue.Clear();
}
}
}
}
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
...@@ -49,9 +51,15 @@ ...@@ -49,9 +51,15 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="AltaxoCore">
<HintPath>..\05-Lib\AltaxoCore.dll</HintPath>
</Reference>
<Reference Include="log4net, Version=2.0.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL"> <Reference Include="log4net, Version=2.0.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.14\lib\net45\log4net.dll</HintPath> <HintPath>..\packages\log4net.2.0.14\lib\net45\log4net.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.Solver.Foundation, Version=3.0.2.10889, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\VIZ.H2V\packages\Microsoft.Solver.Foundation.3.1.0\lib\Microsoft.Solver.Foundation.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualBasic" /> <Reference Include="Microsoft.VisualBasic" />
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
...@@ -190,6 +198,7 @@ ...@@ -190,6 +198,7 @@
<Compile Include="Expand\ThreeDMouse\Core\Navigation3DHelper.cs" /> <Compile Include="Expand\ThreeDMouse\Core\Navigation3DHelper.cs" />
<Compile Include="Expand\ThreeDMouse\Navigation3DDataInfo.cs" /> <Compile Include="Expand\ThreeDMouse\Navigation3DDataInfo.cs" />
<Compile Include="Expand\ThreeDMouse\Navigation3DMapping.cs" /> <Compile Include="Expand\ThreeDMouse\Navigation3DMapping.cs" />
<Compile Include="Expand\ThreeDMouse\Navigation3DSmoothWithSavitzkyGolay.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Expand\SharpDx\RawRectangleFExpand.cs" /> <Compile Include="Expand\SharpDx\RawRectangleFExpand.cs" />
<Compile Include="Expand\SharpDx\SharpDxColorHelper.cs" /> <Compile Include="Expand\SharpDx\SharpDxColorHelper.cs" />
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="log4net" version="2.0.14" targetFramework="net48" /> <package id="log4net" version="2.0.14" targetFramework="net48" />
<package id="Microsoft.Solver.Foundation" version="3.1.0" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" /> <package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
<package id="NvAPIWrapper.Net" version="0.8.1.101" targetFramework="net48" /> <package id="NvAPIWrapper.Net" version="0.8.1.101" targetFramework="net48" />
<package id="SharpDX" version="4.2.0" targetFramework="net48" /> <package id="SharpDX" version="4.2.0" targetFramework="net48" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment