private void timer1_Tick(object sender, EventArgs e) { // Quit running conversions if no more filters if (curFilter >= filters.Count) { timer1.Enabled = false; return; } string filterName, commonName, outputPath; GetFilterName(curFilter, out filterName, out commonName, out outputPath); double averageFrameTime; // Create compressed AVI file. using (AviOutputSession outputSession = new AviOutputSession(outputPath, 30, filterName)) { // Copy the frames from original AVI file to the compressed file. CopyAvi(inputSession, outputSession, out averageFrameTime); } // Calculate results of compression and write them to the table long fileSize = new System.IO.FileInfo(outputPath).Length; double quality; using (AviInputSession writtenSession = new AviInputSession(outputPath)) { quality = CompareAvi(inputSession, writtenSession); } AddItem(commonName, (double)fileSize / 1024, averageFrameTime, quality); progressBar1.Value = progressBar1.Value + 1; curFilter++; Refresh(); }
// Sample Data creates a time stamp, waveform and image. The time stamp // and waveform data are bundled together and flattened to a string. // This data is saved with the image in an AVI so when the AVI image is // read out later, the same data it was written with can also be // retrieved. private void SampleData(AviInputSession sourceSession, AviOutputSession destSession, VisionImage frame, uint frameIndex) { // Get and display time string timestamp = DateTime.Now.ToString(); form2.CurTime.Text = timestamp; // Read the frame sourceSession.ReadFrame(frame, frameIndex); // Get and display sample waveforms double[] waves = new double[3]; waves[0] = 5 * Math.Sin(sinPhase); waves[1] = 4 * Math.Cos(cosPhase); waves[2] = (new Random()).NextDouble() * 6 - 3; sinPhase += .1; cosPhase += .15; form2.Graph.AppendData(waves[0], waves[1], waves[2]); AviData data = new AviData(timestamp, waves[0], waves[1], waves[2]); // Flatten data to buffer byte[] bytes = FlattenData(data); destSession.WriteFrame(frame, bytes); }
private void LoadAVI() { // Load AVI file from dialog. openFileDialog1.InitialDirectory = System.IO.Path.Combine(ExampleImagesFolder.GetExampleImagesFolder(), @"AVIs"); openFileDialog1.Filter = "AVI files (*.avi)|*.avi||"; openFileDialog1.FileName = ""; openFileDialog1.ShowDialog(); if (openFileDialog1.FileName != "") { session = new AviInputSession(openFileDialog1.FileName); } }
private void LoadAvi() { openFileDialog1.InitialDirectory = System.IO.Path.Combine(ExampleImagesFolder.GetExampleImagesFolder(), @"AVIs"); openFileDialog1.FileName = "CompressorComparison.avi"; openFileDialog1.Filter = "AVI files (*.avi)|*.avi||"; openFileDialog1.ShowDialog(); if (openFileDialog1.FileName != "") { inputSession = new AviInputSession(openFileDialog1.FileName); } // Create the temp directory. try { System.IO.Directory.CreateDirectory(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "AVI Compressor Comparison Example")); } catch (Exception) { } }
// CompareAvi compares each frame of two images to calculate the average // difference. This is used to compare quality. private double CompareAvi(AviInputSession original, AviInputSession other) { double totalDiff = 0; using (VisionImage firstImage = new VisionImage(), secondImage = new VisionImage()) { // Setup the images to store the frames. firstImage.Type = ImageType.Rgb32; secondImage.Type = ImageType.Rgb32; // Calculate the difference between frames. for (uint curFrame = 0; curFrame < original.Frames; ++curFrame) { // Read the frames. original.ReadFrame(firstImage, curFrame); other.ReadFrame(secondImage, curFrame); // Resize first image to be same as second if needed. if (firstImage.Width != secondImage.Width || firstImage.Height != secondImage.Height) { Algorithms.Resample(firstImage, firstImage, secondImage.Width, secondImage.Height, InterpolationMethod.Bilinear); } // Calculate the difference between the frames. Algorithms.AbsoluteDifference(firstImage, secondImage, firstImage); ColorHistogramReport colorReport = Algorithms.ColorHistogram(firstImage, 256, ColorMode.Hsl); totalDiff += colorReport.Plane3.Mean; } } // Calculate quality from difference totalDiff = totalDiff / (double)original.Frames; double quality = 1000 - (totalDiff * 10); if (quality < 0) { quality = 0; } return(quality); }
private void timer1_Tick(object sender, EventArgs e) { // Done writing? if (curFrame >= numFrames || mode != RunMode.WritingFrames) { // Close AVIs sourceSession.Dispose(); sourceSession = null; destSession.Dispose(); destSession = null; // Setup for reading AVI frames mode = RunMode.ReadingFrames; startButton.Enabled = true; stopButton.Enabled = false; sourceSession = new AviInputSession(Path.Text); form2.Graph.SetupForReading((int)sourceSession.Frames); form2.FrameSlider.Enabled = true; form2.FrameSlider.SetRangeFromAvi(sourceSession); form2.FrameNum.Enabled = true; form2.FrameNum.Maximum = (int)sourceSession.Frames - 1; // Update display to reflect change in state statusLabel.Text = "Reading AVI File and Data"; this.Refresh(); form2.Graph.ClearGraph(); ReadAllData(); ReadFrame(0); form2.Graph.DoneReading(); timer1.Enabled = false; return; } // Write the frame WriteFrame(curFrame); curFrame++; }
private void CopyAvi(AviInputSession source, AviOutputSession dest, out double averageFrameTime) { int totalTime = 0; // Grab each frame. VisionImage[] frames = new VisionImage[source.Frames]; for (uint i = 0; i < source.Frames; ++i) { frames[i] = new VisionImage(ImageType.Rgb32); source.ReadFrame(frames[i], i); } // Convert each frame. int startTime = System.Environment.TickCount; for (int i = 0; i < source.Frames; ++i) { // Write the frame. dest.WriteFrame(frames[i]); } totalTime = System.Environment.TickCount - startTime; averageFrameTime = ((double)totalTime) / source.Frames; }
private void startButton_Click(object sender, EventArgs e) { // Close AVI if it is open if (mode != RunMode.NotRunning) { form2.Graph.ClearGraph(); form2.FrameNum.Enabled = false; form2.FrameSlider.Enabled = false; if (sourceSession != null) { sourceSession.Dispose(); sourceSession = null; } } // Load base AVI sourceSession = new AviInputSession(System.IO.Path.Combine(ExampleImagesFolder.GetExampleImagesFolder(), @"AVIs\Flame.avi")); numFrames = sourceSession.Frames; // Create output AVI if (Path.Text == "") { Browse(); } destSession = new AviOutputSession(Path.Text, sourceSession.FramesPerSecond, null, -1, true, 10000); // Start writing statusLabel.Text = "Writing AVI File and Data"; curFrame = 0; mode = RunMode.WritingFrames; frame.Type = ImageType.Rgb32; imageViewer1.Attach(frame); timer1.Enabled = true; startButton.Enabled = false; stopButton.Enabled = true; }