public Tuple <SceneMetadata, SceneInfo, ImageCollection> Read(string tag, bool image) { // Read plant information from an xml file. if (!fileMappings.ContainsKey(tag)) { return(null); } List <PlantInfo> plants = new List <PlantInfo>(); // Load document XmlDocument doc = new XmlDocument(); doc.Load(fileMappings[tag]); XmlNode metadataNode = doc.SelectSingleNode("/rsml/metadata"); SceneMetadata metadata = ReadMetadata(metadataNode); XmlNode sceneNode = doc.SelectSingleNode("/rsml/scene"); SceneInfo scene = ReadScene(sceneNode); var imageGroup = new ImageCollection(); if (image) { imageGroup = ImageFinder.ImageSearch(Path.GetDirectoryName(fileMappings[tag]), tag); } return(new Tuple <SceneMetadata, SceneInfo, ImageCollection>(metadata, scene, imageGroup)); }
public bool Write(SceneMetadata metadata, SceneInfo scene) { // Create XML document XmlDocument document = new XmlDocument(); // RSML node XmlNode rsmlNode = document.CreateElement("rsml"); document.AppendChild(rsmlNode); // Metadata rsmlNode.AppendChild(CreateMetadata(document, metadata)); // Scene rsmlNode.AppendChild(CreateScene(document, scene)); // Write document to file if (initialDirectory.Last() != '\\') { initialDirectory += '\\'; } string fileName = metadata.Key; foreach (char c in Path.GetInvalidFileNameChars()) { fileName = fileName.Replace(c, '_'); } string file = System.IO.Path.Combine(initialDirectory, fileName + ".rsml"); document.Save(file); return(true); }
private void WriteMetadata(Excel.Worksheet sheet, SceneMetadata metadata, int startRow, int startColumn, out int finishRow, out int finishColumn) { // Variables int row = startRow, col = startColumn; Excel.Range metaRange = sheet.SelectCell(row, col); // Metadata title metaRange = sheet.SelectCell(row, col); metaRange.Value2 = "RSML Metadata"; row += 1; // Version metaRange = sheet.SelectCells(row++, col, 2, 0); metaRange.Value = new object[] { "Version", "", metadata.Version == "" ? "1" : metadata.Version }; // Unit metaRange = sheet.SelectCells(row++, col, 2, 0); metaRange.Value = new object[] { "Unit", "", metadata.Unit }; // Resolution metaRange = sheet.SelectCells(row++, col, 2, 0); metaRange.Value = new object[] { "Resolution", "", metadata.Resolution.ToString() }; // Last modified metaRange = sheet.SelectCells(row++, col, 2, 0); metaRange.Value = new object[] { "Last Modified", "", metadata.LastModified.ToString("d") }; // Set to date // Software metaRange = sheet.SelectCells(row++, col, 2, 0); metaRange.Value = new object[] { "Software", "", metadata.Software }; // User metaRange = sheet.SelectCells(row++, col, 2, 0); metaRange.Value = new object[] { "User", "", metadata.User }; finishRow = row - 1; finishColumn = col + 2; // Border and resize sheet.SelectCell(finishRow, finishColumn).EntireColumn.AutoFit(); StyleTable(sheet, startRow, startColumn, finishColumn - startColumn, finishRow - startRow); }
public static void SetIncompletePropertyOnScene(SceneMetadata metadata, SceneInfo sceneInfo) { // Metadata if (metadata.PropertyDefinitions == null) { metadata.PropertyDefinitions = new List <PropertyDefinition>(); } metadata.PropertyDefinitions.Add(new PropertyDefinition("complete", PropertyDefinition.PropertyType.Boolean, true)); // Scene if (sceneInfo.Properties == null) { sceneInfo.Properties = new List <Property>(); } sceneInfo.Properties.Add(new Property("complete", "false")); }
public static SceneMetadata RootNavDataToRSMLMetadata(SceneMetadata.ImageInfo imageInfo, RootNav.IO.TiffHeaderInfo headerInfo, string tag, RootCollection collection) { SceneMetadata metadata = new SceneMetadata(); metadata.Image = new SceneMetadata.ImageInfo() { Background = "dark", Hash = imageInfo.Hash, Label = System.IO.Path.GetFileName(imageInfo.Label) }; metadata.Key = tag; metadata.LastModified = DateTime.Now; metadata.Resolution = imageInfo.Unit == "pixels" ? 1.0 : imageInfo.Resolution; metadata.Unit = imageInfo.Unit == "" || imageInfo.Unit == "pixels" ? "pixel" : imageInfo.Unit; metadata.Software = "RootNav"; metadata.Version = "1"; metadata.User = System.Environment.UserName; return(metadata); }
public Tuple <SceneMetadata, SceneInfo, byte[]> Read(String tag, bool image) { // Read plant information from an xml file. if (!fileMappings.ContainsKey(tag)) { return(null); } List <PlantInfo> plants = new List <PlantInfo>(); // Load document XmlDocument doc = new XmlDocument(); doc.Load(fileMappings[tag]); XmlNode metadataNode = doc.SelectSingleNode("/rsml/metadata"); SceneMetadata metadata = ReadMetadata(metadataNode); XmlNode sceneNode = doc.SelectSingleNode("/rsml/scene"); SceneInfo scene = ReadScene(sceneNode); return(new Tuple <SceneMetadata, SceneInfo, byte[]>(metadata, scene, null)); }
public Tuple <SceneMetadata, SceneInfo, byte[]> ReadFromFile(String path, bool image) { List <PlantInfo> plants = new List <PlantInfo>(); try { // Load document XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode metadataNode = doc.SelectSingleNode("/rsml/metadata"); SceneMetadata metadata = ReadMetadata(metadataNode); XmlNode sceneNode = doc.SelectSingleNode("/rsml/scene"); SceneInfo scene = ReadScene(sceneNode); return(new Tuple <SceneMetadata, SceneInfo, byte[]>(metadata, scene, null)); } catch { return(null); } }
unsafe private void segmentedImagesMenuItem_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog() { SelectedPath = "G:\\Training Sets\\new" }; var result = fbd.ShowDialog(); if (result != System.Windows.Forms.DialogResult.OK) { return; } foreach (double penWidth in new double[] { 8 }) { bool useCrop = true; bool tipsOnly = false; bool primaryOnly = false; bool lateralOnly = false; bool conflictOnly = false; bool sourceOnly = false; bool outputOriginalImages = false; //double penWidth = 10; this.SnapsToDevicePixels = true; RenderOptions.SetEdgeMode(this, EdgeMode.Aliased); string[] supportedDatasets = new string[] { "RSDH00", "RSDH01", "RSDH02", "RSDH03", "RSDH04", "RSDH06", "RSDH07", "RSDH08", "RSDH09", "RSDH10" }; List <string> tags = rootReader.FilterTags(supportedDatasets, true); Random rand = new Random(); string path = fbd.SelectedPath; string segPath = path + "\\segmentation" + penWidth; string imagePath = path + "\\images"; int runningTotal = 0; int count = 0; if (!Directory.Exists(imagePath)) { Directory.CreateDirectory(imagePath); } if (!Directory.Exists(segPath)) { Directory.CreateDirectory(segPath); } foreach (string tag in tags) { var data = rootReader.Read(tag, true); // If no image, skip if (data.Item3 == null) { continue; } SceneMetadata metadata = data.Item1; SceneInfo scene = data.Item2; byte[] imageData = data.Item3; BitmapSource currentImage; using (MemoryStream ms = new MemoryStream(imageData)) { JpegBitmapDecoder decoder = new JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); // Due to what appears to be a bug in .NET, wrapping in a WBMP causes a cache refresh and fixes some image corruption currentImage = new WriteableBitmap(decoder.Frames[0]); } SolidColorBrush scb = new SolidColorBrush(Colors.White); Pen p = new Pen(Brushes.White, penWidth); p.StartLineCap = PenLineCap.Round; p.EndLineCap = PenLineCap.Round; // Obtain all root information Dictionary <String, RootInfo> plantComponents = new Dictionary <string, RootInfo>(); foreach (PlantInfo plant in scene.Plants) { foreach (RootInfo root in plant) { plantComponents.Add(root.RelativeID, root); } } // Initialise conflict data int conflictScale = 30; int conflictWidth = currentImage.PixelWidth / conflictScale; int conflictHeight = currentImage.PixelHeight / conflictScale; int[] rootCount = new int[conflictWidth * conflictHeight]; bool[] rootCounted = new bool[conflictWidth * conflictHeight]; // Crop // Find global plant bounding box double left = double.MaxValue, right = double.MinValue, top = double.MaxValue, bottom = double.MinValue; foreach (var kvp in plantComponents) { SampledSpline spline = kvp.Value.Spline as SampledSpline; if (spline == null) { continue; } Rect r = spline.BoundingBox; if (r.Left < left) { left = r.Left; } if (r.Right > right) { right = r.Right; } if (r.Top < top) { top = r.Top; } if (r.Bottom > bottom) { bottom = r.Bottom; } // If detecting conflict if (conflictOnly) { RootInfo parent = plantComponents.ContainsKey(kvp.Key.Substring(0, kvp.Key.LastIndexOf("."))) ? plantComponents[kvp.Key.Substring(0, kvp.Key.LastIndexOf("."))] : null; if (parent == null) { Array.Clear(rootCounted, 0, rootCounted.Length); foreach (Point splinePoint in spline.SampledPoints) { int conflictX = (int)(splinePoint.X / conflictScale); int conflictY = (int)(splinePoint.Y / conflictScale); int conflictIndex = conflictY * conflictWidth + conflictX; if (!rootCounted[conflictIndex]) { rootCount[conflictIndex]++; rootCounted[conflictIndex] = true; } } } } } // Render to drawingVisual, then image int width = currentImage.PixelWidth; int height = currentImage.PixelHeight; // Optional Crop Int32Rect?cropRectangle = null; if (useCrop) { int cropPadding = 64; int l = Math.Max(0, (int)(left - cropPadding)); int r = Math.Min(width, (int)(right + cropPadding)); int t = Math.Max(0, (int)(top - cropPadding)); int b = Math.Min(height, (int)(bottom + cropPadding)); cropRectangle = new Int32Rect(l, t, r - l, b - t); } RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); SolidColorBrush background = new SolidColorBrush(Colors.Black); drawingContext.DrawRectangle(Brushes.Black, null, new Rect(0, 0, width, height)); // Draw splines StreamGeometry geometry = new StreamGeometry(); using (StreamGeometryContext sgc = geometry.Open()) { foreach (var kvp in plantComponents) { SampledSpline spline = kvp.Value.Spline as SampledSpline; if (spline != null) { Point[] points = spline.SampledPoints; // Optional line from start position on parent if (sourceOnly) { // First primary source if (kvp.Value.RelativeID == "1.1") { sgc.BeginFigure(points[0], false, false); sgc.LineTo(points[0], true, true); break; } } if (tipsOnly) { if (primaryOnly) { RootInfo parent = plantComponents.ContainsKey(kvp.Key.Substring(0, kvp.Key.LastIndexOf("."))) ? plantComponents[kvp.Key.Substring(0, kvp.Key.LastIndexOf("."))] : null; if (parent == null) { sgc.BeginFigure(points[points.Length - 2], false, false); sgc.LineTo(points[points.Length - 1], true, true); } } else if (lateralOnly) { RootInfo parent = plantComponents.ContainsKey(kvp.Key.Substring(0, kvp.Key.LastIndexOf("."))) ? plantComponents[kvp.Key.Substring(0, kvp.Key.LastIndexOf("."))] : null; if (parent != null) { sgc.BeginFigure(points[points.Length - 2], false, false); sgc.LineTo(points[points.Length - 1], true, true); } } else { sgc.BeginFigure(points[points.Length - 2], false, false); sgc.LineTo(points[points.Length - 1], true, true); } } else { RootInfo parent = plantComponents.ContainsKey(kvp.Key.Substring(0, kvp.Key.LastIndexOf("."))) ? plantComponents[kvp.Key.Substring(0, kvp.Key.LastIndexOf("."))] : null; if (parent != null) { Point start = parent.Spline.GetPoint(kvp.Value.StartReference); sgc.BeginFigure(new Point(start.X, start.Y), false, false); sgc.LineTo(new Point(points[0].X, points[0].Y), true, true); } else { sgc.BeginFigure(new Point(points[0].X, points[0].Y), false, false); } for (int i = 1; i < points.Length; i++) { sgc.LineTo(new Point(points[i].X, points[i].Y), true, true); } } } } } if (geometry.CanFreeze) { geometry.Freeze(); } drawingContext.DrawGeometry(p.Brush, p, geometry); drawingContext.Close(); rtb.Render(drawingVisual); if (rtb.CanFreeze) { rtb.Freeze(); } // Save original file BitmapEncoder Encoder; if (outputOriginalImages) { Encoder = new PngBitmapEncoder(); using (FileStream FS = File.Open(imagePath + "\\" + count + ".png", FileMode.OpenOrCreate)) { Encoder.Frames.Add(BitmapFrame.Create(useCrop ? new CroppedBitmap(currentImage, cropRectangle.Value) : currentImage)); Encoder.Save(FS); FS.Close(); } } // Create mask from RTB int renderStride = rtb.PixelWidth * 4; byte[] renderArr = new byte[rtb.PixelWidth * rtb.PixelHeight * 4]; rtb.CopyPixels(renderArr, rtb.PixelWidth * 4, 0); WriteableBitmap mask = new WriteableBitmap(rtb.PixelWidth, rtb.PixelHeight, 96.0, 96.0, PixelFormats.Gray8, null); mask.Lock(); byte *ptr = (byte *)mask.BackBuffer.ToPointer(); int stride = mask.BackBufferStride; for (int x = 0; x < mask.PixelWidth; x++) { for (int y = 0; y < mask.PixelHeight; y++) { // Obtain red pixel as representative of gray value byte current = renderArr[y * renderStride + (x * 4) + 1]; if (current > 127) { if (conflictOnly) { int index = (y / conflictScale) * conflictWidth + (x / conflictScale); if (rootCount[index] > 1) { *(ptr + y * stride + x) = 255; } } else { *(ptr + y * stride + x) = 255; } } } } mask.AddDirtyRect(new Int32Rect(0, 0, width, height)); mask.Unlock(); BitmapSource outputMask = mask; Encoder = new PngBitmapEncoder(); using (FileStream FS = File.Open(segPath + "\\" + count.ToString("D4") + ".png", FileMode.OpenOrCreate)) { Encoder.Frames.Add(BitmapFrame.Create(useCrop ? new CroppedBitmap(outputMask, cropRectangle.Value) : outputMask)); Encoder.Save(FS); FS.Close(); } count++; runningTotal++; if (runningTotal > 20) { runningTotal -= 20; GC.Collect(); GC.WaitForPendingFinalizers(); } } } }
private XmlNode CreateMetadata(XmlDocument document, SceneMetadata metadata) { // Create metadata node XmlNode metadataNode = document.CreateElement("metadata"); // Version metadataNode.AppendChild(document.CreateElement("version")).InnerText = metadata.Version; // Unit metadataNode.AppendChild(document.CreateElement("unit")).InnerText = metadata.Unit != "" ? metadata.Unit : "pixel"; // Resolution metadataNode.AppendChild(document.CreateElement("resolution")).InnerText = metadata.Unit != "" ? string.Format(nullInfo, "{0}", metadata.Resolution) : "1"; // Last-modified metadataNode.AppendChild(document.CreateElement("last-modified")).InnerText = DateTime.Now.ToString("s"); // Software metadataNode.AppendChild(document.CreateElement("software")).InnerText = metadata.Software != "" ? metadata.Software : "RootNav"; // User metadataNode.AppendChild(document.CreateElement("user")).InnerText = metadata.User != "" ? metadata.User : System.Environment.UserName; // File Key - Should be created before RSML is written to ensure uniqueness metadataNode.AppendChild(document.CreateElement("file-key")).InnerText = metadata.Key; // Property definitions if (metadata.PropertyDefinitions != null && metadata.PropertyDefinitions.Count > 0) { XmlNode propertyDefinitionsNode = metadataNode.AppendChild(document.CreateElement("property-definitions")); foreach (PropertyDefinition definition in metadata.PropertyDefinitions) { XmlNode definitionNode = propertyDefinitionsNode.AppendChild(document.CreateElement("property-definition")); definitionNode.AppendChild(document.CreateElement("label")).InnerText = definition.Label; definitionNode.AppendChild(document.CreateElement("type")).InnerText = definition.Type.ToString().ToLower(); // Add unit if available if (definition.Unit != null && definition.Unit != "") { definitionNode.AppendChild(document.CreateElement("unit")).InnerText = definition.Unit.ToLower(); } //definitionNode.AppendChild(document.CreateElement("unit")).InnerText = definition.Label; if (definition.DefaultValue != null) { definitionNode.AppendChild(document.CreateElement("default")).InnerText = string.Format(nullInfo, "{0}", definition.DefaultValue).ToLower(); } } } // Time sequence if (metadata.Sequence != null) { XmlNode sequenceNode = metadataNode.AppendChild(document.CreateElement("time-sequence")); sequenceNode.AppendChild(document.CreateElement("label")).InnerText = metadata.Sequence.Label; sequenceNode.AppendChild(document.CreateElement("index")).InnerText = metadata.Sequence.Index.ToString(); sequenceNode.AppendChild(document.CreateElement("unified")).InnerText = metadata.Sequence.Unified.ToString(); } // Image if (metadata.Image != null) { XmlNode imageNode = metadataNode.AppendChild(document.CreateElement("image")); if (metadata.Image.Label != "") { imageNode.AppendChild(document.CreateElement("label")).InnerText = metadata.Image.Label; } if (metadata.Image.Hash != "") { imageNode.AppendChild(document.CreateElement("sha256")).InnerText = metadata.Image.Hash; } if (metadata.Image.Captured.HasValue) { imageNode.AppendChild(document.CreateElement("captured")).InnerText = metadata.Image.Captured.Value.ToString("s"); } } return(metadataNode); }
private SceneMetadata ReadMetadata(XmlNode metadataNode) { SceneMetadata data = new SceneMetadata(); foreach (XmlNode child in metadataNode.ChildNodes) { switch (child.Name) { case "version": data.Version = child.InnerText; break; case "unit": data.Unit = child.InnerText; break; case "resolution": double res; if (double.TryParse(child.InnerText, System.Globalization.NumberStyles.Any, nullInfo, out res)) { data.Resolution = res; } break; case "last-modified": DateTime val; if (DateTime.TryParse(child.InnerText, out val)) { data.LastModified = val; } break; case "software": data.Software = child.InnerText; break; case "user": data.User = child.InnerText; break; case "file-key": data.Key = child.InnerText; break; case "property-definitions": // For each property-definition data.PropertyDefinitions = new List <PropertyDefinition>(); foreach (XmlNode propertyDefinitionNode in child.SelectNodes("property-definition")) { XmlNode labelNode = propertyDefinitionNode.SelectSingleNode("label"); XmlNode typeNode = propertyDefinitionNode.SelectSingleNode("type"); XmlNode defaultNode = propertyDefinitionNode.SelectSingleNode("default"); XmlNode unitNode = propertyDefinitionNode.SelectSingleNode("unit"); if (labelNode == null || typeNode == null || defaultNode == null) { // Invalid property definition continue; } string unit = unitNode != null && unitNode.InnerText != "" ? unitNode.InnerText : null; string typeString = typeNode.InnerText.Trim(new char[] { '\'', '"', ' ' }); // Only create a property if it is of a valid type switch (typeString) { case "boolean": bool defaultBool = false; if (Boolean.TryParse(defaultNode.InnerText, out defaultBool)) { data.PropertyDefinitions.Add(new PropertyDefinition(labelNode.InnerText, PropertyDefinition.PropertyType.Boolean, defaultBool, unit)); } break; case "string": string defaultString = defaultNode.InnerText.Trim(new char[] { ' ', '"', '\'' }); if (defaultString != "") { data.PropertyDefinitions.Add(new PropertyDefinition(labelNode.InnerText, PropertyDefinition.PropertyType.String, defaultString, unit)); } break; case "integer": int defaultInt = 0; if (Int32.TryParse(defaultNode.InnerText, out defaultInt)) { data.PropertyDefinitions.Add(new PropertyDefinition(labelNode.InnerText, PropertyDefinition.PropertyType.Integer, defaultInt, unit)); } break; case "float": float defaultFloat = 0; if (float.TryParse(defaultNode.InnerText, System.Globalization.NumberStyles.Any, nullInfo, out defaultFloat)) { data.PropertyDefinitions.Add(new PropertyDefinition(labelNode.InnerText, PropertyDefinition.PropertyType.Float, defaultFloat, unit)); } break; case "double": double defaultDouble = 0; if (double.TryParse(defaultNode.InnerText, System.Globalization.NumberStyles.Any, nullInfo, out defaultDouble)) { data.PropertyDefinitions.Add(new PropertyDefinition(labelNode.InnerText, PropertyDefinition.PropertyType.Double, defaultDouble, unit)); } break; } } break; case "time-sequence": data.Sequence = new SceneMetadata.TimeSequence(); foreach (XmlNode sequenceChild in child.ChildNodes) { if (sequenceChild.Name == "label") { data.Sequence.Label = sequenceChild.InnerText; } else if (sequenceChild.Name == "index") { int index; if (Int32.TryParse(sequenceChild.InnerText, out index)) { data.Sequence.Index = index; } } else if (sequenceChild.Name == "unified") { bool unified; if (bool.TryParse(sequenceChild.InnerText, out unified)) { data.Sequence.Unified = unified; } } } break; case "image": data.Image = new SceneMetadata.ImageInfo(); foreach (XmlNode imageChild in child.ChildNodes) { if (imageChild.Name == "label") { data.Image.Label = imageChild.InnerText; } else if (imageChild.Name == "sha256") { data.Image.Hash = imageChild.InnerText; } else if (imageChild.Name == "captured") { DateTime captured; if (DateTime.TryParse(imageChild.InnerText, out captured)) { data.Image.Captured = captured; } } } break; } } return(data); }
private void extractImagesMenuItem_Click(object sender, RoutedEventArgs e) { string[] supportedDatasets = new string[] { "RSDH00", "RSDH01", "RSDH02", "RSDH03", "RSDH04", "RSDH06", "RSDH07", "RSDH08", "RSDH09", "RSDH10" }; //string[] supportedDatasets = new string[] { "RSDH06" }; List <string> tags = rootReader.FilterTags(supportedDatasets, true); Random rand = new Random(); System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog() { SelectedPath = "G:\\Training Sets\\root-systems" }; var result = fbd.ShowDialog(); if (result != System.Windows.Forms.DialogResult.OK) { return; } string path = fbd.SelectedPath; string positivePath = path + "\\positive-42"; string negativePath = path + "\\negative-42"; //if (!Directory.Exists(positivePath)) Directory.CreateDirectory(positivePath); // if (!Directory.Exists(negativePath)) Directory.CreateDirectory(negativePath); // Directory selected, begin extracting images StreamReader strmrdr = new StreamReader(path + "\\mapped.txt"); Dictionary <string, string> maps = new Dictionary <string, string>(); while (!strmrdr.EndOfStream) { string line = strmrdr.ReadLine(); string[] splits = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); if (splits[1] != "#N/A") { string s = Path.GetFileName(splits[0]).Replace("JPG", "jpg"); maps.Add(splits[1].Trim(new char[] { '"' }), s); } } strmrdr.Close(); const int DIM = 42; const int HALF = DIM / 2; Dictionary <int, int> labels = new Dictionary <int, int>(); int positiveIndex = 0; int negativeIndex = 0; int runningTotal = 0; foreach (string tag in tags) { var data = rootReader.Read(tag, true); // If no image, skip if (data.Item3 == null) { continue; } SceneMetadata metadata = data.Item1; SceneInfo scene = data.Item2; byte[] imageData = data.Item3; BitmapSource currentImage; using (MemoryStream ms = new MemoryStream(imageData)) { JpegBitmapDecoder decoder = new JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); // Due to what appears to be a bug in .NET, wrapping in a WBMP causes a cache refresh and fixes some image corruption currentImage = new WriteableBitmap(decoder.Frames[0]); } int sampleCount = 0; List <Point> ends = new List <Point>(); foreach (PlantInfo pl in scene.Plants) { /*************************************** * ROOT TIPS */ foreach (RootInfo ro in pl) { if (ro.Spline != null && ro.Spline.Length > HALF) { ends.Add(ro.Spline.End); /* * int x = (int)(ro.Spline.End.X + 0.5); * int y = (int)(ro.Spline.End.Y + 0.5); * * Int32Rect bounds = new Int32Rect(x - HALF, y - HALF, DIM, DIM); * * if (bounds.X < 0) bounds.X = 0; * if (bounds.Y < 0) bounds.Y = 0; * if (bounds.X + bounds.Width >= currentImage.PixelWidth) bounds.X = currentImage.PixelWidth - bounds.Width - 1; * if (bounds.Y + bounds.Height >= currentImage.PixelHeight) bounds.Y = currentImage.PixelHeight - bounds.Height - 1; * * CroppedBitmap cb = new CroppedBitmap(currentImage, bounds); * * BitmapEncoder Encoder = new PngBitmapEncoder(); * * using (FileStream FS = File.Open(positivePath + "\\" + positiveIndex + ".png", FileMode.OpenOrCreate)) * { * Encoder.Frames.Add(BitmapFrame.Create(cb)); * Encoder.Save(FS); * FS.Close(); * } * * positiveIndex++; * sampleCount++; */ } } } ImageEncoder.SaveImage(path + "\\" + maps[tag].Replace("jpg", "png"), currentImage as WriteableBitmap, ImageEncoder.EncodingType.PNG); StreamWriter strm = new StreamWriter(path + "\\" + maps[tag].Replace("jpg", "txt")); foreach (Point p in ends) { strm.WriteLine(p.ToString()); } strm.Close(); int rootNegativeSampleCount = (sampleCount * 2) / 2; int randomNegativeSampleCount = (sampleCount * 2) - rootNegativeSampleCount; /*************************************** * ROOT NEGATIVE SAMPLES */ /* * List<RootInfo> roots = new List<RootInfo>(); * List<double> cumulativeLength = new List<double>(); * * // Prepare roots * foreach (PlantInfo pl in scene.Plants) * { * foreach (RootInfo ro in pl.Roots) * { * //if (ro.Spline.Length > 10) * //{ * roots.Add(ro); * if (cumulativeLength.Count == 0) * cumulativeLength.Add(ro.Spline.Length); * else * cumulativeLength.Add(ro.Spline.Length + cumulativeLength[cumulativeLength.Count - 1]); * //} * } * } * * for (int i = 0; i < rootNegativeSampleCount; i++) * { * * // Select root at random * double position = rand.NextDouble() * cumulativeLength[cumulativeLength.Count - 1]; * * int rootIndex = 0; * for (int p = 0; p < cumulativeLength.Count; p++) * { * if (position <= cumulativeLength[p]) * { * rootIndex = p; * break; * } * } * * RootInfo selectedRoot = roots[rootIndex]; * double selectedPosition = selectedRoot.Spline.Length < DIM ? 0 : rand.NextDouble() * (selectedRoot.Spline.Length - DIM); * Point pos = selectedRoot.Spline.GetPoint(selectedRoot.Spline.GetPositionReference(selectedPosition)); * * int x = (int)(pos.X + 0.5); * int y = (int)(pos.Y + 0.5); * * Int32Rect bounds = new Int32Rect(x - HALF, y - HALF, DIM, DIM); * * if (bounds.X < 0) bounds.X = 0; * if (bounds.Y < 0) bounds.Y = 0; * if (bounds.X + bounds.Width >= currentImage.PixelWidth) bounds.X = currentImage.PixelWidth - bounds.Width - 1; * if (bounds.Y + bounds.Height >= currentImage.PixelHeight) bounds.Y = currentImage.PixelHeight - bounds.Height - 1; * * CroppedBitmap cb = new CroppedBitmap(currentImage, bounds); * * BitmapEncoder Encoder = new PngBitmapEncoder(); * * using (FileStream FS = File.Open(negativePath + "\\" + negativeIndex + ".png", FileMode.OpenOrCreate)) * { * Encoder.Frames.Add(BitmapFrame.Create(cb)); * Encoder.Save(FS); * FS.Close(); * } * * negativeIndex++; * } */ /* * for (int i = 0; i < randomNegativeSampleCount; i++) * { * /*************************************** * RANDOM NEGATIVE SAMPLES */ /* * int x, y; * * bool validPosition; * * do * { * validPosition = true; * x = rand.Next(0, currentImage.PixelWidth - DIM - 1); * y = rand.Next(0, currentImage.PixelWidth - DIM - 1); * * foreach (PlantInfo pl in scene.Plants) * { * if (!validPosition) break; * * foreach (RootInfo ro in pl) * { * if (ro.Spline.Length < DIM) continue; * * int rX = (int)ro.Spline.End.X; * int rY = (int)ro.Spline.End.Y; * * if (x >= rX && x <= rX + DIM && y >= rY && y <= rY + DIM) * { * validPosition = false; * break; * } * } * } * } while (!validPosition); * * Int32Rect bounds = new Int32Rect(x - HALF, y - HALF, DIM, DIM); * * if (bounds.X < 0) bounds.X = 0; * if (bounds.Y < 0) bounds.Y = 0; * if (bounds.X + bounds.Width >= currentImage.PixelWidth) bounds.X = currentImage.PixelWidth - bounds.Width - 1; * if (bounds.Y + bounds.Height >= currentImage.PixelHeight) bounds.Y = currentImage.PixelHeight - bounds.Height - 1; * * CroppedBitmap cb = new CroppedBitmap(currentImage, bounds); * * BitmapEncoder Encoder = new PngBitmapEncoder(); * * using (FileStream FS = File.Open(negativePath + "\\" + negativeIndex + ".png", FileMode.OpenOrCreate)) * { * Encoder.Frames.Add(BitmapFrame.Create(cb)); * Encoder.Save(FS); * FS.Close(); * } * * negativeIndex++; * } */ runningTotal += sampleCount * 2; if (runningTotal > 400) { runningTotal -= 400; GC.Collect(); GC.WaitForPendingFinalizers(); } } }
public void WriteData(Tuple <SceneMetadata, SceneInfo, byte[]> data) { // Freeze workbook Application.ScreenUpdating = false; // Obtain plant data SceneMetadata metadata = data.Item1; SceneInfo scene = data.Item2; Excel.Worksheet currentSheet = (Excel.Worksheet)Application.ActiveSheet; if (currentSheet != null) { if (currentSheet.UsedRange.Address != "$A$1") { // Currentsheet is not blank, create a new one. currentSheet = (Excel.Worksheet)Application.ActiveWorkbook.Worksheets.Add(); } } int row = 2, col = 2; // Write Key //Excel.Range metaRange = currentSheet.Range[currentSheet.Cells[row, col], currentSheet.Cells[row, col]]; Excel.Range metaRange = currentSheet.SelectCell(row, col); metaRange.Value2 = metadata.Key; metaRange.Font.Size += 2; metaRange.Font.Bold = true; // Metadata title row += 2; int metaEndRow, metaEndCol; WriteMetadata(currentSheet, metadata, row, col, out metaEndRow, out metaEndCol); metaRange = currentSheet.SelectCell(metaEndRow, metaEndCol); metaRange.Select(); metaRange = metaRange.Offset[1, 1]; int plantEndRow, dataEndRow, plantEndColumn; List <Point> sourcePositions; List <ImageSource> plantImages = CreateSeparatePlantImages(scene.Plants, out sourcePositions); WritePlants(currentSheet, scene, plantImages, metaEndRow + 2, col, col + 4, out plantEndRow, out plantEndColumn, out dataEndRow); // Plant Image if (scene.Plants.Count > 1) { try { Excel.Range pictureRange = currentSheet.SelectCells(row, metaEndCol + 2, 0, metaEndRow - row); pictureRange.Select(); Point sourcePosition; ImageSource source = CreatePlantImage(scene.Plants, out sourcePosition); InsertImageIntoRange(source, currentSheet, pictureRange); } catch { // Could not create image } } Application.ScreenUpdating = true; }
public Tuple <SceneMetadata, SceneInfo, byte[]> Read(String tag, bool image) { SceneInfo scene; SceneMetadata metadata; MySqlConnection connection = dbm.Connection as MySqlConnection; MySqlCommand selectCommand = new MySqlCommand("SELECT Stamp, RelativeID, StartReference, Spline, Label, CompleteArchitecture, User, HullArea FROM rootdata WHERE Tag = (?tag)", connection); selectCommand.Parameters.AddWithValue("?tag", tag); Dictionary <Tuple <String, DateTime>, List <RootInfo> > roots = new Dictionary <Tuple <String, DateTime>, List <RootInfo> >(); DateTime stamp = DateTime.Now; bool complete = true; HashSet <String> plantIDs = new HashSet <string>(); string user = ""; using (MySqlDataReader Reader = selectCommand.ExecuteReader()) { while (Reader.Read()) { if (user == "") { user = Reader.GetString("User"); } stamp = Reader.GetDateTime("Stamp"); String relativeID = Reader.GetString("RelativeID"); SampledSpline spline = null; complete = Reader.GetBoolean("CompleteArchitecture"); string label = null; if (!Reader.IsDBNull(4)) { label = Reader.GetString("Label"); } if (!Reader.IsDBNull(3)) { try { spline = RootNav.Data.SplineSerializer.BinaryToObject((byte[])Reader["Spline"]) as SampledSpline; } catch { spline = null; } } SplinePositionReference position = null; if (!Reader.IsDBNull(2)) { position = RootNav.Data.SplineSerializer.BinaryToObject((byte[])Reader["StartReference"]) as SplinePositionReference; } String key = relativeID.IndexOf('.') < 0 ? relativeID : relativeID.Substring(0, relativeID.IndexOf('.')); Tuple <string, DateTime> datedKey = new Tuple <string, DateTime>(key, stamp); if (!roots.ContainsKey(datedKey)) { roots.Add(datedKey, new List <RootInfo>()); } roots[datedKey].Add(new RootInfo() { RelativeID = relativeID, StartReference = position, Spline = spline, Label = label, Stamp = stamp }); } var timeSeparatedRoots = DuplicateTagCheck(roots); List <PlantInfo> plants = new List <PlantInfo>(); foreach (var timeStampedKVP in timeSeparatedRoots) { DateTime currentStamp = timeStampedKVP.Key; var stampedRoots = timeStampedKVP.Value; foreach (var kvp in stampedRoots) { plants.Add(PlantInfo.CreateTree(tag, currentStamp, complete, kvp.Value)); } } metadata = new SceneMetadata() { Key = tag, Complete = complete, Sequence = null, User = user, Software = "RootNav", Resolution = 1.0, Unit = "pixel", }; scene = new SceneInfo() { Plants = plants }; } // Optional image search byte[] imageData = null; if (image) { connection = dbm.Connection as MySqlConnection; MySqlCommand imageCommand = new MySqlCommand("SELECT Image FROM images WHERE Tag = (?tag) AND Stamp = (?stamp)", connection); imageCommand.Parameters.AddWithValue("?tag", tag); imageCommand.Parameters.AddWithValue("?stamp", stamp); using (MySqlDataReader imageReader = imageCommand.ExecuteReader()) { if (imageReader.HasRows) { imageReader.Read(); imageData = imageReader["Image"] as byte[]; } } } return(new Tuple <SceneMetadata, SceneInfo, byte[]> (metadata, scene, imageData)); }