Пример #1
0
        /// <summary>
        /// Convert the CudaImage to its equivalent Bitmap representation
        /// </summary>
        public static Bitmap ToBitmap <TColor, TDepth>(this CudaImage <TColor, TDepth> cudaImage) where
        TColor : struct, IColor
            where TDepth : new()
        {
            if (typeof(TColor) == typeof(Bgr) && typeof(TDepth) == typeof(Byte))
            {
                Size   s      = cudaImage.Size;
                Bitmap result = new Bitmap(s.Width, s.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                System.Drawing.Imaging.BitmapData data = result.LockBits(new Rectangle(Point.Empty, result.Size),
                                                                         System.Drawing.Imaging.ImageLockMode.WriteOnly, result.PixelFormat);
                using (Image <TColor, TDepth> tmp = new Image <TColor, TDepth>(s.Width, s.Height, data.Stride, data.Scan0)
                       )
                {
                    cudaImage.Download(tmp);
                }

                result.UnlockBits(data);
                return(result);
            }
            else
            {
                using (Image <TColor, TDepth> tmp = cudaImage.ToImage())
                {
                    return(tmp.ToBitmap());
                }
            }
        }
Пример #2
0
        public void TestCudaPyr()
        {
            if (!CudaInvoke.HasCuda)
            {
                return;
            }
            Image <Gray, Byte> img = new Image <Gray, byte>(640, 480);

            img.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
            Image <Gray, Byte> down = img.PyrDown();
            Image <Gray, Byte> up   = down.PyrUp();

            CudaImage <Gray, Byte> gImg  = new CudaImage <Gray, byte>(img);
            CudaImage <Gray, Byte> gDown = new CudaImage <Gray, byte>(img.Size.Width >> 1, img.Size.Height >> 1);
            CudaImage <Gray, Byte> gUp   = new CudaImage <Gray, byte>(img.Size);

            CudaInvoke.PyrDown(gImg, gDown, null);
            CudaInvoke.PyrUp(gDown, gUp, null);

            CvInvoke.AbsDiff(down, gDown.ToImage(), down);
            CvInvoke.AbsDiff(up, gUp.ToImage(), up);
            double[] minVals, maxVals;
            Point[]  minLocs, maxLocs;
            down.MinMax(out minVals, out maxVals, out minLocs, out maxLocs);
            double maxVal = 0.0;

            for (int i = 0; i < maxVals.Length; i++)
            {
                if (maxVals[i] > maxVal)
                {
                    maxVal = maxVals[i];
                }
            }
            Trace.WriteLine(String.Format("Max diff: {0}", maxVal));
            EmguAssert.IsTrue(maxVal <= 1.0);
            //Assert.LessOrEqual(maxVal, 1.0);

            up.MinMax(out minVals, out maxVals, out minLocs, out maxLocs);
            maxVal = 0.0;
            for (int i = 0; i < maxVals.Length; i++)
            {
                if (maxVals[i] > maxVal)
                {
                    maxVal = maxVals[i];
                }
            }
            Trace.WriteLine(String.Format("Max diff: {0}", maxVal));
            EmguAssert.IsTrue(maxVal <= 1.0);
            //Assert.LessOrEqual(maxVal, 1.0);
        }
Пример #3
0
        public void TestClone()
        {
            if (CudaInvoke.HasCuda)
            {
                Image <Gray, Byte> img = new Image <Gray, byte>(300, 400);
                img.SetRandUniform(new MCvScalar(0.0), new MCvScalar(255.0));

                using (CudaImage <Gray, Byte> gImg1 = new CudaImage <Gray, byte>(img))
                    using (CudaImage <Gray, Byte> gImg2 = gImg1.Clone(null))
                        using (Image <Gray, Byte> img2 = gImg2.ToImage())
                        {
                            Assert.IsTrue(img.Equals(img2));
                        }
            }
        }
Пример #4
0
        public void TestColorConvert()
        {
            if (CudaInvoke.HasCuda)
            {
                Image <Bgr, Byte> img = new Image <Bgr, byte>(300, 400);
                img.SetRandUniform(new MCvScalar(0.0, 0.0, 0.0), new MCvScalar(255.0, 255.0, 255.0));
                Image <Gray, Byte> imgGray = img.Convert <Gray, Byte>();
                Image <Hsv, Byte>  imgHsv  = img.Convert <Hsv, Byte>();

                CudaImage <Bgr, Byte>  gpuImg     = new CudaImage <Bgr, Byte>(img);
                CudaImage <Gray, Byte> gpuImgGray = gpuImg.Convert <Gray, Byte>();
                CudaImage <Hsv, Byte>  gpuImgHsv  = gpuImg.Convert <Hsv, Byte>();

                Assert.IsTrue(gpuImgGray.Equals(new CudaImage <Gray, Byte>(imgGray)));
                Assert.IsTrue(gpuImgHsv.ToImage().Equals(imgHsv));
                Assert.IsTrue(gpuImgHsv.Equals(new CudaImage <Hsv, Byte>(imgHsv)));
            }
        }
Пример #5
0
        public Rectangle[] FindFaces(Mat frame, ref int type)
        {
            if (CudaInvoke.HasCuda && Global.useCuda)
            {
                using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(frame))
                    using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>())
                        using (GpuMat region = new GpuMat())
                        {
                            cuda_ccFace.DetectMultiScale(gpuGray, region);
                            Rectangle[] faces = cuda_ccFace.Convert(region);

                            if (faces.Length == 0)
                            {
                                cuda_ccSideFace.DetectMultiScale(gpuGray, region);
                                faces = cuda_ccSideFace.Convert(region);
                                if (faces.Length == 0)
                                {
                                    Image <Gray, byte> grayImage = gpuGray.ToImage();
                                    faces = ccAltFace.DetectMultiScale(grayImage, 1.02, 5, cuda_ccFace.MinObjectSize);
                                    if (faces.Length != 0)
                                    {
                                        type = 3;
                                    }
                                }
                                else
                                {
                                    type = 2;
                                }
                            }
                            else
                            {
                                type = 1;
                            }

                            return(faces);
                        }
            }
            else
            {
                return(FindFaces_WithoutGPU(frame, ref type));
            }
            //return null;
        }
Пример #6
0
        public void TestResizeGray()
        {
            if (CudaInvoke.HasCuda)
            {
                Image <Gray, Byte> img = new Image <Gray, byte>(300, 400);
                img.SetRandUniform(new MCvScalar(0.0), new MCvScalar(255.0));

                //Image<Gray, Byte> img = new Image<Gray, byte>("airplane.jpg");

                Image <Gray, Byte>     small       = img.Resize(100, 200, Emgu.CV.CvEnum.Inter.Linear);
                CudaImage <Gray, Byte> gpuImg      = new CudaImage <Gray, byte>(img);
                CudaImage <Gray, byte> smallGpuImg = new CudaImage <Gray, byte>(small.Size);
                CudaInvoke.Resize(gpuImg, smallGpuImg, small.Size);
                Image <Gray, Byte> diff = smallGpuImg.ToImage().AbsDiff(small);
                //ImageViewer.Show(smallGpuImg.ToImage());
                //ImageViewer.Show(small);
                //Assert.IsTrue(smallGpuImg.ToImage().Equals(small));
            }
        }
Пример #7
0
        public void TestResizeBgr()
        {
            if (CudaInvoke.HasCuda)
            {
                Image <Bgr, Byte> img = new Image <Bgr, byte>("pedestrian.png");
                //img.SetRandUniform(new MCvScalar(0.0, 0.0, 0.0), new MCvScalar(255.0, 255.0, 255.0));

                Size size = new Size(100, 200);

                CudaImage <Bgr, Byte> cudaImg      = new CudaImage <Bgr, byte>(img);
                CudaImage <Bgr, byte> smallCudaImg = new CudaImage <Bgr, byte>(size);

                CudaInvoke.Resize(cudaImg, smallCudaImg, size);
                Image <Bgr, Byte> smallCpuImg = img.Resize(size.Width, size.Height, Emgu.CV.CvEnum.Inter.Linear);


                Image <Bgr, Byte> diff = smallCudaImg.ToImage().AbsDiff(smallCpuImg);
                //TODO: Check why they are not an exact match
                //Assert.IsTrue(diff.CountNonzero()[0] == 0);
                //ImageViewer.Show(smallGpuImg.ToImage());
                //ImageViewer.Show(small);
            }
        }
Пример #8
0
        public void TestGpuMatAdd()
        {
            if (CudaInvoke.HasCuda)
            {
                int repeat = 1000;
                Image <Gray, Byte> img1 = new Image <Gray, byte>(1200, 640);
                Image <Gray, Byte> img2 = new Image <Gray, byte>(img1.Size);
                img1.SetRandUniform(new MCvScalar(0, 0, 0), new MCvScalar(255, 255, 255));
                img2.SetRandUniform(new MCvScalar(0, 0, 0), new MCvScalar(255, 255, 255));
                Image <Gray, Byte> cpuImgSum = new Image <Gray, byte>(img1.Size);
                Stopwatch          watch     = Stopwatch.StartNew();
                for (int i = 0; i < repeat; i++)
                {
                    CvInvoke.Add(img1, img2, cpuImgSum, null, CvEnum.DepthType.Cv8U);
                }
                watch.Stop();
                Trace.WriteLine(String.Format("CPU processing time: {0}ms", (double)watch.ElapsedMilliseconds / repeat));

                watch.Reset(); watch.Start();
                CudaImage <Gray, Byte> gpuImg1   = new CudaImage <Gray, byte>(img1);
                CudaImage <Gray, Byte> gpuImg2   = new CudaImage <Gray, byte>(img2);
                CudaImage <Gray, Byte> gpuImgSum = new CudaImage <Gray, byte>(gpuImg1.Size);
                Stopwatch watch2 = Stopwatch.StartNew();
                for (int i = 0; i < repeat; i++)
                {
                    CudaInvoke.Add(gpuImg1, gpuImg2, gpuImgSum);
                }
                watch2.Stop();
                Image <Gray, Byte> cpuImgSumFromGpu = gpuImgSum.ToImage();
                watch.Stop();
                Trace.WriteLine(String.Format("Core GPU processing time: {0}ms", (double)watch2.ElapsedMilliseconds / repeat));
                //Trace.WriteLine(String.Format("Total GPU processing time: {0}ms", (double)watch.ElapsedMilliseconds/repeat));

                Assert.IsTrue(cpuImgSum.Equals(cpuImgSumFromGpu));
            }
        }
Пример #9
0
      public void TestCudaPyr()
      {
         if (!CudaInvoke.HasCuda)
            return;
         Image<Gray, Byte> img = new Image<Gray, byte>(640, 480);
         img.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
         Image<Gray, Byte> down = img.PyrDown();
         Image<Gray, Byte> up = down.PyrUp();

         CudaImage<Gray, Byte> gImg = new CudaImage<Gray, byte>(img);
         CudaImage<Gray, Byte> gDown = new CudaImage<Gray, byte>(img.Size.Width >> 1, img.Size.Height >> 1);
         CudaImage<Gray, Byte> gUp = new CudaImage<Gray, byte>(img.Size);
         CudaInvoke.PyrDown(gImg, gDown, null);
         CudaInvoke.PyrUp(gDown, gUp, null);

         CvInvoke.AbsDiff(down, gDown.ToImage(), down);
         CvInvoke.AbsDiff(up, gUp.ToImage(), up);
         double[] minVals, maxVals;
         Point[] minLocs, maxLocs;
         down.MinMax(out minVals, out maxVals, out minLocs, out maxLocs);
         double maxVal = 0.0;
         for (int i = 0; i < maxVals.Length; i++)
         {
            if (maxVals[i] > maxVal)
               maxVal = maxVals[i];
         }
         Trace.WriteLine(String.Format("Max diff: {0}", maxVal));
         Assert.LessOrEqual(maxVal, 1.0);

         up.MinMax(out minVals, out maxVals, out minLocs, out maxLocs);
         maxVal = 0.0;
         for (int i = 0; i < maxVals.Length; i++)
         {
            if (maxVals[i] > maxVal)
               maxVal = maxVals[i];
         }
         Trace.WriteLine(String.Format("Max diff: {0}", maxVal));
         Assert.LessOrEqual(maxVal, 1.0);
      }
Пример #10
0
      public void TestResizeBgr()
      {
         if (CudaInvoke.HasCuda)
         {
            Image<Bgr, Byte> img = new Image<Bgr, byte>("pedestrian.png");
            //img.SetRandUniform(new MCvScalar(0.0, 0.0, 0.0), new MCvScalar(255.0, 255.0, 255.0));

            Size size = new Size(100, 200);

            CudaImage<Bgr, Byte> cudaImg = new CudaImage<Bgr, byte>(img);
            CudaImage<Bgr, byte> smallCudaImg = new CudaImage<Bgr, byte>(size);

            CudaInvoke.Resize(cudaImg, smallCudaImg, size);
            Image<Bgr, Byte> smallCpuImg = img.Resize(size.Width, size.Height, Emgu.CV.CvEnum.Inter.Linear);


            Image<Bgr, Byte> diff = smallCudaImg.ToImage().AbsDiff(smallCpuImg);
            //TODO: Check why they are not an exact match
            //Assert.IsTrue(diff.CountNonzero()[0] == 0);
            //ImageViewer.Show(smallGpuImg.ToImage());
            //ImageViewer.Show(small);
         }
      }
Пример #11
0
      public void TestResizeGray()
      {
         if (CudaInvoke.HasCuda)
         {
            Image<Gray, Byte> img = new Image<Gray, byte>(300, 400);
            img.SetRandUniform(new MCvScalar(0.0), new MCvScalar(255.0));

            //Image<Gray, Byte> img = new Image<Gray, byte>("airplane.jpg");

            Image<Gray, Byte> small = img.Resize(100, 200, Emgu.CV.CvEnum.Inter.Linear);
            CudaImage<Gray, Byte> gpuImg = new CudaImage<Gray, byte>(img);
            CudaImage<Gray, byte> smallGpuImg = new CudaImage<Gray, byte>(small.Size);
            CudaInvoke.Resize(gpuImg, smallGpuImg, small.Size);
            Image<Gray, Byte> diff = smallGpuImg.ToImage().AbsDiff(small);
            //ImageViewer.Show(smallGpuImg.ToImage());
            //ImageViewer.Show(small);
            //Assert.IsTrue(smallGpuImg.ToImage().Equals(small));
         }
      }
Пример #12
0
      public void TestGpuMatAdd()
      {
         if (CudaInvoke.HasCuda)
         {
            int repeat = 1000;
            Image<Gray, Byte> img1 = new Image<Gray, byte>(1200, 640);
            Image<Gray, Byte> img2 = new Image<Gray, byte>(img1.Size);
            img1.SetRandUniform(new MCvScalar(0, 0, 0), new MCvScalar(255, 255, 255));
            img2.SetRandUniform(new MCvScalar(0, 0, 0), new MCvScalar(255, 255, 255));
            Image<Gray, Byte> cpuImgSum = new Image<Gray, byte>(img1.Size);
            Stopwatch watch = Stopwatch.StartNew();
            for (int i = 0; i < repeat; i++)
               CvInvoke.Add(img1, img2, cpuImgSum, null, CvEnum.DepthType.Cv8U);
            watch.Stop();
            Trace.WriteLine(String.Format("CPU processing time: {0}ms", (double)watch.ElapsedMilliseconds / repeat));

            watch.Reset(); watch.Start();
            CudaImage<Gray, Byte> gpuImg1 = new CudaImage<Gray, byte>(img1);
            CudaImage<Gray, Byte> gpuImg2 = new CudaImage<Gray, byte>(img2);
            CudaImage<Gray, Byte> gpuImgSum = new CudaImage<Gray, byte>(gpuImg1.Size);
            Stopwatch watch2 = Stopwatch.StartNew();
            for (int i = 0; i < repeat; i++)
               CudaInvoke.Add(gpuImg1, gpuImg2, gpuImgSum);
            watch2.Stop();
            Image<Gray, Byte> cpuImgSumFromGpu = gpuImgSum.ToImage();
            watch.Stop();
            Trace.WriteLine(String.Format("Core GPU processing time: {0}ms", (double)watch2.ElapsedMilliseconds / repeat));
            //Trace.WriteLine(String.Format("Total GPU processing time: {0}ms", (double)watch.ElapsedMilliseconds/repeat));

            Assert.IsTrue(cpuImgSum.Equals(cpuImgSumFromGpu));
         }
      }