Esempio n. 1
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));
            }
        }
Esempio n. 2
0
        public object[] PerformRotationCompareCuda(double angulo, double scalex, double scaley, int tx, int ty, double skX = 0, double skY = 0)
        {
            Image <Rgba, byte> two       = null;
            Image <Rgba, byte> one       = null;
            GpuMat             resultMat = new GpuMat();
            GpuMat             destiny   = null;
            GpuMat             compare   = null;

            object[] array   = null;
            GpuMat   sumaImg = new GpuMat();

            try
            {
                //   tx += imgUtil.TX/2;


                //make one from all tranformations
                // one = imgUtil.expandedOne[0];
                one = UIOne;
                double raidus;
                int    newWidth  = Convert.ToInt32(UIOne.Width * scalex);
                int    newHeight = Convert.ToInt32(UIOne.Height * scaley);


                GpuMat gOne = new GpuMat(one.Mat);
                GpuMat gNew = new GpuMat();



                CudaInvoke.Rotate(gOne, gNew, gOne.Size, angulo, tx, ty);

                gOne.Dispose();
                gOne = new GpuMat();

                CudaInvoke.Resize(gNew, gOne, new Size(newWidth, newHeight));

                // one = one.Rotate(angulo, imgUtil.pitchBlack).Resize(newWidth, newHeight, Emgu.CV.CvEnum.Inter.Cubic);

                one = gOne.ToMat().ToImage <Rgba, byte>();

                //gNew.Dispose();

                //  gOne.Dispose();


                if (skX != 0 && skY != 0)
                {
                    //comparison
                    int rows       = one.Rows;
                    int cols       = one.Cols;
                    int maxXOffset = Convert.ToInt32(Math.Abs(cols * skX));
                    int maxYOffset = Convert.ToInt32(Math.Abs(rows * skY));

                    CudaImage <Rgba, byte> sheared = new CudaImage <Rgba, byte>(rows + maxYOffset, cols + maxXOffset, true);
                    try
                    {
                        for (int r = 0; r < sheared.Bitmap.Height; r++)
                        {
                            for (int c = 0; c < sheared.Bitmap.Width; c++)
                            {
                                int newR = r + Convert.ToInt32(c * skY) - maxYOffset / 2;
                                int newC = c + Convert.ToInt32(r * skX) - maxXOffset / 2;
                                if (newR >= 0 && newR < rows && newC >= 0 && newC < cols)
                                {
                                    sheared.Bitmap.SetPixel(r, c, one.Bitmap.GetPixel(newR, newC));
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    //    one.Dispose();
                    one.Dispose();
                    one = sheared.ToMat().ToImage <Rgba, byte>();
                    sheared.Dispose();
                }


                //   ty += imgUtil.TY/2;

                // MakeCanvas(ref one, out raidus);


                // int newWidth = Convert.ToInt32(imgUtil.expandedOne[0].Width*scalex);
                // int newHeight = Convert.ToInt32(imgUtil.expandedOne[0].Height*scaley);
                //    int newWidth = Convert.ToInt32(UIOne.Width * scalex);
                //    int newHeight = Convert.ToInt32(UIOne.Height * scaley);
                //    int newWidth = Convert.ToInt32(one.Width * scalex);
                //   int newHeight = Convert.ToInt32(one.Height * scaley);

                //  CvInvoke.MinAreaRect(UITwo.Bitmap.GetBounds().)


                //   int area = (UITwo).Height * (UITwo).Width;
                //  area/=3;

                //  int area = Convert.ToInt32( 4e5);
                //determine bigger between one and two
                Image <Rgba, byte>[] biggerSmaller = GetBiggerSmallerImages(ref UITwo, ref one);

                //make canvas from bigger and copy the smaller into this canvas

                //int area = (biggerSmaller[0]).Height * (biggerSmaller[0]).Width;


                two = MakeCanvas(ref biggerSmaller[0], out raidus);
                ///CAMBIE ESTO
                Point middle = GetMiddlePointCanvas(raidus, ref biggerSmaller[1]);
                //  middle.X += tx;
                //  middle.Y += ty;


                ///////////// // Point middle = GetMiddlePointCanvas(raidus, ref biggerSmaller[0]);
                Copy(ref middle, ref two, ref biggerSmaller[1]);

                //get Mat
                destiny = new GpuMat(two.Mat);
                two     = two.CopyBlank();
                // two.Dispose();

                //make canvas from bigger again
                //   two = MakeCanvas(ref biggerSmaller[0], out raidus);//;.Width, expandedEscaledUIToCompare[1].Height, pitchWhite);

                // Copy the bigger into the canvas
                middle = GetMiddlePointCanvas(raidus, ref biggerSmaller[0]);

                Copy(ref middle, ref two, ref biggerSmaller[0]);


                compare = new GpuMat(two.Mat); //two.Mat.Clone();

                two.Dispose();

                object result = null;


                CudaInvoke.BitwiseXor(compare, destiny, resultMat, null);// Emgu.CV.CvEnum.CmpType.Equal);
                //  CudaInvoke.Subtract(compare, destiny, resultMat);


                // CudaInvoke.Pow(resultMat, 2, resultMat);


                //   CvInvoke.Compare(compare, destiny, resultMat, Emgu.CV.CvEnum.CmpType.Equal);
                CudaInvoke.Add(compare, destiny, sumaImg);//.ass.ToImage<Rgba,byte>().Add(destiny.ToImage<Rgba,byte>()).Mat;

                result = resultMat.ToMat();

                two = resultMat.ToMat().ToImage <Rgba, byte>();

                object suma = null;

                object count = null;

                suma  = two.GetSum();
                count = two.CountNonzero();
                int area = Convert.ToInt32(2e5);

                (count as int[])[3] = area;
                //   result = two.Mat.Clone();


                //  CvInvoke.Subtract(compare, destiny, resultMat);


                array = new object[] { result, count, suma };
            }
            catch (Exception ex)
            {
            }
            destiny?.Dispose();
            compare?.Dispose();
            resultMat?.Dispose();
            two?.Dispose();
            one?.Dispose();
            sumaImg?.Dispose();
            return(array);
        }