コード例 #1
0
        /// <summary>
        /// image warp perspective batch.
        /// </summary>
        /// <param name="oSmallestSrcSize">Size in pixels of the entire smallest source image width and height, may be from different images.</param>
        /// <param name="oSrcRectROI">Region of interest in the source images (may overlap source image size width and height).</param>
        /// <param name="oDstRectROI">Region of interest in the destination images (may overlap destination image size width and height).</param>
        /// <param name="eInterpolation">The type of eInterpolation to perform resampling. Currently limited to NPPI_INTER_NN, NPPI_INTER_LINEAR, NPPI_INTER_CUBIC, or NPPI_INTER_SUPER. </param>
        /// <param name="pBatchList">Device memory pointer to nBatchSize list of NppiWarpAffineBatchCXR structures.</param>
        /// <param name="nppStreamCtx">NPP stream context.</param>
        public static void WarpPerspectiveBatch(NppiSize oSmallestSrcSize, NppiRect oSrcRectROI, NppiRect oDstRectROI, InterpolationMode eInterpolation, CudaDeviceVariable <NppiWarpAffineBatchCXR> pBatchList, NppStreamContext nppStreamCtx)
        {
            NppStatus status = NPPNativeMethods_Ctx.NPPi.GeometricTransforms.nppiWarpPerspectiveBatch_16f_C1R_Ctx(oSmallestSrcSize, oSrcRectROI, oDstRectROI, eInterpolation, pBatchList.DevicePointer, pBatchList.Size, nppStreamCtx);

            Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nppiWarpPerspectiveBatch_16f_C1R_Ctx", status));
            NPPException.CheckNppStatus(status, null);
        }
コード例 #2
0
 /// <summary>
 /// Perspective transform of an image.<para/>
 /// This function operates using given transform coefficients that 
 /// can be obtained by using nppiGetPerspectiveTransform function or set
 /// explicitly. The function operates on source and destination regions 
 /// of interest. The perspective warp function transforms the source image
 /// pixel coordinates (x,y) according to the following formulas:<para/>
 /// X_new = (C_00 * x + C_01 * y + C_02) / (C_20 * x + C_21 * y + C_22)<para/>
 /// Y_new = (C_10 * x + C_11 * y + C_12) / (C_20 * x + C_21 * y + C_22)<para/>
 /// The transformed part of the source image is resampled using the specified
 /// interpolation method and written to the destination ROI.
 /// The functions nppiGetPerspectiveQuad and nppiGetPerspectiveBound can help
 /// with destination ROI specification.<para/>
 /// NPPI specific recommendation: <para/>
 /// The function operates using 2 types of kernels: fast and accurate. The fast
 /// method is about 4 times faster than its accurate variant,
 /// but doesn't perform memory access checks and requires the destination ROI
 /// to be 64 bytes aligned. Hence any destination ROI is 
 /// chunked into 3 vertical stripes: the first and the third are processed by
 /// accurate kernels and the central one is processed by the fast one.
 /// In order to get the maximum available speed of execution, the projection of
 /// destination ROI onto image addresses must be 64 bytes aligned. This is
 /// always true if the values <para/>
 /// <code>(int)((void *)(pDst + dstRoi.x))</code> and <para/>
 /// <code>(int)((void *)(pDst + dstRoi.x + dstRoi.width))</code> <para/>
 /// are multiples of 64. Another rule of thumb is to specify destination ROI in
 /// such way that left and right sides of the projected image are separated from
 /// the ROI by at least 63 bytes from each side. However, this requires the
 /// whole ROI to be part of allocated memory. In case when the conditions above
 /// are not satisfied, the function may decrease in speed slightly and will
 /// return NPP_MISALIGNED_DST_ROI_WARNING warning.
 /// </summary>
 /// <param name="dest">Destination image</param>
 /// <param name="coeffs">Perspective transform coefficients [3,3]</param>
 /// <param name="eInterpolation">Interpolation mode: can be <see cref="InterpolationMode.NearestNeighbor"/>, <see cref="InterpolationMode.Linear"/> or <see cref="InterpolationMode.Cubic"/></param>
 public void WarpPerspective(NPPImage_16fC1 dest, double[,] coeffs, InterpolationMode eInterpolation)
 {
     NppiRect rectIn = new NppiRect(_pointRoi, _sizeRoi);
     NppiRect rectOut = new NppiRect(dest.PointRoi, dest.SizeRoi);
     status = NPPNativeMethods.NPPi.PerspectiveTransforms.nppiWarpPerspective_16f_C1R(_devPtr, _sizeOriginal, _pitch, rectIn, dest.DevicePointer, dest.Pitch, rectOut, coeffs, eInterpolation);
     Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nppiWarpPerspective_16f_C1R", status));
     NPPException.CheckNppStatus(status, this);
 }
コード例 #3
0
 private void pictureBox_src_MouseDown(object sender, MouseEventArgs e)
 {
     pressed       = e.Button;
     clickedCorner = GetCorner(e.X, e.Y);
     if (clickedCorner > 0)
     {
         org_selection = selection;
         pos           = new Point(e.X, e.Y);
     }
 }
コード例 #4
0
        static NppiRect GetOutputROI(NppiRect inputROI, int borderSize)
        {
            NppiRect output = new NppiRect();

            output.x      = inputROI.x + borderSize;
            output.y      = inputROI.y + borderSize;
            output.width  = inputROI.width - 2 * borderSize;
            output.height = inputROI.height - 2 * borderSize;

            return(output);
        }
コード例 #5
0
        public void TrimapFromRect(CudaPitchedDeviceVariable <byte> alpha, NppiRect rect, int width, int height)
        {
            dim3 block = new dim3(32, 8, 1);
            dim3 grid  = new dim3((int)((width + (block.x * 4) - 1) / (block.x * 4)), (height + 31) / 32, 1);

            //rect.y = height - 1 - (rect.y + rect.height - 1) ; // Flip horizontal (FreeImage inverts y axis)

            TrimapFromRectKernel.BlockDimensions = block;
            TrimapFromRectKernel.GridDimensions  = grid;
            TrimapFromRectKernel.Run(alpha.DevicePointer, (int)alpha.Pitch, rect, width, height);
            //TrimapFromRectKernel<<<grid, block>>>(alpha, alpha_pitch, rect, width, height );
        }
コード例 #6
0
        private static List <NppiRect> GetROIs(NppiRect maxRoi, int patchSize)
        {
            List <NppiRect> liste = new List <NppiRect>();

            for (int y = maxRoi.Top; y < maxRoi.Bottom - patchSize; y += patchSize)
            {
                for (int x = maxRoi.Left; x < maxRoi.Right - patchSize; x += patchSize)
                {
                    liste.Add(new NppiRect(x, y, patchSize, patchSize));
                }
            }
            return(liste);
        }
コード例 #7
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);
        }
コード例 #8
0
        public void RunImage(NPPImage_32fC3 input, NPPImage_32fC3 output)
        {
            NppiRect roiOrig = new NppiRect(input.PointRoi, input.SizeRoi);

            output.Set(new float[] { 0, 0, 0 });
            IEnumerable <Tiler.RoiInputOutput> rois = Tiler.GetROIs(new NppiRect(new NppiPoint(8, 8), new NppiSize(input.WidthRoi - 16, input.HeightRoi - 16)), _tileSize, 8);

            foreach (var item in rois)
            {
                input.SetRoi(item.inputROI);
                output.SetRoi(item.positionInFinalImage);
                tile.ResetRoi();

                input.Copy(tile);

                NPPImage_32fC1 npp32fCR = new NPPImage_32fC1(tileAsPlanes.DevicePointer, _tileSize, _tileSize, _tileSize * sizeof(float));
                NPPImage_32fC1 npp32fCG = new NPPImage_32fC1(tileAsPlanes.DevicePointer + (_tileSize * _tileSize * sizeof(float)), _tileSize, _tileSize, _tileSize * sizeof(float));
                NPPImage_32fC1 npp32fCB = new NPPImage_32fC1(tileAsPlanes.DevicePointer + 2 * (_tileSize * _tileSize * sizeof(float)), _tileSize, _tileSize, _tileSize * sizeof(float));

                tile.Copy(npp32fCR, 0);
                tile.Copy(npp32fCG, 1);
                tile.Copy(npp32fCB, 2);

                start.Inference(tileAsPlanes);

                CudaDeviceVariable <float> res = final.GetResult();
                int size = _tileSize - 16;
                npp32fCR = new NPPImage_32fC1(res.DevicePointer, size, size, size * sizeof(float));
                npp32fCG = new NPPImage_32fC1(res.DevicePointer + (size * size * sizeof(float)), size, size, size * sizeof(float));
                npp32fCB = new NPPImage_32fC1(res.DevicePointer + 2 * (size * size * sizeof(float)), size, size, size * sizeof(float));

                tile.SetRoi(0, 0, _tileSize - 16, _tileSize - 16);

                npp32fCR.Copy(tile, 0);
                npp32fCG.Copy(tile, 1);
                npp32fCB.Copy(tile, 2);

                tile.SetRoi(item.outputROI);
                tile.Copy(output);
            }
            input.SetRoi(roiOrig);
            output.SetRoi(roiOrig);
        }
コード例 #9
0
 /// <summary>
 /// 32-bit float convolution filter with border control.
 /// </summary>
 /// <param name="dest">Destination image</param>
 /// <param name="pKernel">Pointer to the start address of the kernel coefficient array. Coeffcients are expected to be stored in reverse order</param>
 /// <param name="nKernelSize">Width and Height of the rectangular kernel.</param>
 /// <param name="oAnchor">X and Y offsets of the kernel origin frame of reference relative to the source pixel.</param>
 /// <param name="eBorderType">The border type operation to be applied at source image border boundaries.</param>
 /// <param name="filterArea">The area where the filter is allowed to read pixels. The point is relative to the ROI set to source image, the size is the total size starting from the filterArea point. Default value is the set ROI.</param>
 /// <param name="nppStreamCtx">NPP stream context.</param>
 public void FilterBorder(NPPImage_16fC1 dest, CudaDeviceVariable <float> pKernel, NppiSize nKernelSize, NppiPoint oAnchor, NppiBorderType eBorderType, NppStreamContext nppStreamCtx, NppiRect filterArea = new NppiRect())
 {
     if (filterArea.Size == new NppiSize())
     {
         filterArea.Size = _sizeRoi;
     }
     status = NPPNativeMethods_Ctx.NPPi.FilterBorder32f.nppiFilterBorder32f_16f_C1R_Ctx(_devPtrRoi, _pitch, filterArea.Size, filterArea.Location, dest.DevicePointerRoi, dest.Pitch, dest.SizeRoi, pKernel.DevicePointer, nKernelSize, oAnchor, eBorderType, nppStreamCtx);
     Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nppiFilterBorder32f_16f_C1R_Ctx", status));
     NPPException.CheckNppStatus(status, this);
 }
コード例 #10
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));
        }
コード例 #11
0
        private void pictureBox_src_MouseMove(object sender, MouseEventArgs e)
        {
            Direction dir = CheckDirection(e.X, e.Y);

            if (dir == Direction.Negative)
            {
                Cursor.Current = Cursors.SizeNWSE;
            }
            else
            if (dir == Direction.Positive)
            {
                Cursor.Current = Cursors.SizeNESW;
            }
            else
            {
                Cursor.Current = Cursors.Default;
            }


            if (pressed == System.Windows.Forms.MouseButtons.Left)
            {
                NppiRect newSelection = new NppiRect(selection.x, selection.y, selection.width, selection.height);
                switch (clickedCorner)
                {
                case 1:
                    newSelection.x      = org_selection.x - (pos.X - e.X);
                    newSelection.y      = org_selection.y - (pos.Y - e.Y);
                    newSelection.width  = org_selection.width + (pos.X - e.X);
                    newSelection.height = org_selection.height + (pos.Y - e.Y);
                    break;

                case 2:
                    newSelection.y      = org_selection.y - (pos.Y - e.Y);
                    newSelection.width  = org_selection.width - (pos.X - e.X);
                    newSelection.height = org_selection.height + (pos.Y - e.Y);
                    break;

                case 3:
                    newSelection.x      = org_selection.x - (pos.X - e.X);
                    newSelection.width  = org_selection.width + (pos.X - e.X);
                    newSelection.height = org_selection.height - (pos.Y - e.Y);
                    break;

                case 4:
                    newSelection.width  = org_selection.width - (pos.X - e.X);
                    newSelection.height = org_selection.height - (pos.Y - e.Y);
                    break;
                }
                if (clickedCorner > 0)
                {
                    if (newSelection.width > 20 && newSelection.height > 20)
                    {
                        if (newSelection.x >= 0 && newSelection.y >= 0)
                        {
                            if (newSelection.x + newSelection.width <= bmp_src.Width && newSelection.y + newSelection.height <= bmp_src.Height)
                            {
                                selection = newSelection;
                                pictureBox_src.Invalidate();
                            }
                        }
                    }
                }
            }
        }
コード例 #12
0
        public static IEnumerable <RoiInputOutput> GetROIs(NppiRect inputROI, int tileSize, int borderSize)
        {
            List <RoiInputOutput> rois = new List <RoiInputOutput>();

            int tileStep = tileSize - 2 * borderSize;

            for (int y = inputROI.y - borderSize; y < inputROI.y + inputROI.height + borderSize; y += tileStep)
            {
                for (int x = inputROI.x - borderSize; x < inputROI.x + inputROI.width + borderSize; x += tileStep)
                {
                    RoiInputOutput roi = new RoiInputOutput();
                    if (x + tileSize < inputROI.width + borderSize)
                    {
                        roi.inputROI.x     = x;
                        roi.inputROI.width = tileSize;

                        roi.outputROI.x     = 0;
                        roi.outputROI.width = tileStep;

                        roi.positionInFinalImage.x     = x;
                        roi.positionInFinalImage.width = tileStep;
                    }
                    else
                    {
                        roi.inputROI.x     = inputROI.x + inputROI.width + borderSize - tileSize;
                        roi.inputROI.width = tileSize;

                        roi.outputROI.x     = x - (inputROI.x + inputROI.width + borderSize - tileSize);
                        roi.outputROI.width = inputROI.width - x;

                        roi.positionInFinalImage.x     = x;
                        roi.positionInFinalImage.width = inputROI.width - x;
                    }

                    if (y + tileSize < inputROI.height + borderSize)
                    {
                        roi.inputROI.y      = y;
                        roi.inputROI.height = tileSize;

                        roi.outputROI.y      = 0;
                        roi.outputROI.height = tileStep;

                        roi.positionInFinalImage.y      = y;
                        roi.positionInFinalImage.height = tileStep;
                    }
                    else
                    {
                        roi.inputROI.y      = inputROI.y + inputROI.height + borderSize - tileSize;
                        roi.inputROI.height = tileSize;

                        roi.outputROI.y      = y - (inputROI.y + inputROI.height + borderSize - tileSize);
                        roi.outputROI.height = inputROI.height - y;

                        roi.positionInFinalImage.y      = y;
                        roi.positionInFinalImage.height = inputROI.height - y;
                    }

                    rois.Add(roi);
                }
            }


            return(rois);
        }
コード例 #13
0
        private void btn_Process_Click(object sender, EventArgs e)
        {
            if (bmpInput == null)
            {
                return;
            }

            denoiseAndDemoisaic.LoadNetwork("epoch_" + learningRate.ToString(CultureInfo.InvariantCulture) + "_" + noiseLevelsFolders[cmb_IsoValue.SelectedIndex] + "_1999.cnn");

            if (bmpInput.PixelFormat == PixelFormat.Format32bppArgb)
            {
                inputImage8uC4.CopyToDeviceRoi(bmpInput, new NppiRect(0, 0, bmpInput.Width, bmpInput.Height));
                //Convert C4 to C3 and BGR to RGB
                inputImage8uC4.Copy(inputImage8uC1, 0);
                inputImage8uC1.Copy(inputImage8uC3, 2);
                inputImage8uC4.Copy(inputImage8uC1, 1);
                inputImage8uC1.Copy(inputImage8uC3, 1);
                inputImage8uC4.Copy(inputImage8uC1, 2);
                inputImage8uC1.Copy(inputImage8uC3, 0);
            }
            else
            {
                inputImage8uC3.CopyToDeviceRoi(bmpInput, new NppiRect(0, 0, bmpInput.Width, bmpInput.Height));
                inputImage8uC3.ColorTwist(twist);
            }

            inputImage8uC3.Convert(inputImage32f);
            inputImage32f.Div(new float[] { 255, 255, 255 });

            NppiRect oldRoi = new NppiRect(0, 0, inputImage32f.WidthRoi, inputImage32f.HeightRoi);
            IEnumerable <Tiler.RoiInputOutput> rois = Tiler.GetROIs(oldRoi, TileSize, 0);

            foreach (var roi in rois)
            {
                inputImage32f.SetRoi(roi.inputROI);
                tile.ResetRoi();
                inputImage32f.Copy(tile);
                tile.SetRoi(roi.outputROI);
                imageBayer.SetRoi(roi.positionInFinalImage);
                createBayerKernel.RunSafe(CuRandStates, tile, imageBayer, noiseLevels[cmb_IsoValue.SelectedIndex], 0);
            }
            imageBayer.SetRoi(oldRoi);
            inputImage32f.SetRoi(oldRoi);

            deBayerGreenKernel.RunSafe(imageBayer, inputImage32f, new float3(), new float3(1, 1, 1));
            deBayerRedBlueKernel.RunSafe(imageBayer, inputImage32f, new float3(), new float3(1, 1, 1));

            inputImage32f.Mul(new float[] { 255, 255, 255 }, noiseImage32f);
            noiseImage32f.Convert(noisyImage8u, NppRoundMode.Near);
            noisyImage8u.ColorTwist(twist);
            noisyImage8u.CopyToHostRoi(bmpNoisy, new NppiRect(0, 0, bmpNoisy.Width, bmpNoisy.Height));

            inputImage32f.Sub(new float[] { 0.5f, 0.5f, 0.5f });

            CudaStopWatch csw = new CudaStopWatch();

            csw.Start();

            denoiseAndDemoisaic.RunImage(inputImage32f, resultImage32f);

            csw.Stop();

            Console.WriteLine("Needed time: " + csw.GetElapsedTime() + " [msec]");
            csw.Dispose();

            resultImage32f.Add(new float[] { 0.5f, 0.5f, 0.5f });
            resultImage32f.Mul(new float[] { 255, 255, 255 });
            resultImage32f.Convert(resultImage8u, NppRoundMode.Near);
            resultImage8u.ColorTwist(twist);
            resultImage8u.SetRoi(0, 0, bmpResult.Width - 16, bmpResult.Height - 16);
            resultImage8u.CopyToHostRoi(bmpResult, new NppiRect(8, 8, bmpResult.Width - 16, bmpResult.Height - 16));

            pictureBox2.Image = bmpNoisy;
            pictureBox3.Image = bmpResult;
        }
コード例 #14
0
        static void Main(string[] args)
        {
            CudaContext                 ctx = null;
            DeBayersSubSampleKernel     kernelDeBayersSubSample    = null;
            DeBayersSubSampleDNGKernel  kernelDeBayersSubSampleDNG = null;
            SetupCurandKernel           kernelSetupCurand          = null;
            CreateBayerWithNoiseKernel  kernelCreateBayerWithNoise = null;
            CudaDeviceVariable <ushort> rawImg;

            string outputPathOwn = @"C:\Users\kunz_\Desktop\TrainingDataNN\FromOwnDataset\";
            string outputPath5k  = @"C:\Users\kunz_\Desktop\TrainingDataNN\From5kDataset\";


            const int patchSize = 66;

            //These are the noise levels I measured for each ISO of my camera:
            double[] noiseLevels        = new double[] { 6.66667E-05, 0.0001, 0.000192308, 0.000357143, 0.000714286, 0.001388889, 0.0025 };
            string[] noiseLevelsFolders = new string[] { "ISO100", "ISO200", "ISO400", "ISO800", "ISO1600", "ISO3200", "ISO6400" };


            //Process files from my own dataset:
            string[] files = File.ReadAllLines("FileListOwnImages.txt");


            if (ctx == null)
            {
                ctx = new PrimaryContext();
                ctx.SetCurrent();
                CUmodule mod = ctx.LoadModulePTX("DeBayer.ptx");
                kernelDeBayersSubSample    = new DeBayersSubSampleKernel(ctx, mod);
                kernelDeBayersSubSampleDNG = new DeBayersSubSampleDNGKernel(ctx, mod);
                kernelSetupCurand          = new SetupCurandKernel(ctx, mod);
                kernelCreateBayerWithNoise = new CreateBayerWithNoiseKernel(ctx, mod);
            }

            FileStream   fs = new FileStream("ImagesCompleted.txt", FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);

            PEFFile pef = new PEFFile(files[0]);

            BayerColor[] bayerPattern = new BayerColor[pef.BayerPattern.Length];
            for (int i = 0; i < pef.BayerPattern.Length; i++)
            {
                bayerPattern[i] = (BayerColor)pef.BayerPattern[i];
            }
            kernelDeBayersSubSample.BayerPattern = bayerPattern;



            rawImg = new CudaDeviceVariable <ushort>(pef.RawWidth * pef.RawHeight);
            NPPImage_32fC3 img                 = new NPPImage_32fC3(pef.RawWidth / 2, pef.RawHeight / 2);
            NPPImage_32fC3 imgsmall            = new NPPImage_32fC3(pef.RawWidth / 8, pef.RawHeight / 8);
            NPPImage_32fC3 patch               = new NPPImage_32fC3(patchSize, patchSize);
            NPPImage_32fC1 patchBayerWithNoise = new NPPImage_32fC1(patchSize, patchSize);
            //NPPImage_8uC3 img8u = new NPPImage_8uC3(patchSize, patchSize);
            //Bitmap bmp = new Bitmap(patchSize, patchSize, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            CudaDeviceVariable <byte> states = new CudaDeviceVariable <byte>(patchSize * patchSize * 48); //one state has the size of 48 bytes

            kernelSetupCurand.RunSafe(states, patchSize * patchSize);

            NppiRect maxRoi = new NppiRect(10, 10, pef.RawWidth / 8 - 10, pef.RawHeight / 8 - 10);

            List <NppiRect> ROIs = GetROIs(maxRoi, patchSize);

            float3[] imgGroundTruth  = new float3[patchSize * patchSize];
            float[]  noisyPatchBayer = new float[patchSize * patchSize];


            int counter = 0;

            int fileCounter = 0;

            FileStream   fsWB1 = new FileStream("WhiteBalancesOwn.txt", FileMode.Create, FileAccess.Write);
            StreamWriter swWB1 = new StreamWriter(fsWB1);

            foreach (var file in files)
            {
                pef = new PEFFile(file);

                float  whiteLevelAll = pef.WhiteLevel.Value;
                float3 whitePoint    = new float3(whiteLevelAll, whiteLevelAll, whiteLevelAll);
                float3 blackPoint    = new float3(pef.BlackPoint.Value[0], pef.BlackPoint.Value[1], pef.BlackPoint.Value[3]);
                whitePoint -= blackPoint;
                float  scale   = pef.Scaling.Value;
                float3 scaling = new float3(pef.WhitePoint.Value[0] / scale, pef.WhitePoint.Value[1] / scale, pef.WhitePoint.Value[3] / scale);

                int RoiCounter = 0;
                foreach (var roi in ROIs)
                {
                    swWB1.WriteLine((counter + RoiCounter).ToString("0000000") + "\t" + scaling.x.ToString(CultureInfo.InvariantCulture) + "\t" + scaling.y.ToString(CultureInfo.InvariantCulture) + "\t" + scaling.z.ToString(CultureInfo.InvariantCulture));

                    RoiCounter++;
                }
                fileCounter++;
                Console.WriteLine("Done " + fileCounter + " of " + files.Length);


                rawImg.CopyToDevice(pef.RawImage);
                kernelDeBayersSubSample.RunSafe(rawImg, img, (float)Math.Pow(2.0, pef.BitDepth));

                imgsmall.ResetRoi();
                img.ResizeSqrPixel(imgsmall, 0.25, 0.25, 0, 0, InterpolationMode.SuperSampling);

                RoiCounter = 0;
                foreach (var roi in ROIs)
                {
                    imgsmall.SetRoi(roi);
                    imgsmall.Copy(patch);
                    patch.CopyToHost(imgGroundTruth);
                    WriteRAWFile(outputPathOwn + @"GroundTruth\img_" + (counter + RoiCounter).ToString("0000000") + ".bin", imgGroundTruth, patchSize, patchSize);

                    RoiCounter++;
                }

                RoiCounter = 0;
                foreach (var roi in ROIs)
                {
                    imgsmall.SetRoi(roi);
                    for (int i = 0; i < 7; i++)
                    {
                        imgsmall.Copy(patch);
                        kernelCreateBayerWithNoise.RunSafe(states, patch, patchBayerWithNoise, (float)noiseLevels[i], 0);

                        patchBayerWithNoise.CopyToHost(noisyPatchBayer);
                        WriteRAWFile(outputPathOwn + noiseLevelsFolders[i] + @"\img_" + (counter + RoiCounter).ToString("0000000") + ".bin", noisyPatchBayer, patchSize, patchSize);
                    }
                    RoiCounter++;
                }
                fileCounter++;
                counter += ROIs.Count;
                Console.WriteLine("Done " + fileCounter + " of " + files.Length);
                sw.WriteLine(file);
                sw.Flush();
            }
            sw.Close();
            sw.Dispose();

            swWB1.Flush();
            swWB1.Close();


            rawImg.Dispose();
            img.Dispose();
            imgsmall.Dispose();
            patch.Dispose();
            patchBayerWithNoise.Dispose();



            //Move on to DNG images from 5k dataset:
            files = File.ReadAllLines("FileListe5KKomplett.txt");
            fs    = new FileStream("ImagesCompleted5k.txt", FileMode.Append, FileAccess.Write);
            sw    = new StreamWriter(fs);

            DNGFile dng = new DNGFile(files[0]);

            int maxWidth  = 7000;
            int maxHeight = 5000;


            rawImg              = new CudaDeviceVariable <ushort>(maxWidth * maxHeight);
            img                 = new NPPImage_32fC3(maxWidth, maxHeight); // /2
            imgsmall            = new NPPImage_32fC3(maxWidth, maxHeight); // /8
            patch               = new NPPImage_32fC3(patchSize, patchSize);
            patchBayerWithNoise = new NPPImage_32fC1(patchSize, patchSize);



            imgGroundTruth  = new float3[patchSize * patchSize];
            noisyPatchBayer = new float[patchSize * patchSize];


            counter = 0;

            fileCounter = 0;
            int roiCount = 0;

            FileStream   fsWB2 = new FileStream("WhiteBalances5k.txt", FileMode.Create, FileAccess.Write);
            StreamWriter swWB2 = new StreamWriter(fsWB2);


            foreach (var file in files)
            {
                dng = new DNGFile(file);

                bayerPattern = new BayerColor[dng.BayerPattern.Length];
                for (int i = 0; i < dng.BayerPattern.Length; i++)
                {
                    bayerPattern[i] = (BayerColor)dng.BayerPattern[i];
                }
                kernelDeBayersSubSampleDNG.BayerPattern = bayerPattern;

                maxRoi = new NppiRect(10, 10, dng.RawWidth / 8 - 10, dng.RawHeight / 8 - 10);

                ROIs      = GetROIs(maxRoi, patchSize);
                roiCount += ROIs.Count;

                float[] wb         = dng.AsShotNeutral;
                int     RoiCounter = 0;
                foreach (var roi in ROIs)
                {
                    swWB2.WriteLine((counter + RoiCounter).ToString("0000000") + "\t" + (1.0f / wb[0]).ToString(CultureInfo.InvariantCulture) + "\t" + (1.0f / wb[1]).ToString(CultureInfo.InvariantCulture) + "\t" + (1.0f / wb[2]).ToString(CultureInfo.InvariantCulture));

                    RoiCounter++;
                }
                fileCounter++;
                Console.WriteLine("Done " + fileCounter + " of " + files.Length);


                Console.WriteLine("RoiCoint: " + ROIs.Count);
                unsafe
                {
                    fixed(ushort *ptr = dng.RawImage)
                    {
                        rawImg.CopyToDevice((IntPtr)ptr, 0, 0, dng.RawWidth * dng.RawHeight * 2);
                    }
                }
                NppiRect rect = new NppiRect(0, 0, dng.RawWidth / 2, dng.RawHeight / 2);
                img.SetRoi(rect);
                kernelDeBayersSubSampleDNG.RunSafe(rawImg, img, dng.MaxVal, dng.MinVal);
                rect = new NppiRect(0, 0, dng.RawWidth / 8, dng.RawHeight / 8);
                imgsmall.SetRoi(rect);
                img.ResizeSqrPixel(imgsmall, 0.25, 0.25, 0, 0, InterpolationMode.SuperSampling);

                RoiCounter = 0;
                foreach (var roi in ROIs)
                {
                    imgsmall.SetRoi(roi);
                    imgsmall.Copy(patch);
                    patch.CopyToHost(imgGroundTruth);
                    WriteRAWFile(outputPath5k + @"GroundTruth\img_" + (counter + RoiCounter).ToString("0000000") + ".bin", imgGroundTruth, patchSize, patchSize);

                    RoiCounter++;
                }

                RoiCounter = 0;
                foreach (var roi in ROIs)
                {
                    imgsmall.SetRoi(roi);
                    for (int i = 0; i < 7; i++)
                    {
                        imgsmall.Copy(patch);
                        kernelCreateBayerWithNoise.RunSafe(states, patch, patchBayerWithNoise, (float)noiseLevels[i], 0);

                        patchBayerWithNoise.CopyToHost(noisyPatchBayer);
                        WriteRAWFile(outputPath5k + noiseLevelsFolders[i] + @"\img_" + (counter + RoiCounter).ToString("0000000") + ".bin", noisyPatchBayer, patchSize, patchSize);
                    }
                    RoiCounter++;
                }
                fileCounter++;
                counter += ROIs.Count;
                Console.WriteLine("Done " + fileCounter + " of " + files.Length);
                sw.WriteLine(file);
                sw.Flush();
            }
            sw.Close();
            sw.Dispose();

            swWB2.Flush();
            swWB2.Close();
            Console.WriteLine("Total cropped ROIs: " + roiCount);


            rawImg.Dispose();
            img.Dispose();
            imgsmall.Dispose();
            patch.Dispose();
            patchBayerWithNoise.Dispose();
            states.Dispose();

            ctx.Dispose();
        }