public TestRawFile(string dummyFile, int size, float preShiftX, float preShiftY, float preRotDeg, float shiftX, float shiftY) : base(dummyFile) { //close the file Close(); Random rand = new Random(0); mRawImage = new ushort[size, size]; float[] temp = new float[size * size]; NPPImage_32fC1 img1 = new NPPImage_32fC1(size, size); NPPImage_32fC1 img2 = new NPPImage_32fC1(size, size); NPPImage_16uC1 img16u = new NPPImage_16uC1(size, size); for (int i = 0; i < temp.Length; i++) { temp[i] = (float)rand.NextDouble(); } img1.CopyToDevice(temp); img1.FilterGaussBorder(img2, MaskSize.Size_5_X_5, NppiBorderType.Replicate); img1.Set(0); img2.WarpAffine(img1, Matrix3x3.ShiftAffine(-preShiftX, -preShiftY).ToAffine(), InterpolationMode.Cubic); img2.Set(0); img1.WarpAffine(img2, Matrix3x3.RotAroundCenter(-preRotDeg, size, size).ToAffine(), InterpolationMode.Cubic); img1.Set(0); img2.WarpAffine(img1, Matrix3x3.ShiftAffine(-shiftX, -shiftY).ToAffine(), InterpolationMode.Cubic); img1.Mul(65535); img1.Convert(img16u, NppRoundMode.Near); img16u.CopyToHost(mRawImage, size * sizeof(ushort)); double[] colorMatrix = new double[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; mColorSpec = new PentaxPefFile.DNGColorSpec(colorMatrix, colorMatrix, PentaxPefFile.IFDDNGCalibrationIlluminant.Illuminant.D50, PentaxPefFile.IFDDNGCalibrationIlluminant.Illuminant.D65, new float[] { 1f, 1, 1f }); mOrientation = new PentaxPefFile.DNGOrientation(PentaxPefFile.DNGOrientation.Orientation.Normal); mWidth = size; mHeight = size; mCropLeft = 0; mCropTop = 0; mCroppedWidth = size; mCroppedHeight = size; mBitDepth = 16; mISO = 100; mBayerPattern = new BayerColor[] { BayerColor.Red, BayerColor.Cyan, BayerColor.Blue }; mWhiteLevel = new float[] { 65535, 65535, 65535 }; mBlackLevel = new float[] { 0, 0, 0 }; mWhiteBalance = new float[] { 1, 1, 1 };; mRollAngle = 0; mRollAnglePresent = false; mNoiseModelAlpha = float.Epsilon; mNoiseModelBeta = 0; mExposureTime = new PentaxPefFile.Rational(1, 1); mRecordingDate = DateTime.Now; mMake = "None"; mUniqueModelName = "Test file"; img1.Dispose(); img2.Dispose(); img16u.Dispose(); }
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(); }