/// <summary> /// Get the depth type of the image /// </summary> /// <param name="image">The image to apply reflection on</param> /// <returns>The depth type of the image</returns> public static Type GetTypeOfDepth(IInputArray image) { Type baseType = Toolbox.GetBaseType(image.GetType(), "Image`2") ?? Toolbox.GetBaseType(image.GetType(), "CudaImage`2"); if (baseType != null) { return(baseType.GetGenericArguments()[1]); } else { using (InputArray iaImage = image.GetInputArray()) { return(CvInvoke.GetDepthType(iaImage.GetDepth())); } //baseType = Toolbox.GetBaseType(image.GetType(), "Mat"); //return //baseType == null ? null : //CvInvoke.GetDepthType((image as Mat).Depth); } }
/// <summary> /// Get the color type of the image /// </summary> /// <param name="image">The image to apply reflection on</param> /// <returns>The color type of the image</returns> public static Type GetTypeOfColor(IInputArray image) { Type baseType = Toolbox.GetBaseType(image.GetType(), "Image`2") ?? Toolbox.GetBaseType(image.GetType(), "CudaImage`2"); if (baseType != null) { return(baseType.GetGenericArguments()[0]); } else { using (InputArray ia = image.GetInputArray()) { int numberOfChannels = ia.GetChannels(); return (numberOfChannels == 1 ? typeof(Gray) : numberOfChannels == 3 ? typeof(Bgr) : numberOfChannels == 4 ? typeof(Bgra) : null); } } }
private void BuildOperationMenuItem(IInputArray image) { Type typeOfImage = image.GetType(); filtersToolStripMenuItem.DropDownItems.Clear(); //check if the menu for the specific image type has been built before if (!_typeToToolStripMenuItemsDictionary.ContainsKey(typeOfImage)) { //if not built, build it and save to the cache. _typeToToolStripMenuItemsDictionary.Add( typeOfImage, BuildOperationTree(Reflection.ReflectIImage.GetImageMethods(image)) ); } filtersToolStripMenuItem.DropDownItems.AddRange(_typeToToolStripMenuItemsDictionary[typeOfImage]); }
/// <summary> /// Get all the methods that belongs to the IImage and Image class with ExposableMethodAttribute set true. /// </summary> /// <param name="image">The IImage object to be refelected for methods marked with ExposableMethodAttribute</param> /// <returns>All the methods that belongs to the IImage and Image class with ExposableMethodAttribute set true</returns> public static IEnumerable <KeyValuePair <String, MethodInfo> > GetImageMethods(IInputArray image) { if (image != null) { foreach (MethodInfo mi in image.GetType().GetMethods()) { Object[] atts = mi.GetCustomAttributes(typeof(ExposableMethodAttribute), false); if (atts.Length > 0) { ExposableMethodAttribute att = (ExposableMethodAttribute)atts[0]; if (att.Exposable) { yield return(new KeyValuePair <String, MethodInfo>(att.Category, mi)); } } } } }
/// <summary> /// Get the color at the specific location of the image /// </summary> /// <param name="image">The image to obtain pixel value from</param> /// <param name="location">The location to sample a pixel</param> /// <returns>The color at the specific location</returns> public static IColor GetPixelColor(IInputArray image, Point location) { using (InputArray ia = image.GetInputArray()) { Size size = ia.GetSize(); location.X = Math.Min(location.X, size.Width - 1); location.Y = Math.Min(location.Y, size.Height - 1); MethodInfo indexers = image.GetType() .GetMethod("get_Item", new Type[2] { typeof(int), typeof(int) }); return(indexers == null ? new Bgra() : indexers.Invoke(image, new object[2] { location.Y, location.X }) as IColor); } }
public void SetImage(IInputArray image) { #region display the size of the image if (image != null) { using (InputArray iaImage = image.GetInputArray()) { Size size = iaImage.GetSize(); widthTextbox.Text = size.Width.ToString(); heightTextBox.Text = size.Height.ToString(); } } else { widthTextbox.Text = String.Empty; heightTextBox.Text = string.Empty; } #endregion #region display the color type of the image if (image != null) { Type colorType = Reflection.ReflectIImage.GetTypeOfColor(image); Object[] colorAttributes = colorType.GetCustomAttributes(typeof(ColorInfoAttribute), true); if (colorAttributes.Length > 0) { ColorInfoAttribute info = (ColorInfoAttribute)colorAttributes[0]; typeOfColorTexbox.Text = info.ConversionCodename; } else { typeOfColorTexbox.Text = Properties.StringTable.Unknown; } Type colorDepth = Reflection.ReflectIImage.GetTypeOfDepth(image); typeOfDepthTextBox.Text = colorDepth.Name; } else { typeOfColorTexbox.Text = string.Empty; typeOfDepthTextBox.Text = string.Empty; } #endregion #region check if image is a subclass of CvArr type if (image != null) { Type imgType = image.GetType(); if (IsSubTypeOf(imgType, typeof(CvArray <>))) { _imageType = typeof(CvArray <>); } else if (IsSubTypeOf(imgType, typeof(Mat))) { _imageType = typeof(Mat); } else if (IsSubTypeOf(imgType, typeof(UMat))) { _imageType = typeof(UMat); } else { _imageType = null; } } else { _imageType = null; } #endregion UpdateHistogram(); UpdateZoomScale(); }
/// <summary> /// Generate histograms for the image. One histogram is generated for each color channel. /// You will need to call the Refresh function to do the painting afterward. /// </summary> /// <param name="image">The image to generate histogram from</param> /// <param name="numberOfBins">The number of bins for each histogram</param> public void GenerateHistograms(IInputArray image, int numberOfBins) { using (InputArray iaImage = image.GetInputArray()) { int channelCount = iaImage.GetChannels(); Mat[] channels = new Mat[channelCount]; Type imageType; if ((imageType = Toolbox.GetBaseType(image.GetType(), "Image`2")) != null || (imageType = Toolbox.GetBaseType(image.GetType(), "Mat")) != null || (imageType = Toolbox.GetBaseType(image.GetType(), "UMat")) != null) { for (int i = 0; i < channelCount; i++) { Mat channel = new Mat(); CvInvoke.ExtractChannel(image, channel, i); channels[i] = channel; } } else if ((imageType = Toolbox.GetBaseType(image.GetType(), "CudaImage`2")) != null) { using (Mat img = imageType.GetMethod("ToMat").Invoke(image, null) as Mat) for (int i = 0; i < channelCount; i++) { Mat channel = new Mat(); CvInvoke.ExtractChannel(img, channel, i); channels[i] = channel; } } else { throw new ArgumentException(String.Format("The input image type of {0} is not supported", image.GetType().ToString())); } Type[] genericArguments = imageType.GetGenericArguments(); String[] channelNames; Color[] colors; Type typeOfDepth; if (genericArguments.Length > 0) { IColor typeOfColor = Activator.CreateInstance(genericArguments[0]) as IColor; channelNames = Reflection.ReflectColorType.GetNamesOfChannels(typeOfColor); colors = Reflection.ReflectColorType.GetDisplayColorOfChannels(typeOfColor); typeOfDepth = imageType.GetGenericArguments()[1]; } else { channelNames = new String[channelCount]; colors = new Color[channelCount]; for (int i = 0; i < channelCount; i++) { channelNames[i] = String.Format("Channel {0}", i); colors[i] = Color.Red; } if (image is Mat) { typeOfDepth = CvInvoke.GetDepthType(((Mat)image).Depth); } else if (image is UMat) { typeOfDepth = CvInvoke.GetDepthType(((UMat)image).Depth); } else { throw new ArgumentException(String.Format( "Unable to get the type of depth from image of type {0}", image.GetType().ToString())); } } float minVal, maxVal; #region Get the maximum and minimum color intensity values if (typeOfDepth == typeof(Byte)) { minVal = 0.0f; maxVal = 256.0f; } else { #region obtain the maximum and minimum color value double[] minValues, maxValues; Point[] minLocations, maxLocations; using (InputArray ia = image.GetInputArray()) using (Mat m = ia.GetMat()) { m.MinMax(out minValues, out maxValues, out minLocations, out maxLocations); double min = minValues[0], max = maxValues[0]; for (int i = 1; i < minValues.Length; i++) { if (minValues[i] < min) { min = minValues[i]; } if (maxValues[i] > max) { max = maxValues[i]; } } minVal = (float)min; maxVal = (float)max; } #endregion } #endregion Mat[] histograms = new Mat[channels.Length]; for (int i = 0; i < channels.Length; i++) { //using (DenseHistogram hist = new DenseHistogram(numberOfBins, new RangeF(minVal, maxVal))) using (Mat hist = new Mat()) using (Util.VectorOfMat vm = new Util.VectorOfMat()) { vm.Push(channels[i]); float[] ranges = new float[] { minVal, maxVal }; CvInvoke.CalcHist(vm, new int[] { 0 }, null, hist, new int[] { numberOfBins }, ranges, false); //hist.Calculate(new IImage[1] { channels[i] }, true, null); histograms[i] = GenerateHistogram(channelNames[i], colors[i], hist, numberOfBins, ranges); } } if (histograms.Length == 1) { this.Image = histograms[0]; } else { int maxWidth = 0; int totalHeight = 0; for (int i = 0; i < histograms.Length; i++) { maxWidth = Math.Max(maxWidth, histograms[i].Width); totalHeight += histograms[i].Height; } Mat concated = new Mat(new Size(maxWidth, totalHeight), histograms[0].Depth, histograms[0].NumberOfChannels); int currentY = 0; for (int i = 0; i < histograms.Length; i++) { using (Mat roi = new Mat(concated, new Rectangle(new Point(0, currentY), histograms[i].Size))) { histograms[i].CopyTo(roi); } currentY += histograms[i].Height; histograms[i].Dispose(); } this.Image = concated; } } }