示例#1
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);
        }
示例#2
0
        private void Btn_ProcessPEF_Click(object sender, EventArgs e)
        {
            if (pef == null)
            {
                return;
            }

            denoiseAndDemoisaic.LoadNetwork("epoch_" + learningRate.ToString(CultureInfo.InvariantCulture) + "_" + noiseLevelsFolders[cmb_IsoValue.SelectedIndex] + "_1999.cnn");
            NPPImage_16uC1 rawTemp = new NPPImage_16uC1(pef.RawWidth, pef.RawHeight);

            rawTemp.CopyToDevice(pef.RawImage);
            rawTemp.Convert(imageBayer);

            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);


            inputImage32f.Set(new float[] { 0, 0, 0 });
            deBayerGreenKernel.RunSafe(imageBayer, inputImage32f, blackPoint, scaling);
            deBayerRedBlueKernel.RunSafe(imageBayer, inputImage32f, blackPoint, scaling);
            inputImage32f.Div(new float[] { whitePoint.x *scaling.x, whitePoint.y *scaling.y, whitePoint.z *scaling.z });



            highlightRecoveryKernel.RunSafe(inputImage32f, new float3(scaling.x, scaling.y, scaling.z), 1);

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

            Console.WriteLine("Start denoising...");
            CudaStopWatch csw = new CudaStopWatch();

            csw.Start();
            denoiseAndDemoisaic.RunImage(noiseImage32f, resultImage32f);
            csw.Stop();
            Console.WriteLine("Needed time: " + csw.GetElapsedTime() + " [msec]");
            csw.Dispose();

            resultImage32f.Add(new float[] { 0.5f, 0.5f, 0.5f });

            ColorManagment cm = new ColorManagment();

            float3  wp        = 1.0f / scaling;
            double3 wb        = new double3(wp.x, wp.y, wp.z);
            double2 neutralXY = cm.NeutralToXY(wb);

            cm.SetWhiteXY(neutralXY);
            ColorMatrix camToXYZ2 = cm.CameraToPCS;


            ColorMatrix d50Tod65  = new ColorMatrix(new double[] { 0.9555766, -0.0230393, 0.0631636, -0.0282895, 1.0099416, 0.0210077, 0.0122982, -0.0204830, 1.3299098 });
            ColorMatrix d65TosRGB = new ColorMatrix(new double[] { 3.2406, -1.5372, -0.4986, -0.9689, 1.8758, 0.0415, 0.0557, -0.2040, 1.0570 });
            ColorMatrix final     = d65TosRGB * d50Tod65 * camToXYZ2;

            float[] matData = new float[9];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    matData[j + i * 3] = (float)final[i, j];
                }
            }

            camToXYZKernel.RunSafe(inputImage32f, matData);
            camToXYZKernel.RunSafe(resultImage32f, matData);

            //This is a LUT that maps well to most of the JPEGs out of camera, but not always... found somewhere on internet, if I remember well from darktable?
            float[] x = new float[] { 0,
                                      0.004754f,
                                      0.009529f,
                                      0.023713f,
                                      0.031866f,
                                      0.046734f,
                                      0.059989f,
                                      0.088415f,
                                      0.13661f,
                                      0.17448f,
                                      0.205192f,
                                      0.228896f,
                                      0.286411f,
                                      0.355314f,
                                      0.440014f,
                                      0.567096f,
                                      0.620597f,
                                      0.760355f,
                                      0.875139f,
                                      1 };
            float[] y = new float[] { 0,
                                      0.002208f,
                                      0.004214f,
                                      0.013508f,
                                      0.020352f,
                                      0.034063f,
                                      0.052413f,
                                      0.09603f,
                                      0.190629f,
                                      0.256484f,
                                      0.30743f,
                                      0.348447f,
                                      0.42868f,
                                      0.513527f,
                                      0.607651f,
                                      0.732791f,
                                      0.775968f,
                                      0.881828f,
                                      0.960682f,
                                      1 };

            CudaDeviceVariable <float> d_x = x;
            CudaDeviceVariable <float> d_y = y;

            inputImage32f.LUTCubic(new CudaDeviceVariable <float>[] { d_y, d_y, d_y }, new CudaDeviceVariable <float>[] { d_x, d_x, d_x });
            resultImage32f.LUTCubic(new CudaDeviceVariable <float>[] { d_y, d_y, d_y }, new CudaDeviceVariable <float>[] { d_x, d_x, d_x });

            convertRGBTosRGBKernel.RunSafe(inputImage32f);
            convertRGBTosRGBKernel.RunSafe(resultImage32f);

            inputImage32f.Convert(noisyImage8u, NppRoundMode.Near);
            resultImage32f.Convert(resultImage8u, NppRoundMode.Near);

            noisyImage8u.SetRoi(0, 0, bmpNoisy.Width - 4, bmpNoisy.Height - 4);
            noisyImage8u.CopyToHostRoi(bmpNoisy, new NppiRect(2, 2, bmpNoisy.Width - 4, bmpNoisy.Height - 4));

            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;

            rawTemp.Dispose();
            d_y.Dispose();
            d_x.Dispose();
        }