public FrameSet(BlackAndWhiteDoubleArray frame1, BlackAndWhiteDoubleArray frame2, StreamWriter stream, long box) { frameOne = frame1; frameTwo = frame2; fileStream = stream; BoxSize = box; }
// SLOW DO NOT USE #region threeThreadImplementation //rather than generating a new thread for every box, this one generates only 3 threads to be better optimized to run on a 4 thread CPU public StatisticalBox ThreeThreadOpticFlowAsync(BlackAndWhiteDoubleArray _firstFrame, BlackAndWhiteDoubleArray _secondFrame, int _boxSize) { firstFrame = _firstFrame; secondFrame = _secondFrame; boxSize = _boxSize; //determine the size of the buffers on the outside of the frame, and set the size of the output array if (firstFrame.height % boxSize == 0) { topBuffer = boxSize / 2; Height = firstFrame.height / boxSize - 1; } else { topBuffer = (firstFrame.height % boxSize) / 2; Height = firstFrame.height / boxSize; } if (firstFrame.width % boxSize == 0) { leftBuffer = boxSize / 2; Width = firstFrame.width / boxSize - 1; } else { leftBuffer = (firstFrame.width % boxSize) / 2; Width = firstFrame.width / boxSize; } opticflow = new Vector2[Height, Width]; leftover = Height % 3; Parallel.Invoke(() => RunBlock(topBuffer, 0, Height / 3), () => RunBlock(topBuffer + (Height / 3) * boxSize, Height / 3, Height / 3), () => RunBlock(topBuffer + (Height / 3) * 2 * boxSize, (Height / 3) * 2, Height / 3 + leftover)); return(CalculateStatistics()); }
public async void StartAsync() { await Task.Run(() => { double frameToSkip = 1; //the number of frames to skip, setting to 1 never skips a frame double nextFrame = 0; //the next frame to analyze, we always analyze the first frame if (DesiredFramerate < reader.FrameRate) { frameToSkip = (double)reader.FrameRate / DesiredFramerate; } BlackAndWhiteDoubleArray firstFrame; var secondFrame = new BlackAndWhiteDoubleArray(reader.ReadVideoFrame()); //load the first frame outside of the loop as prep output = new StreamWriter(resultsPath); //for some reason ~4-5 frames from the end I was getting out of index errors so I just said f**k it and quit 10 frames early for (CompletedFrames = 1; CompletedFrames < reader.FrameCount - 10; CompletedFrames++) { //if (CompletedFrames % 200 == 0) Console.WriteLine($"OPTS: {CompletedFrames}"); //write progress to console, not required, but nice for debug if ((int)nextFrame <= CompletedFrames) //we do <= to catch any potential double fuckery { nextFrame += frameToSkip; try { firstFrame = secondFrame; secondFrame = new BlackAndWhiteDoubleArray(reader.ReadVideoFrame()); var fs = new FrameSet(firstFrame, secondFrame, output, BoxSize); VideoToBitmap.framesToAnalyse.Add(fs); } catch (NullReferenceException) //writes to the debug log the frame that caused problems but still throws the exception, I may want to catch this elsewhere { Console.WriteLine(CompletedFrames); throw; } if (nextFrame > reader.FrameCount - 10) { break; //if the next frame to analyse is outside of the } } else { reader.ReadVideoFrame().Dispose(); //reading and disposing of frames like this feels inefficient, but is more performant that putting the desired frame as a parameter } } Console.WriteLine("Completed: " + videoPath); }); }
public StatisticalBox CalculateAverageOpticFlow(BlackAndWhiteDoubleArray _firstFrame, BlackAndWhiteDoubleArray _secondFrame, long _boxSize) { //determine the size of the buffers on the outside of the frame and the number of iterations we need to go to firstFrame = _firstFrame; secondFrame = _secondFrame; boxSize = _boxSize; if (firstFrame.height % boxSize == 0) { topBuffer = boxSize / 2; Height = firstFrame.height / boxSize - 1; } else { topBuffer = (firstFrame.height % boxSize) / 2; Height = firstFrame.height / boxSize; } if (firstFrame.width % boxSize == 0) { leftBuffer = boxSize / 2; Width = firstFrame.width / boxSize - 1; } else { leftBuffer = (firstFrame.width % boxSize) / 2; Width = firstFrame.width / boxSize; } List <Tuple <int, int> > arrs = new List <Tuple <int, int> >(); for (int h = 0; h < Height; h++) { for (int w = 0; w < Width; w++) { arrs.Add(new Tuple <int, int>(h, w)); } } ConcurrentBag <Vector2> opticFlow = new ConcurrentBag <Vector2>(); OrderablePartitioner <Tuple <int, int> > op = Partitioner.Create(arrs, true); Parallel.ForEach(op, x => opticFlow.Add(findOpticVector(x.Item1 * boxSize + topBuffer, x.Item2 * boxSize + leftBuffer))); return(new StatisticalBox(opticFlow.ToArray())); }
public StatisticalBox CalculateAverageOpticFlow(BlackAndWhiteDoubleArray _firstFrame, BlackAndWhiteDoubleArray _secondFrame, int _boxSize) { //determine the size of the buffers on the outside of the frame and the number of iterations we need to go to firstFrame = _firstFrame; secondFrame = _secondFrame; boxSize = _boxSize; if (firstFrame.height % boxSize == 0) { topBuffer = boxSize / 2; Height = firstFrame.height / boxSize - 1; } else { topBuffer = (firstFrame.height % boxSize) / 2; Height = firstFrame.height / boxSize; } if (firstFrame.width % boxSize == 0) { leftBuffer = boxSize / 2; Width = firstFrame.width / boxSize - 1; } else { leftBuffer = (firstFrame.width % boxSize) / 2; Width = firstFrame.width / boxSize; } opticflow = new Vector2[Height, Width]; Parallel.For(0, Height, yit => Parallel.For(0, Width, xit => opticflow[yit, xit] = findOpticVector(yit * boxSize + topBuffer, xit * boxSize + leftBuffer) ) ); return(CalculateStatistics()); }