Exemple #1
0
 private static void ConvertColor(IInputArray src, IOutputArray dest, Type srcColor, Type destColor, int dcn, Size size, Stream stream)
 {
     try
     {
         // if the direct conversion exist, apply the conversion
         CudaInvoke.CvtColor(src, dest, CvToolbox.GetColorCvtCode(srcColor, destColor), dcn, stream);
     } catch
     {
         try
         {
             //if a direct conversion doesn't exist, apply a two step conversion
             //in this case, needs to wait for the completion of the stream because a temporary local image buffer is used
             //we don't want the tmp image to be released before the operation is completed.
             using (CudaImage <Bgr, TDepth> tmp = new CudaImage <Bgr, TDepth>(size))
             {
                 CudaInvoke.CvtColor(src, tmp, CvToolbox.GetColorCvtCode(srcColor, typeof(Bgr)), 3, stream);
                 CudaInvoke.CvtColor(tmp, dest, CvToolbox.GetColorCvtCode(typeof(Bgr), destColor), dcn, stream);
                 stream.WaitForCompletion();
             }
         } catch (Exception excpt)
         {
             throw new NotSupportedException(String.Format(
                                                 "Conversion from CudaImage<{0}, {1}> to CudaImage<{2}, {3}> is not supported by OpenCV: {4}",
                                                 srcColor.ToString(),
                                                 typeof(TDepth).ToString(),
                                                 destColor.ToString(),
                                                 typeof(TDepth).ToString(),
                                                 excpt.Message));
         }
     }
 }
 private static void ConvertColor(IntPtr src, IntPtr dest, Type srcColor, Type destColor, Size size, Stream stream)
 {
     try
     {
         // if the direct conversion exist, apply the conversion
         GpuInvoke.CvtColor(src, dest, CvToolbox.GetColorCvtCode(srcColor, destColor), stream);
     }
     catch
     {
         try
         {
             //if a direct conversion doesn't exist, apply a two step conversion
             using (GpuImage <Bgr, TDepth> tmp = new GpuImage <Bgr, TDepth>(size))
             {
                 GpuInvoke.CvtColor(src, tmp.Ptr, CvToolbox.GetColorCvtCode(srcColor, typeof(Bgr)), stream);
                 GpuInvoke.CvtColor(tmp.Ptr, dest, CvToolbox.GetColorCvtCode(typeof(Bgr), destColor), stream);
             }
         }
         catch
         {
             throw new NotSupportedException(String.Format(
                                                 "Convertion from Image<{0}, {1}> to Image<{2}, {3}> is not supported by OpenCV",
                                                 srcColor.ToString(),
                                                 typeof(TDepth).ToString(),
                                                 destColor.ToString(),
                                                 typeof(TDepth).ToString()));
         }
     }
 }
Exemple #3
0
 private static void ConvertColor(IntPtr src, IntPtr dest, Type srcColor, Type destColor, Size size, Stream stream)
 {
     try
     {
         // if the direct conversion exist, apply the conversion
         GpuInvoke.CvtColor(src, dest, CvToolbox.GetColorCvtCode(srcColor, destColor), stream);
     } catch
     {
         try
         {
             //if a direct conversion doesn't exist, apply a two step conversion
             //in this case, needs to wait for the completion of the stream because a temporary local image buffer is used
             //we don't want the tmp image to be released before the operation is completed.
             using (GpuImage <Bgr, TDepth> tmp = new GpuImage <Bgr, TDepth>(size))
             {
                 GpuInvoke.CvtColor(src, tmp.Ptr, CvToolbox.GetColorCvtCode(srcColor, typeof(Bgr)), stream);
                 GpuInvoke.CvtColor(tmp.Ptr, dest, CvToolbox.GetColorCvtCode(typeof(Bgr), destColor), stream);
                 stream.WaitForCompletion();
             }
         } catch
         {
             throw new NotSupportedException(String.Format(
                                                 "Convertion from Image<{0}, {1}> to Image<{2}, {3}> is not supported by OpenCV",
                                                 srcColor.ToString(),
                                                 typeof(TDepth).ToString(),
                                                 destColor.ToString(),
                                                 typeof(TDepth).ToString()));
         }
     }
 }
Exemple #4
0
        public static Lab[] ToLabPalette <TColor>(TColor[] palette)
            where TColor : struct, IColor
        {
            Lab[] labPalette = null;

            try {
                // Try direct conversion
                CvToolbox.GetColorCvtCode(typeof(TColor), typeof(Lab));
                labPalette = ColorConversion.ConvertColors <TColor, Lab>(palette);
            } catch {
                // Indirect conversion (converting first to Rgb)
                Rgb[] tempPalette = ColorConversion.ConvertColors <TColor, Rgb>(palette);
                labPalette = ColorConversion.ConvertColors <Rgb, Lab>(tempPalette);
            }

            return(labPalette);
        }
Exemple #5
0
        public void TestCudaImageAsyncOps()
        {
            if (CudaInvoke.HasCuda)
            {
                int       counter = 0;
                Stopwatch watch   = Stopwatch.StartNew();
                using (GpuMat img1 = new GpuMat(3000, 2000, DepthType.Cv8U, 3))
                    using (GpuMat img2 = new GpuMat(3000, 2000, DepthType.Cv8U, 3))
                        using (GpuMat img3 = new GpuMat())
                            using (Stream stream = new Stream())
                                using (GpuMat mat1 = new GpuMat())
                                {
                                    img1.ConvertTo(mat1, DepthType.Cv8U, 1, 0, stream);
                                    while (!stream.Completed)
                                    {
                                        if (counter <= int.MaxValue)
                                        {
                                            counter++;
                                        }
                                    }
                                    Trace.WriteLine(String.Format("Counter has been incremented {0} times", counter));

                                    counter = 0;
                                    CudaInvoke.CvtColor(img2, img3, CvToolbox.GetColorCvtCode(typeof(Bgr), typeof(Gray)), 1, stream);
                                    while (!stream.Completed)
                                    {
                                        if (counter <= int.MaxValue)
                                        {
                                            counter++;
                                        }
                                    }
                                    Trace.WriteLine(String.Format("Counter has been incremented {0} times", counter));
                                }
                watch.Stop();
                Trace.WriteLine(String.Format("Total time: {0} milliseconds", watch.ElapsedMilliseconds));
            }
        }