Esempio n. 1
0
        public void TestMatchTemplate()
        {
            if (!CudaInvoke.HasCuda)
            {
                return;
            }

            #region prepare synthetic image for testing
            int   templWidth  = 50;
            int   templHeight = 50;
            Point templCenter = new Point(120, 100);

            //Create a random object
            Image <Bgr, Byte> randomObj = new Image <Bgr, byte>(templWidth, templHeight);
            randomObj.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));

            //Draw the object in image1 center at templCenter;
            Image <Bgr, Byte> img            = new Image <Bgr, byte>(300, 200);
            Rectangle         objectLocation = new Rectangle(templCenter.X - (templWidth >> 1), templCenter.Y - (templHeight >> 1), templWidth, templHeight);
            img.ROI = objectLocation;
            randomObj.Copy(img, null);
            img.ROI = Rectangle.Empty;
            #endregion

            Image <Gray, Single> match = img.MatchTemplate(randomObj, Emgu.CV.CvEnum.TemplateMatchingType.Sqdiff);
            double[]             minVal, maxVal;
            Point[] minLoc, maxLoc;
            match.MinMax(out minVal, out maxVal, out minLoc, out maxLoc);

            double gpuMinVal = 0, gpuMaxVal = 0;
            Point  gpuMinLoc = Point.Empty, gpuMaxLoc = Point.Empty;
            GpuMat cudaImage    = new GpuMat(img);
            GpuMat gpuRandomObj = new GpuMat(randomObj);
            GpuMat gpuMatch     = new GpuMat();
            using (CudaTemplateMatching buffer = new CudaTemplateMatching(DepthType.Cv8U, 3, CvEnum.TemplateMatchingType.Sqdiff))
                using (Stream stream = new Stream())
                {
                    buffer.Match(cudaImage, gpuRandomObj, gpuMatch, stream);
                    //GpuInvoke.MatchTemplate(CudaImage, gpuRandomObj, gpuMatch, CvEnum.TM_TYPE.CV_TM_SQDIFF, buffer, stream);
                    stream.WaitForCompletion();
                    CudaInvoke.MinMaxLoc(gpuMatch, ref gpuMinVal, ref gpuMaxVal, ref gpuMinLoc, ref gpuMaxLoc, null);
                }

            EmguAssert.AreEqual(minLoc[0].X, templCenter.X - templWidth / 2);
            EmguAssert.AreEqual(minLoc[0].Y, templCenter.Y - templHeight / 2);
            EmguAssert.IsTrue(minLoc[0].Equals(gpuMinLoc));
            EmguAssert.IsTrue(maxLoc[0].Equals(gpuMaxLoc));
        }