/// <summary> /// Extracts annotated samples - their bounding boxes. /// </summary> /// <param name="labelMatch">Regular expression for valid label.</param> private void extractPositives(Regex labelMatch) { if (database == null || capture == null || imageKeyFunc == null) { return; //form not initialized } var currPos = capture.Position; for (int i = 0; i < capture.Length; i++) { var imgKey = imageKeyFunc(i); if (database.ContainsKey(imgKey)) { capture.Seek(i, SeekOrigin.Begin); var imgName = (capture as ImageDirectoryReader).CurrentImageName; var img = capture.Read(); var labelCounter = new Dictionary <string, int>(); foreach (var annotation in database[imgKey]) { if (labelMatch.IsMatch(annotation.Label) == false) { continue; } //**************** get current label instance index ***************/ if (labelCounter.ContainsKey(annotation.Label) == false) { labelCounter.Add(annotation.Label, 0); } labelCounter[annotation.Label]++; var label = annotation.Label; if (labelCounter[annotation.Label] > 1) { label += " " + "(" + labelCounter[annotation.Label] + ")"; } //**************** get current label instance index ***************/ var databaseDir = imgName.Replace(imgKey, String.Empty); var samplePath = getOutputImageName(databaseDir, imgKey, label); Directory.CreateDirectory(Path.GetDirectoryName(samplePath)); var sampleImg = img.GetSubRect(annotation.BoundingRectangle); //sampleImg = (sampleImg as Image<Bgr, byte>).SmoothGaussian(5); ImageIO.TrySave(sampleImg, samplePath); } } } capture.Seek(currPos, SeekOrigin.Begin); }
/// <summary> /// Calls read function defined by the stream and converts an returned image if necessary. /// <para>If the image can not be read (null), null is returned.</para> /// </summary> /// <param name="imageStream">Image stream.</param> /// <returns>Converted image or null if the image can not be read.</returns> public static Image <TColor> ReadAs <TColor>(this ImageStreamReader <IImage> imageStream) where TColor : struct { var image = imageStream.Read(); if (image == null) { return(null); } return(image as Image <TColor>); }
/// <summary> /// Calls read function defined by the stream and converts an returned image if necessary. /// <para>If the image can not be read (null), null is returned.</para> /// </summary> /// <param name="imageStream">Image stream.</param> /// <param name="copyAlways">Forces data copy even if a casting is enough.</param> /// <param name="failIfCannotCast">If data copy is needed throws an exception.</param> /// <returns>Converted image or null if the image can not be read.</returns> public static Image <TColor, TDepth> ReadAs <TColor, TDepth>(this ImageStreamReader <IImage> imageStream, bool copyAlways = false, bool failIfCannotCast = false) where TColor : IColor where TDepth : struct { var image = imageStream.Read(); if (image == null) { return(null); } return(((Image)image).Convert <TColor, TDepth>(copyAlways, failIfCannotCast)); }