/// <summary> /// Saves this image to file, looking up the labels in <paramref name="labels"/>. /// </summary> /// <param name="labels">Label lookup dictionary</param> /// <param name="filename">File to save the image to</param> public unsafe void Save(LabelDictionary labels, string filename) { if (labels.Count < 256) { BitmapPalette palette = new BitmapPalette(labels.LabelLookup.Keys.ToList()); byte[] pixels = new byte[Columns * Rows]; fixed(short *dataSrc = RawArray) { fixed(byte *pixelsSrc = pixels) { short *dataPtr = dataSrc; byte * pixelsPtr = pixelsSrc; int count = pixels.Length; while (count-- > 0) { *pixelsPtr++ = (byte)*dataPtr++; } } } BitmapSource source = BitmapSource.Create(Columns, Rows, 96, 96, PixelFormats.Indexed8, palette, pixels, Columns); BitmapEncoder encoder = IO.GetEncoder(filename); encoder.Frames.Add(BitmapFrame.Create(source)); encoder.Save(filename); } else { ToRGBImage(labels).Save(filename); } }
/// <summary> /// Constructor. /// </summary> /// <param name="image">Color image in label image format</param> /// <param name="dictionary">Color dictionary lookup for interpreting the colors in the source image into labels</param> public unsafe LabelImage(RGBImage image, LabelDictionary dictionary) { _handler = new ShortArrayHandler(image.Rows, image.Columns, 1); short index = 1; short[, ,] lookup = new short[256, 256, 256]; if (dictionary != null) { Dictionary <Color, short> labelLookup = dictionary.LabelLookup; foreach (Color c in labelLookup.Keys) { lookup[c.R, c.G, c.B] = labelLookup[c]; } } else { for (int R = 0; R < 256; R++) { for (int G = 0; G < 256; G++) { for (int B = 0; B < 256; B++) { lookup[R, G, B] = -1; } } } } fixed(byte *imageSrc = image.RawArray) { fixed(short *labelsSrc = RawArray) { byte * imagePtr = imageSrc; short *labelsPtr = labelsSrc; int count = image.Rows * image.Columns; while (count-- > 0) { byte R = *imagePtr++; byte G = *imagePtr++; byte B = *imagePtr++; short label = lookup[R, G, B]; if (label < 0) { label = index; lookup[R, G, B] = index++; } *labelsPtr++ = label; } } } }
/// <summary> /// Converts the label image to an RGB Image using <paramref name="dictionary"/>. /// </summary> /// <param name="dictionary">The label lookup dictionary</param> /// <returns>An RGB Image</returns> public unsafe RGBImage ToRGBImage(LabelDictionary dictionary) { updateLabelCounts(); int labelCount = _labelCounts.Keys.Max() + 1; Color[] colors = new Color[labelCount]; colors[0] = Colors.Black; for (short i = 1; i < labelCount; i++) { colors[i] = LabelDictionary.LabelToColor(i); } if (dictionary != null) { colors = new Color[dictionary.Count]; Dictionary <Color, short> lookupTable = dictionary.LabelLookup; foreach (Color key in lookupTable.Keys) { colors[lookupTable[key]] = key; } } RGBImage rgb = new RGBImage(Rows, Columns); fixed(short *labelsSrc = RawArray) { fixed(byte *rgbSrc = rgb.RawArray) { short *labelsPtr = labelsSrc; byte * rgbPtr = rgbSrc; int count = Rows * Columns; while (count-- > 0) { short label = *labelsPtr++; if (label < 0) { label = 0; } Color c = colors[label]; * rgbPtr++ = c.R; * rgbPtr++ = c.G; * rgbPtr++ = c.B; } } } return(rgb); }
/// <summary> /// Returns a string representation of this label set using <paramref name="labelDictionary"/> to lookup the label values for representative strings. /// </summary> /// <param name="labelDictionary">Lookup dictionary</param> /// <returns>A string representation</returns> public string ToString(LabelDictionary labelDictionary) { if (_labels.Count == 0) { return("[]"); } string[] names = labelDictionary.LabelNames; StringBuilder sb = new StringBuilder(); sb.AppendFormat("[{0}", names[_labels[0]]); for (int i = 1; i < _labels.Count; i++) { sb.AppendFormat(", {0}", names[_labels[i]]); } sb.Append("]"); return(sb.ToString()); }
/// <summary> /// Writes <paramref name="dict"/> to <paramref name="stream"/> /// </summary> /// <param name="stream">The stream to write to</param> /// <param name="dict">The dictionary to write</param> public static void Write(Stream stream, LabelDictionary dict) { BinaryWriter output = new BinaryWriter(stream); output.Write(dict.Count); foreach (Color key in dict.LabelLookup.Keys) { short label = dict.LabelLookup[key]; if (label >= dict.Count) { continue; } string name = dict.LabelNames[label]; output.Write(label); output.Write(name); output.Write(key.R); output.Write(key.G); output.Write(key.B); } }
/// <summary> /// Saves this image to disk by choosing the maximum likelihood label at each pixel and looking up colors using <paramref name="dictionary"/>. /// </summary> /// <param name="dictionary">Color lookup dictionary</param> /// <param name="filename">Filename to write the image to</param> public void Save(LabelDictionary dictionary, string filename) { ToLabelImage().Save(dictionary, filename); }
/// <summary> /// Constructor. /// </summary> /// <param name="image">Color image in label image format</param> /// <param name="dictionary">Color dictionary lookup for interpreting the colors in the source image into labels</param> public LabelImage(System.Drawing.Bitmap image, LabelDictionary dictionary) : this(new RGBImage(image), dictionary) { }
/// <summary> /// Constructor. /// </summary> /// <param name="image">Color image in label image format</param> /// <param name="dictionary">Color dictionary lookup for interpreting the colors in the source image into labels</param> public LabelImage(BitmapSource image, LabelDictionary dictionary) : this(new RGBImage(image), dictionary) { }
/// <summary> /// Constructor. /// </summary> /// <param name="filename">Color image in label image format</param> /// <param name="dictionary">Color dictionary lookup for interpreting the colors in the source image into labels</param> public LabelImage(string filename, LabelDictionary dictionary) : this(new RGBImage(filename), dictionary) { }