/// <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(IImage image, int numberOfBins) { Mat[] channels = new Mat[image.NumberOfChannels]; Type imageType; if ((imageType = Toolbox.GetBaseType(image.GetType(), "Image`2")) != null || (imageType = Toolbox.GetBaseType(image.GetType(), "Mat")) != null) { for (int i = 0; i < image.NumberOfChannels; i++) { Mat channel = new Mat(); CvInvoke.ExtractChannel(image, channel, i); channels[i] = channel; } } else if ((imageType = Toolbox.GetBaseType(image.GetType(), "CudaImage`2")) != null) { IImage img = imageType.GetMethod("ToImage").Invoke(image, null) as IImage; for (int i = 0; i < img.NumberOfChannels; 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[image.NumberOfChannels]; colors = new Color[image.NumberOfChannels]; for (int i = 0; i < image.NumberOfChannels; i++) { channelNames[i] = String.Format("Channel {0}", i); colors[i] = Color.Red; } if (image is Mat) { typeOfDepth = CvInvoke.GetDepthType(((Mat)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; image.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]; } } #endregion minVal = (float)min; maxVal = (float)max; } #endregion 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); AddHistogram(channelNames[i], colors[i], hist, numberOfBins, ranges); } } }
/// <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(IImage image, int numberOfBins) { Mat[] channels = new Mat[image.NumberOfChannels]; Type imageType; if ((imageType = Toolbox.GetBaseType(image.GetType(), "Image`2")) != null || (imageType = Toolbox.GetBaseType(image.GetType(), "Mat")) != null) { for (int i = 0; i < image.NumberOfChannels; i++) { Mat channel = new Mat(); CvInvoke.ExtractChannel(image, channel, i); channels[i] = channel; } } else if ((imageType = Toolbox.GetBaseType(image.GetType(), "CudaImage`2")) != null) { IImage img = imageType.GetMethod("ToImage").Invoke(image, null) as IImage; for (int i = 0; i < img.NumberOfChannels; 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[image.NumberOfChannels]; colors = new Color[image.NumberOfChannels]; for (int i = 0; i < image.NumberOfChannels; i++) { channelNames[i] = String.Format("Channel {0}", i); colors[i] = Color.Red; } if (image is Mat) { typeOfDepth = CvInvoke.GetDepthType(((Mat)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; image.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]; } #endregion minVal = (float)min; maxVal = (float)max; } #endregion 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); AddHistogram(channelNames[i], colors[i], hist, numberOfBins, ranges ); } } }
/// <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; } } }
public void TestMatrixDFT() { //The matrix to be transformed. Matrix<float> matB = new Matrix<float>( new float[,] { {1.0f / 16.0f, 1.0f / 16.0f, 1.0f / 16.0f}, {1.0f / 16.0f, 8.0f / 16.0f, 1.0f / 16.0f}, {1.0f / 16.0f, 1.0f / 16.0f, 1.0f / 16.0f}}); Matrix<float> matBDft = new Matrix<float>( CvInvoke.GetOptimalDFTSize(matB.Rows), CvInvoke.GetOptimalDFTSize(matB.Cols)); CvInvoke.CopyMakeBorder(matB, matBDft, 0, matBDft.Height - matB.Height, 0, matBDft.Width - matB.Width, Emgu.CV.CvEnum.BorderType.Constant, new MCvScalar()); Matrix<float> dftIn = new Matrix<float>(matBDft.Rows, matBDft.Cols, 2); Matrix<float> matBDftBlank = matBDft.CopyBlank(); using (Util.VectorOfMat mv = new Util.VectorOfMat(new Mat[] { matBDft.Mat, matBDftBlank.Mat })) CvInvoke.Merge(mv, dftIn); Matrix<float> dftOut = new Matrix<float>(dftIn.Rows, dftIn.Cols, 2); //perform the Fourior Transform CvInvoke.Dft(dftIn, dftOut, Emgu.CV.CvEnum.DxtType.Forward, matB.Rows); //The real part of the Fourior Transform Matrix<float> outReal = new Matrix<float>(matBDft.Size); //The imaginary part of the Fourior Transform Matrix<float> outIm = new Matrix<float>(matBDft.Size); using (Util.VectorOfMat vm = new Util.VectorOfMat()) { vm.Push(outReal.Mat); vm.Push(outIm.Mat); CvInvoke.Split(dftOut, vm); } }