예제 #1
0
        // ROI: Region of Interest
        private bool myDetector(InputArray image, out System.Drawing.Rectangle[] ROIs)
        {
            Mat gray = null;

            // Convert multi-channel BGR image to gray
            if (image.GetChannels() > 1)
            {
                CvInvoke.CvtColor(image.GetMat(), gray, ColorConversion.Bgr2Gray);
            }
            else
            {
                gray = image.GetMat().Clone();
            }

            CvInvoke.EqualizeHist(gray, gray);
            ROIs = faceCsc.DetectMultiScale(gray, 1.1, 3, Size.Empty);
            return(true);
        }
예제 #2
0
        /// <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);
                }
            }
        }
예제 #3
0
        /// <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;
                }
            }
        }