Browse Source

Fixed a bug in CudaSparsePyrLKOpticalFlow. ( issue #220 )

pull/262/head
Canming Huang 6 years ago
parent
commit
1f5c00d1ea
  1. 6
      Emgu.CV.Cuda/Optflow/CudaSparsePyrLKOpticalFlow.cs
  2. 15
      Emgu.CV.Cuda/Optflow/ICudaSparseOpticalFlow.cs
  3. 18
      Emgu.CV.Test/AutoTestCuda.cs

6
Emgu.CV.Cuda/Optflow/CudaSparsePyrLKOpticalFlow.cs

@ -30,7 +30,7 @@ namespace Emgu.CV.Cuda
public CudaSparsePyrLKOpticalFlow(Size winSize, int maxLevel = 3, int iters = 30, bool useInitialFlow = false)
{
_ptr = CudaInvoke.cudaSparsePyrLKOpticalFlowCreate(
winSize, maxLevel, iters, useInitialFlow,
ref winSize, maxLevel, iters, useInitialFlow,
ref _sparseFlow, ref _algorithm, ref _sharedPtr);
}
@ -69,7 +69,9 @@ namespace Emgu.CV.Cuda
{
[DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal extern static IntPtr cudaSparsePyrLKOpticalFlowCreate(
Size winSize, int maxLevel, int iters,
ref Size winSize,
int maxLevel,
int iters,
[MarshalAs(CvInvoke.BoolMarshalType)]
bool useInitialFlow,
ref IntPtr sparseFlow,

15
Emgu.CV.Cuda/Optflow/ICudaSparseOpticalFlow.cs

@ -35,16 +35,23 @@ namespace Emgu.CV.Cuda
/// <param name="status">Output status vector. Each element of the vector is set to 1 if the flow for the corresponding features has been found. Otherwise, it is set to 0.</param>
/// <param name="err">Optional output vector that contains error response for each point (inverse confidence).</param>
/// <param name="stream">Use a Stream to call the function asynchronously (non-blocking) or null to call the function synchronously (blocking).</param>
public static void Calc(this ICudaSparseOpticalFlow sparseFlow, IInputArray prevImg, IInputArray nextImg, IInputArray prevPts, IInputOutputArray nextPts, IOutputArray status = null, IOutputArray err = null, Stream stream = null)
public static void Calc(this ICudaSparseOpticalFlow sparseFlow, IInputArray prevImg, IInputArray nextImg, IInputArray prevPts, IInputOutputArray nextPts, IOutputArray status, IOutputArray err = null, Stream stream = null)
{
using (InputArray iaPrevImg = prevImg.GetInputArray())
using (InputArray iaNextImg = nextImg.GetInputArray())
using (InputArray iaPrevPts = prevPts.GetInputArray())
using (InputOutputArray ioaNextPts = nextPts.GetInputOutputArray())
using (OutputArray oaStatus = (status == null ? OutputArray.GetEmpty() : status.GetOutputArray()))
using (OutputArray oaStatus = status.GetOutputArray())
using (OutputArray oaErr = (err == null ? OutputArray.GetEmpty() : err.GetOutputArray()))
cudaSparseOpticalFlowCalc(sparseFlow.SparseOpticalFlowPtr, iaPrevImg, iaNextImg, iaPrevPts, ioaNextPts,
oaStatus, oaErr, (stream == null) ? IntPtr.Zero : stream.Ptr);
cudaSparseOpticalFlowCalc(
sparseFlow.SparseOpticalFlowPtr,
iaPrevImg,
iaNextImg,
iaPrevPts,
ioaNextPts,
oaStatus,
oaErr,
(stream == null) ? IntPtr.Zero : stream.Ptr);
}
[DllImport(CvInvoke.ExternCudaLibrary, CallingConvention = CvInvoke.CvCallingConvention)]

18
Emgu.CV.Test/AutoTestCuda.cs

@ -742,14 +742,30 @@ namespace Emgu.CV.Test
Image<Gray, Byte> prevImg, currImg;
AutoTestVarious.OpticalFlowImage(out prevImg, out currImg);
Mat flow = new Mat();
CudaDensePyrLKOpticalFlow opticalflow = new CudaDensePyrLKOpticalFlow(new Size(21, 21), 3, 30, false);
using (CudaDensePyrLKOpticalFlow opticalflow = new CudaDensePyrLKOpticalFlow(new Size(21, 21), 3, 30, false))
using(CudaSparsePyrLKOpticalFlow opticalFlowSparse = new CudaSparsePyrLKOpticalFlow(new Size(21, 21)))
using (CudaImage<Gray, Byte> prevGpu = new CudaImage<Gray, byte>(prevImg))
using (CudaImage<Gray, byte> currGpu = new CudaImage<Gray, byte>(currImg))
using (GpuMat flowGpu = new GpuMat())
using (VectorOfPointF prevPts = new VectorOfPointF())
using (GpuMat currPtrsGpu = new GpuMat())
using (GpuMat statusGpu = new GpuMat())
{
opticalflow.Calc(prevGpu, currGpu, flowGpu);
flowGpu.Download(flow);
int pointsCount = 100;
Size s = prevGpu.Size;
PointF[] points = new PointF[pointsCount];
Random r = new Random(1234);
for (int i = 0; i < points.Length; i++)
{
points[i] = new PointF(r.Next(s.Height), r.Next(s.Width));
}
prevPts.Push(points);
using (GpuMat prevPtsGpu = new GpuMat(prevPts))
opticalFlowSparse.Calc(prevGpu, currGpu, prevPtsGpu, currPtrsGpu, statusGpu);
}
}

Loading…
Cancel
Save