Example #1
0
 /// <summary>
 /// Primal-dual algorithm is an algorithm for solving special types of variational problems (that is, finding a function to minimize some functional).
 /// As the image denoising, in particular, may be seen as the variational problem, primal-dual algorithm then can be used to perform 
 /// denoising and this is exactly what is implemented.
 /// </summary>
 /// <param name="observations">This array should contain one or more noised versions of the image that is to be restored.</param>
 /// <param name="result">Here the denoised image will be stored. There is no need to do pre-allocation of storage space, as it will be automatically allocated, if necessary.</param>
 /// <param name="lambda">Corresponds to  in the formulas above. As it is enlarged, the smooth (blurred) images are treated more favorably than detailed (but maybe more noised) ones. Roughly speaking, as it becomes smaller, the result will be more blur but more sever outliers will be removed.</param>
 /// <param name="niters">Number of iterations that the algorithm will run. Of course, as more iterations as better, but it is hard to quantitatively refine this statement, so just use the default and increase it if the results are poor.</param>
 public static void DenoiseTVL1(Mat[] observations, Mat result, double lambda, int niters)
 {
    using (Util.VectorOfMat vm = new Util.VectorOfMat(observations))
    {
       cveDenoiseTVL1(vm, result, lambda, niters);
    }
 }
Example #2
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(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);
                    }
            }
        }
Example #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;
                }
            }
        }
Example #4
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(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 );
            }
         }
      }
Example #5
0
      /// <summary>
      /// Computes dense optical flow using Gunnar Farneback's algorithm
      /// </summary>
      /// <param name="prev0">The first 8-bit single-channel input image</param>
      /// <param name="next0">The second input image of the same size and the same type as prevImg</param>
      /// <param name="flowX">The computed flow image for x-velocity; will have the same size as prevImg</param>
      /// <param name="flowY">The computed flow image for y-velocity; will have the same size as prevImg</param>
      /// <param name="pyrScale">Specifies the image scale (!1) to build the pyramids for each image. pyrScale=0.5 means the classical pyramid, where each next layer is twice smaller than the previous</param>
      /// <param name="levels">The number of pyramid layers, including the initial image. levels=1 means that no extra layers are created and only the original images are used</param>
      /// <param name="winSize">The averaging window size; The larger values increase the algorithm robustness to image noise and give more chances for fast motion detection, but yield more blurred motion field</param>
      /// <param name="iterations">The number of iterations the algorithm does at each pyramid level</param>
      /// <param name="polyN">Size of the pixel neighborhood used to find polynomial expansion in each pixel. The larger values mean that the image will be approximated with smoother surfaces, yielding more robust algorithm and more blurred motion field. Typically, poly n=5 or 7</param>
      /// <param name="polySigma">Standard deviation of the Gaussian that is used to smooth derivatives that are used as a basis for the polynomial expansion. For poly n=5 you can set poly sigma=1.1, for poly n=7 a good value would be poly sigma=1.5</param>
      /// <param name="flags">The operation flags</param>
      public static void CalcOpticalFlowFarneback(
         Image<Gray, Byte> prev0,
         Image<Gray, Byte> next0,
         Image<Gray, Single> flowX,
         Image<Gray, Single> flowY,
         double pyrScale,
         int levels,
         int winSize,
         int iterations,
         int polyN,
         double polySigma,
         CvEnum.OpticalflowFarnebackFlag flags)
      {
         using (Mat flow0 = new Mat(prev0.Height, prev0.Width, CvEnum.DepthType.Cv32F, 2))
         using (Util.VectorOfMat vm = new Util.VectorOfMat(flowX.Mat, flowY.Mat))
         {
            if ((int)(flags & Emgu.CV.CvEnum.OpticalflowFarnebackFlag.UseInitialFlow) != 0)
            {  //use initial flow
               CvInvoke.Merge(vm, flow0);
            }

            CvInvoke.CalcOpticalFlowFarneback(prev0, next0, flow0, pyrScale, levels, winSize, iterations, polyN, polySigma, flags);
            CvInvoke.Split(flow0, vm);
         }
      }
Example #6
0
      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);
         }
      }