/// <summary>Finds ROI in a video.</summary> /// <param name="mask_finder_path">Path to find_ruler model file.</param> /// <param name="vid_path">Path to the video.</param> /// <param name="roi">Rect specifying the ROI.</param> /// <param name="endpoints">Ruler endpoints. </param> static void FindVideoRoi( string mask_finder_path, string vid_path, out Rect roi, out PointPair endpoints) { // Determined by experimentation with GPU having 8GB memory. const int kMaxImg = 8; // Create and initialize the mask finder. RulerMaskFinder mask_finder = new RulerMaskFinder(); ErrorCode status = mask_finder.Init(mask_finder_path); if (status != ErrorCode.kSuccess) { throw new Exception("Failed to initialize mask finder!"); } // Decode the first 100 frames and find the mask that corresponds // to the largest ruler area. VideoReader reader = new VideoReader(); status = reader.Init(vid_path); if (status != ErrorCode.kSuccess) { throw new Exception("Failed to open video!"); } VectorImage masks = new VectorImage(); Image best_mask = new Image(); double max_mask_sum = 0.0; bool vid_end = false; for (int i = 0; i < 100 / kMaxImg; ++i) { for (int j = 0; j < kMaxImg; ++j) { Image img = new Image(); status = reader.GetFrame(img); if (status != ErrorCode.kSuccess) { vid_end = true; break; } status = mask_finder.AddImage(img); if (status != ErrorCode.kSuccess) { throw new Exception("Failed to add frame to mask finder!"); } } status = mask_finder.Process(masks); if (status != ErrorCode.kSuccess) { throw new Exception("Failed to process mask finder!"); } foreach (var mask in masks) { double mask_sum = mask.Sum()[0]; if (mask_sum > max_mask_sum) { max_mask_sum = mask_sum; best_mask = mask; } } if (vid_end) { break; } } // Now that we have the best mask, use this to compute ROI. best_mask.Resize(reader.Width(), reader.Height()); endpoints = openem.RulerEndpoints(best_mask); Image r_mask = openem.Rectify(best_mask, endpoints); roi = openem.FindRoi(r_mask); }
/// <summary> /// Main program. /// </summary> static int Main(string[] args) { // Check input arguments. if (args.Length < 2) { Console.WriteLine("Expected at least two arguments:"); Console.WriteLine(" Path to protobuf file containing model"); Console.WriteLine(" Paths to one or more image files"); return(-1); } // Create and initialize the mask finder. RulerMaskFinder mask_finder = new RulerMaskFinder(); ErrorCode status = mask_finder.Init(args[0]); if (status != ErrorCode.kSuccess) { Console.WriteLine("Failed to initialize ruler mask finder!"); return(-1); } // Load in images. VectorImage imgs = new VectorImage(); for (int i = 1; i < args.Length; i++) { Image img = new Image(); status = img.FromFile(args[i]); if (status != ErrorCode.kSuccess) { Console.WriteLine("Failed to load image {0}!", args[i]); return(-1); } imgs.Add(img); } // Add images to processing queue. foreach (var img in imgs) { status = mask_finder.AddImage(img); if (status != ErrorCode.kSuccess) { Console.WriteLine("Failed to add image for processing!"); return(-1); } } // Process the loaded images. VectorImage masks = new VectorImage(); status = mask_finder.Process(masks); if (status != ErrorCode.kSuccess) { Console.WriteLine("Failed to process images!"); return(-1); } for (int i = 0; i < masks.Count; ++i) { // Resize the masks back into the same size as the images. masks[i].Resize(imgs[i].Width(), imgs[i].Height()); // Check if the ruler is present. bool present = openem.RulerPresent(masks[i]); if (!present) { Console.WriteLine("Could not find ruler in image! Skipping..."); continue; } // Find orientation and region of interest based on the mask. PointPair endpoints = new PointPair(); endpoints = openem.RulerEndpoints(masks[i]); Image r_mask = openem.Rectify(masks[i], endpoints); Rect roi = openem.FindRoi(r_mask); // Rectify, crop, and display the image. Image r_img = openem.Rectify(imgs[i], endpoints); Image c_img = openem.Crop(r_img, roi); c_img.Show(); } return(0); }