Esempio n. 1
0
        private void AllocateImagesNPP(Bitmap size)
        {
            int w = size.Width;
            int h = size.Height;

            if (inputImage8uC3 == null)
            {
                inputImage8uC1 = new NPPImage_8uC1(w, h);
                inputImage8uC3 = new NPPImage_8uC3(w, h);
                inputImage8uC4 = new NPPImage_8uC4(w, h);
                imageBayer     = new NPPImage_32fC1(w, h);
                inputImage32f  = new NPPImage_32fC3(w, h);
                noisyImage8u   = new NPPImage_8uC3(w, h);
                noiseImage32f  = new NPPImage_32fC3(w, h);
                resultImage8u  = new NPPImage_8uC3(w, h);
                resultImage32f = new NPPImage_32fC3(w, h);
                return;
            }

            if (inputImage8uC3.Width >= w && inputImage8uC3.Height >= h)
            {
                inputImage8uC1.SetRoi(0, 0, w, h);
                inputImage8uC3.SetRoi(0, 0, w, h);
                inputImage8uC4.SetRoi(0, 0, w, h);
                imageBayer.SetRoi(0, 0, w, h);
                inputImage32f.SetRoi(0, 0, w, h);
                noisyImage8u.SetRoi(0, 0, w, h);
                noiseImage32f.SetRoi(0, 0, w, h);
                resultImage8u.SetRoi(0, 0, w, h);
                resultImage32f.SetRoi(0, 0, w, h);
            }
            else
            {
                inputImage8uC1.Dispose();
                inputImage8uC3.Dispose();
                inputImage8uC4.Dispose();
                imageBayer.Dispose();
                inputImage32f.Dispose();
                noisyImage8u.Dispose();
                noiseImage32f.Dispose();
                resultImage8u.Dispose();
                resultImage32f.Dispose();

                inputImage8uC1 = new NPPImage_8uC1(w, h);
                inputImage8uC3 = new NPPImage_8uC3(w, h);
                inputImage8uC4 = new NPPImage_8uC4(w, h);
                imageBayer     = new NPPImage_32fC1(w, h);
                inputImage32f  = new NPPImage_32fC3(w, h);
                noisyImage8u   = new NPPImage_8uC3(w, h);
                noiseImage32f  = new NPPImage_32fC3(w, h);
                resultImage8u  = new NPPImage_8uC3(w, h);
                resultImage32f = new NPPImage_32fC3(w, h);
            }
        }
Esempio n. 2
0
        public void SetReferenceImage(NPPImage_32fC1 reference)
        {
            NppiRect saveRoi = new NppiRect(reference.PointRoi, reference.SizeRoi);
            NppiRect roi     = new NppiRect();

            roi.x      = 0; // (reference.WidthRoi - imgToTrackRotated.WidthRoi) / 2;
            roi.y      = 0; // (reference.HeightRoi - imgToTrackRotated.HeightRoi) / 2;
            roi.width  = imgToTrackRotated.WidthRoi;
            roi.height = imgToTrackRotated.HeightRoi;

            reference.SetRoi(roi);
            reference.Copy(imgToTrackRotated);

            forward.Exec(imgToTrackRotated.DevicePointerRoi, imgRefCplx.DevicePointer);
            reference.SetRoi(saveRoi);
        }
Esempio n. 3
0
        public override float Inference(CudaDeviceVariable <float> input)
        {
            _input = input;

            NPPImage_32fC1 tempConv = new NPPImage_32fC1(_tempConvolution.DevicePointer, InWidth, InHeight, InWidth * sizeof(float));

            for (int outLayer = 0; outLayer < OutChannels; outLayer++)
            {
                SizeT          offsetOut        = outLayer * OutWidth * OutHeight * sizeof(float);
                CUdeviceptr    ptrWithOffsetOut = _z.DevicePointer + offsetOut;
                NPPImage_32fC1 imgOut           = new NPPImage_32fC1(ptrWithOffsetOut, OutWidth, OutHeight, OutWidth * sizeof(float));
                imgOut.Set(0);

                for (int inLayer = 0; inLayer < InChannels; inLayer++)
                {
                    SizeT          offsetIn        = inLayer * InWidth * InHeight * sizeof(float);
                    CUdeviceptr    ptrWithOffsetIn = _input.DevicePointer + offsetIn;
                    NPPImage_32fC1 imgIn           = new NPPImage_32fC1(ptrWithOffsetIn, InWidth, InHeight, InWidth * sizeof(float));

                    imgIn.SetRoi(_filterX / 2, _filterY / 2, InWidth - _filterX + 1, InHeight - _filterY + 1);

                    SizeT offsetFilter = (outLayer * InChannels * _filterX * _filterY + inLayer * _filterX * _filterY) * sizeof(float);
                    CudaDeviceVariable <float> filter = new CudaDeviceVariable <float>(_weights.DevicePointer + offsetFilter, false, _filterX * _filterY * sizeof(float));

                    imgIn.Filter(tempConv, filter, new NppiSize(_filterX, _filterY), new NppiPoint(_filterX / 2, _filterY / 2));
                    imgOut.Add(tempConv);
                }
                imgOut.Add(bHost[outLayer]);
            }

            switch (_activation)
            {
            case Activation.None:
                _y.CopyToDevice(_z);
                break;

            case Activation.Relu:
                //_aRelu is set to 0!
                _KernelPReluForward.RunSafe(_z, _aRelu, _y, _outWidth * _outHeight, _outChannels, _batch);
                break;

            case Activation.PRelu:
                _KernelPReluForward.RunSafe(_z, _aRelu, _y, _outWidth * _outHeight, _outChannels, _batch);
                break;

            case Activation.LeakyRelu:
                _KernelPReluForward.RunSafe(_z, _aRelu, _y, _outWidth * _outHeight, _outChannels, _batch);
                break;

            default:
                break;
            }

            return(_nextLayer.Inference(_y));
        }
Esempio n. 4
0
        public double4 ScanAngles(NPPImage_32fC1 img, double incr, double range, double zero)
        {
            NppiRect saveRoi = new NppiRect(img.PointRoi, img.SizeRoi);
            NppiRect roi     = new NppiRect();

            roi.x      = 0;
            roi.y      = 0;
            roi.width  = imgToTrackRotated.WidthRoi;
            roi.height = imgToTrackRotated.HeightRoi;
            img.SetRoi(roi);

            double maxVal = -double.MaxValue;
            double maxAng = 0;
            double maxX   = 0;
            double maxY   = 0;

            //first perform a coarse search
            for (double ang = zero - range; ang <= zero + range; ang += 5 * incr)
            {
                Matrix3x3 mat = Matrix3x3.RotAroundCenter(ang, imgToTrackRotated.Width, imgToTrackRotated.Height);
                imgToTrackRotated.Set(0);
                img.WarpAffine(imgToTrackRotated, mat.ToAffine(), InterpolationMode.Cubic);

                forward.Exec(imgToTrackRotated.DevicePointerRoi, imgToTrackCplx.DevicePointer);

                conjKernel.RunSafe(imgRefCplx, imgToTrackCplx);
                backward.Exec(imgToTrackCplx.DevicePointer, imgToTrackRotated.DevicePointerRoi);
                imgToTrackRotated.Div(imgToTrackRotated.WidthRoi * imgToTrackRotated.HeightRoi);

                imgToTrackRotated.MaxIndex(val, x, y);
                float v  = val;
                int   hx = x;
                int   hy = y;
                //Console.WriteLine("Found Max at " + ang.ToString("0.000") + " deg (" + hx + ", " + hy + ") = " + v);
                if (v > maxVal)
                {
                    maxVal = v;
                    maxAng = ang;
                    maxX   = x;
                    maxY   = y;
                    //Console.WriteLine("Max set!");
                }
            }

            zero  = maxAng;
            range = 10 * incr;
            //now perform a fine search but only around the previously found peak
            for (double ang = zero - range; ang <= zero + range; ang += incr)
            {
                Matrix3x3 mat = Matrix3x3.RotAroundCenter(ang, imgToTrackRotated.Width, imgToTrackRotated.Height);
                imgToTrackRotated.Set(0);
                img.WarpAffine(imgToTrackRotated, mat.ToAffine(), InterpolationMode.Cubic);

                int fftWidth = width / 2 + 1;
                forward.Exec(imgToTrackRotated.DevicePointerRoi, imgToTrackCplx.DevicePointer);
                conjKernel.RunSafe(imgRefCplx, imgToTrackCplx);
                backward.Exec(imgToTrackCplx.DevicePointer, imgToTrackRotated.DevicePointerRoi);
                imgToTrackRotated.Div(imgToTrackRotated.WidthRoi * imgToTrackRotated.HeightRoi);

                imgToTrackRotated.MaxIndex(val, x, y);

                float v  = val;
                int   hx = x;
                int   hy = y;
                if (v > maxVal)
                {
                    maxVal = v;
                    maxAng = ang;
                    maxX   = x;
                    maxY   = y;
                    //Console.WriteLine("Found Max at " + ang.ToString("0.000") + " deg (" + hx + ", " + hy + ") = " + v);
                    //Console.WriteLine("Max set!");
                }
            }

            if (maxX > imgToTrackRotated.WidthRoi / 2)
            {
                maxX -= imgToTrackRotated.WidthRoi;
            }
            if (maxY > imgToTrackRotated.HeightRoi / 2)
            {
                maxY -= imgToTrackRotated.HeightRoi;
            }

            img.SetRoi(saveRoi);
            return(new double4(-maxX, -maxY, maxAng, maxVal));
        }