/// <summary> /// Image copy. /// </summary> /// <param name="dst">Destination image</param> /// <param name="channel">Channel number. This number is added to the dst pointer</param> public void Copy(NPPImage_32fC1 dst, int channel) { if (channel < 0 | channel >= _channels) throw new ArgumentOutOfRangeException("channel", "channel must be in range [0..1]."); status = NPPNativeMethods.NPPi.MemCopy.nppiCopy_32f_C2C1R(_devPtrRoi + channel * _typeSize, _pitch, dst.DevicePointerRoi, dst.Pitch, _sizeRoi); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nppiCopy_32f_C2C1R", status)); NPPException.CheckNppStatus(status, this); }
/// <summary> /// Image copy. /// </summary> /// <param name="dst">Destination image</param> /// <param name="channel">Channel indicator (real or imaginary part of the complex number)</param> public void Copy(NPPImage_32fC1 dst, ComplexChannel channel) { int c = (int)channel; //typesize is sizeof(float2), so the entire complex number, use sizeof(float) instead! status = NPPNativeMethods.NPPi.MemCopy.nppiCopy_32f_C2C1R(_devPtrRoi + c * sizeof(float), _pitch, dst.DevicePointerRoi, dst.Pitch, _sizeRoi); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nppiCopy_32f_C2C1R", status)); NPPException.CheckNppStatus(status, this); }
/// <summary> /// Graphcut of a flow network (32bit floating point edge capacities). The /// function computes the minimal cut (graphcut) of a 2D regular 4-connected /// graph. <para/> /// The inputs are the capacities of the horizontal (in transposed form), /// vertical and terminal (source and sink) edges. The capacities to source and /// sink /// are stored as capacity differences in the terminals array /// ( terminals(x) = source(x) - sink(x) ). The implementation assumes that the /// edge capacities /// for boundary edges that would connect to nodes outside the specified domain /// are set to 0 (for example left(0,*) == 0). If this is not fulfilled the /// computed labeling may be wrong!<para/> /// The computed binary labeling is encoded as unsigned 8bit values (0 and >0). /// </summary> /// <param name="Terminals">Pointer to differences of terminal edge capacities</param> /// <param name="LeftTransposed">Pointer to transposed left edge capacities</param> /// <param name="RightTransposed">Pointer to transposed right edge capacities</param> /// <param name="Top">Pointer to top edge capacities (top(*,0) must be 0)</param> /// <param name="Bottom">Pointer to bottom edge capacities (bottom(*,height-1)</param> /// <param name="Label">Pointer to destination label image </param> /// <returns></returns> public void GraphCut(NPPImage_32fC1 Terminals, NPPImage_32fC1 LeftTransposed, NPPImage_32fC1 RightTransposed, NPPImage_32fC1 Top, NPPImage_32fC1 Bottom, NPPImage_8uC1 Label) { status = NPPNativeMethods.NPPi.ImageLabeling.nppiGraphcut_32f8u(Terminals.DevicePointer, LeftTransposed.DevicePointer, RightTransposed.DevicePointer, Top.DevicePointer, Bottom.DevicePointer, Terminals.Pitch, LeftTransposed.Pitch, _size, Label.DevicePointer, Label.Pitch, _state); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nppiGraphcut_32f8u", status)); NPPException.CheckNppStatus(status, this); }
public float RunSafe(NPPImage_32fC2 shifts, NPPImage_32fC1 imFx, NPPImage_32fC1 imFy, NPPImage_32fC1 imFt, float minDet, int windowSize) { this.BlockDimensions = new dim3(32, 16, 1); this.SetComputeSize((uint)(shifts.WidthRoi), (uint)(shifts.HeightRoi), 1); //this.DynamicSharedMemory = (uint)(5 * windowSize * windowSize) * BlockDimensions.x * BlockDimensions.y * sizeof(float); int windowSizeHalf = windowSize / 2; return(this.Run(shifts.DevicePointerRoi, imFx.DevicePointerRoi, imFy.DevicePointerRoi, imFt.DevicePointerRoi, shifts.Pitch, imFx.Pitch, shifts.WidthRoi, shifts.HeightRoi, windowSizeHalf, minDet)); }
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)); }
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); } }
public void FourierFilter(NPPImage_32fC1 img, int clearAxis, float aHighPass, float aHighPassSigma) { forward.Exec(img.DevicePointerRoi, imgToTrackCplx.DevicePointer); fourierFilterKernel.RunSafe(imgToTrackCplx, width, height, clearAxis, 1, aHighPass, 1, aHighPassSigma); backward.Exec(imgToTrackCplx.DevicePointer, img.DevicePointerRoi); img.Div(img.WidthRoi * img.HeightRoi); CudaDeviceVariable <float> minVal = new CudaDeviceVariable <float>(x.DevicePointer, 4); CudaDeviceVariable <float> maxVal = new CudaDeviceVariable <float>(y.DevicePointer, 4); img.MinMax(minVal, maxVal, buffer); float min = minVal; float max = maxVal; img.ThresholdLTGT(0, 0, 1, 1); }
//int width, int height, int stride, //cudaTextureObject_t texUV, float* __restrict__ out, cudaTextureObject_t texToWarp public float RunSafe(NPPImage_32fC1 inImg, NPPImage_32fC1 outImg, NPPImage_32fC2 flow) { this.BlockDimensions = new dim3(32, 6, 1); this.SetComputeSize((uint)(outImg.WidthRoi), (uint)(outImg.HeightRoi), 1); CudaResourceDesc descImg = new CudaResourceDesc(inImg); CudaTextureDescriptor texDescImg = new CudaTextureDescriptor(CUAddressMode.Mirror, CUFilterMode.Linear, CUTexRefSetFlags.NormalizedCoordinates); CudaTexObject texImg = new CudaTexObject(descImg, texDescImg); CudaResourceDesc descFlow = new CudaResourceDesc(flow); CudaTextureDescriptor texDescFlow = new CudaTextureDescriptor(CUAddressMode.Clamp, CUFilterMode.Point, CUTexRefSetFlags.NormalizedCoordinates); CudaTexObject texFlow = new CudaTexObject(descFlow, texDescFlow); return(this.Run(outImg.WidthRoi, outImg.HeightRoi, outImg.Pitch, texFlow.TexObject, outImg.DevicePointerRoi, texImg.TexObject)); }
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); }
public float RunSafe(NPPImage_32fC1 imgSource, NPPImage_32fC1 Ix, NPPImage_32fC1 Iy) { this.BlockDimensions = new dim3(32, 6, 1); this.SetComputeSize((uint)(imgSource.WidthRoi), (uint)(imgSource.HeightRoi), 1); CudaResourceDesc descImgSource = new CudaResourceDesc(imgSource); CudaTextureDescriptor texDescImgSource = new CudaTextureDescriptor(CUAddressMode.Mirror, CUFilterMode.Linear, CUTexRefSetFlags.NormalizedCoordinates); CudaTexObject texImgSource = new CudaTexObject(descImgSource, texDescImgSource); float t = this.Run(Ix.WidthRoi, Ix.HeightRoi, Ix.Pitch, Ix.DevicePointerRoi, Iy.DevicePointerRoi, texImgSource.TexObject); texImgSource.Dispose(); return(t); }
public PreAlignment(NPPImage_32fC1 img, CudaContext ctx) { width = img.WidthRoi; height = img.HeightRoi; imgToTrackRotated = new NPPImage_32fC1(width, height); CUmodule mod = ctx.LoadModule("kernel.ptx"); int fftWidth = width / 2 + 1; conjKernel = new conjugateComplexMulKernel(ctx, mod); fourierFilterKernel = new fourierFilterKernel(ctx, mod); fftshiftKernel = new fftshiftKernel(ctx, mod); squaredSumKernel = new squaredSumKernel(ctx, mod); boxFilterXKernel = new boxFilterWithBorderXKernel(ctx, mod); boxFilterYKernel = new boxFilterWithBorderYKernel(ctx, mod); normalizedCCKernel = new normalizedCCKernel(ctx, mod); findMinimumKernel = new findMinimumKernel(ctx, mod); int n = 2; int[] dims = new int[] { height, width }; int batches = 1; int[] inembed = new int[] { 1, imgToTrackRotated.Pitch / 4 }; int[] onembed = new int[] { 1, fftWidth }; int idist = height * imgToTrackRotated.Pitch / 4; int odist = height * fftWidth; int istride = 1; int ostride = 1; cufftHandle handleForward = cufftHandle.Create(); cufftHandle handleBackward = cufftHandle.Create(); SizeT sizeForward = new SizeT(); SizeT sizeBackward = new SizeT(); forward = new CudaFFTPlanMany(handleForward, n, dims, batches, cufftType.R2C, inembed, istride, idist, onembed, ostride, odist, ref sizeForward, false); backward = new CudaFFTPlanMany(handleBackward, n, dims, batches, cufftType.C2R, onembed, ostride, odist, inembed, istride, idist, ref sizeBackward, false); FFTBufferSize = sizeForward > sizeBackward ? sizeForward : sizeBackward; }
public void LucasKanade(NPPImage_32fC1 sourceImg, NPPImage_32fC1 targetImg, NPPImage_32fC2 tiledFlow, int tileSize, int tileCountX, int tileCountY, int iterations, float2 baseShift, float baseRotation, float minDet, int windowSize) { createFlowFieldFromTiles.RunSafe(tiledFlow, d_flow, baseShift, baseRotation, tileSize, tileCountX, tileCountY); for (int iter = 0; iter < iterations; iter++) { warpingKernel.RunSafe(sourceImg, d_tmp, d_flow); NppiPoint p = new NppiPoint(0, 0); d_Ix.Set(0); d_Iy.Set(0); d_Iz.Set(0); computeDerivativesKernel.RunSafe(d_tmp, targetImg, d_Ix, d_Iy, d_Iz); lukasKanade.RunSafe(d_flow, d_Ix, d_Iy, d_Iz, minDet, windowSize); } warpingKernel.RunSafe(sourceImg, d_tmp, d_flow); d_tmp.Copy(sourceImg); }
public void Track(NPPImage_32fC1 imgTrack, NPPImage_32fC1 imgRef, NPPImage_32fC2 preShift, int i, float2 baseShiftRef, float baseRotationRef, float2 baseShifttoTrack, float baseRotationtoTrack, float threshold) { if (imgTrack.WidthRoi != imgRef.WidthRoi || imgTrack.HeightRoi != imgRef.HeightRoi || imgTrack.WidthRoi != currentWidth || imgTrack.HeightRoi != currentHeight) { throw new ArgumentOutOfRangeException(); } int level = imgTrack.Width / imgTrack.WidthRoi; convertToTilesBorder.RunSafe(imgRef, imgRefSortedTiles, currentTileSize, currentMaxShift, CurrentBlockCountX, CurrentBlockCountY, baseShiftRef, baseRotationRef); //template forward[i].Exec(imgRefSortedTiles.DevicePointer, imgRefCplx.DevicePointer); convertToTiles.RunSafe(imgTrack, imgToTrackSortedTiles, preShift, currentTileSize, currentMaxShift, CurrentBlockCountX, CurrentBlockCountY, baseShifttoTrack, baseRotationtoTrack); //image in paper //DumpFloat(imgToTrackSortedTiles, currentTileSize + 2* currentMaxShift, currentTileSize + 2 * currentMaxShift, CurrentBlockCountX * CurrentBlockCountY, tileIdx, "tilesTrack_" + level + "_" + debugCallCounter + ".bin"); //DumpFloat(imgRefSortedTiles, currentTileSize + 2 * currentMaxShift, currentTileSize + 2 * currentMaxShift, CurrentBlockCountX * CurrentBlockCountY, tileIdx, "tilesRef_" + level + "_" + debugCallCounter + ".bin"); forward[i].Exec(imgToTrackSortedTiles.DevicePointer, imgToTrackCplx.DevicePointer); conjKernel.RunSafe(imgRefCplx, imgToTrackCplx); backward[i].Exec(imgToTrackCplx.DevicePointer, imgCrossCorrelation.DevicePointer); imgCrossCorrelation.DivC(CurrentBlockSize * CurrentBlockSize); squaredSumKernel.RunSafe(imgRefSortedTiles, squaredSumsOfTiles, currentMaxShift, currentTileSize, CurrentBlockCountX * CurrentBlockCountY); //DumpFloat(squaredSumsOfTiles, 1, 1, CurrentBlockCountX * CurrentBlockCountY, tileIdx, "squaredSums_" + level + "_" + debugCallCounter + ".bin"); boxFilterXKernel.RunSafe(imgToTrackSortedTiles, imgRefSortedTiles, currentMaxShift, currentTileSize, CurrentBlockCountX * CurrentBlockCountY); boxFilterYKernel.RunSafe(imgRefSortedTiles, imgToTrackSortedTiles, currentMaxShift, currentTileSize, CurrentBlockCountX * CurrentBlockCountY); //DumpFloat(imgToTrackSortedTiles, currentTileSize + 2 * currentMaxShift, currentTileSize + 2 * currentMaxShift, CurrentBlockCountX * CurrentBlockCountY, tileIdx, "boxFilter_" + level + "_" + debugCallCounter + ".bin"); normalizedCCKernel.RunSafe(imgCrossCorrelation, squaredSumsOfTiles, imgToTrackSortedTiles, shiftImages, currentMaxShift, currentTileSize, CurrentBlockCountX * CurrentBlockCountY); //DumpFloat(shiftImages, (2 * currentMaxShift + 1), (2 * currentMaxShift + 1), CurrentBlockCountX * CurrentBlockCountY, tileIdx, "tilesShift_" + level + "_" + debugCallCounter + ".bin"); patchShift.SetRoi(0, 0, CurrentBlockCountX, CurrentBlockCountY); findMinimumKernel.RunSafe(shiftImages, patchShift, currentMaxShift, CurrentBlockCountX, CurrentBlockCountY, threshold); NPPImage_32fC1 preShiftFloat = new NPPImage_32fC1(preShift.DevicePointer, 2 * CurrentBlockCountX, CurrentBlockCountY, preShift.Pitch); NPPImage_32fC1 patchShiftFloat = new NPPImage_32fC1(patchShift.DevicePointer, 2 * CurrentBlockCountX, CurrentBlockCountY, patchShift.Pitch); preShiftFloat.Add(patchShiftFloat); debugCallCounter++; }
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); }
private void DumpImage(NPPImage_32fC1 img, string filename) { float[] f = new float[img.Width * img.Height]; img.CopyToHost(f); FileStream fs = File.OpenWrite(filename); BinaryWriter bw = new BinaryWriter(fs); bw.Write(img.Width); bw.Write(img.Height); for (int i = 0; i < f.Length; i++) { bw.Write(f[i]); } bw.Close(); fs.Close(); bw.Dispose(); fs.Dispose(); }
public (int, int) TestCC(NPPImage_32fC1 img, int shiftX, int shiftY) { Matrix3x3 mat = Matrix3x3.ShiftAffine(shiftX, shiftY); img.WarpAffine(imgToTrackRotated, mat.ToAffine(), InterpolationMode.NearestNeighbor); forward.Exec(imgToTrackRotated.DevicePointerRoi, imgToTrackCplx.DevicePointer); conjKernel.RunSafe(imgRefCplx, imgToTrackCplx); fftshiftKernel.RunSafe(imgToTrackCplx, width, height); backward.Exec(imgToTrackCplx.DevicePointer, img.DevicePointerRoi); img.Div(img.WidthRoi * img.HeightRoi); img.MaxIndex(val, x, y); float v = val; int hx = x; int hy = y; hx -= imgToTrackRotated.WidthRoi / 2; hy -= imgToTrackRotated.HeightRoi / 2; CudaDeviceVariable <float> minVal = new CudaDeviceVariable <float>(x.DevicePointer, 4); CudaDeviceVariable <float> maxVal = new CudaDeviceVariable <float>(y.DevicePointer, 4); img.MinMax(minVal, maxVal, buffer); float min = minVal; float max = maxVal; img.Sub(min); img.Div(max - min); img.ThresholdLTGT(0, 0, 1, 1); return(hx, hy); }
public OpticalFlow(int width, int height, CudaContext ctx) { CUmodule mod = ctx.LoadModulePTX("opticalFlow.ptx"); warpingKernel = new WarpingKernel(ctx, mod); createFlowFieldFromTiles = new CreateFlowFieldFromTiles(ctx, mod); computeDerivativesKernel = new ComputeDerivativesKernel(ctx, mod); lukasKanade = new LukasKanadeKernel(ctx, mod); d_tmp = new NPPImage_32fC1(width, height); d_Ix = new NPPImage_32fC1(width, height); d_Iy = new NPPImage_32fC1(width, height); d_Iz = new NPPImage_32fC1(width, height); d_flow = new NPPImage_32fC2(width, height); buffer = new CudaDeviceVariable <byte>(d_tmp.MeanStdDevGetBufferHostSize() * 3); mean = new CudaDeviceVariable <double>(1); std = new CudaDeviceVariable <double>(1); d_filterX = new float[] { -0.25f, 0.25f, -0.25f, 0.25f }; d_filterY = new float[] { -0.25f, -0.25f, 0.25f, 0.25f }; d_filterT = new float[] { 0.25f, 0.25f, 0.25f, 0.25f }; }
public float RunSafe(NPPImage_32fC1 imgIn, NPPImage_32fC3 imgOut, float3 blackPoint, float3 scale) { //const int width, const int height, const float* __restrict__ imgIn, int strideIn, float3 *outImage, int strideOut, float3 blackPoint, float3 scale SetComputeSize((uint)imgIn.Width, (uint)imgIn.Height, 1); return(base.Run(imgIn.Width, imgIn.Height, imgIn.DevicePointer, imgIn.Pitch, imgOut.DevicePointer, imgOut.Pitch, blackPoint, scale)); }
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(); }
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)); }
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(); }
public float RunSafe(CudaDeviceVariable <byte> states, NPPImage_32fC3 imgIn, NPPImage_32fC1 imgOut, float alpha, float beta) { SetComputeSize((uint)imgOut.WidthRoi, (uint)imgOut.HeightRoi); return(base.Run(states.DevicePointer, imgIn.DevicePointerRoi, imgOut.DevicePointerRoi, imgOut.WidthRoi, imgOut.HeightRoi, imgIn.Pitch, imgOut.Pitch, alpha, beta)); }
public float RunSafe(NPPImage_32fC3 imgIn, NPPImage_32fC1 imgOut) { SetComputeSize((uint)imgIn.Width, (uint)imgIn.Height, 1); return(base.Run(imgIn.DevicePointer, imgOut.DevicePointer, imgIn.Width, imgIn.Height, imgIn.Pitch, imgOut.Pitch)); }
/// <summary> /// 16-bit floating point to 32-bit conversion. /// </summary> /// <param name="dst">Destination image</param> /// <param name="nppStreamCtx">NPP stream context.</param> public void Convert(NPPImage_32fC1 dst, NppStreamContext nppStreamCtx) { status = NPPNativeMethods_Ctx.NPPi.BitDepthConversion.nppiConvert_16f32f_C1R_Ctx(_devPtrRoi, _pitch, dst.DevicePointerRoi, dst.Pitch, _sizeRoi, nppStreamCtx); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nppiConvert_16f32f_C1R_Ctx", status)); NPPException.CheckNppStatus(status, this); }
public float RunSafe(NPPImage_32fC1 imgDx, NPPImage_32fC1 imgDy, NPPImage_32fC3 outImg) { this.SetComputeSize((uint)(outImg.WidthRoi), (uint)(outImg.HeightRoi), 1); return(this.Run(imgDx.DevicePointerRoi, imgDy.DevicePointerRoi, outImg.DevicePointerRoi, outImg.WidthRoi, outImg.HeightRoi, imgDx.Pitch, outImg.Pitch)); }
/// <summary> /// 32-bit floating point complex to 32-bit floating point squared magnitude. /// <para/> /// Converts complex-number pixel image to single channel image computing /// the result pixels as the squared magnitude of the complex values. /// <para/> /// The squared magnitude is an itermediate result of magnitude computation and /// can thus be computed faster than actual magnitude. If magnitudes are required /// for sorting/comparing only, using this function instead of nppiMagnitude_32fc32f_C1R /// can be a worthwhile performance optimization. /// </summary> /// <param name="dest">Destination image</param> public void MagnitudeSqr(NPPImage_32fC1 dest) { status = NPPNativeMethods.NPPi.LinearTransforms.nppiMagnitudeSqr_32fc32f_C1R(_devPtrRoi, _pitch, dest.DevicePointerRoi, dest.Pitch, _sizeRoi); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "nppiMagnitudeSqr_32fc32f_C1R", status)); NPPException.CheckNppStatus(status, this); }
public float RunSafe(NPPImage_32fC1 inImg, CudaDeviceVariable <float> outTiles, int tileSize, int maxShift, int tileCountX, int tileCountY, float2 baseShift, float baseRotation) { this.SetComputeSize((uint)(tileSize + 2 * maxShift), (uint)(tileSize + 2 * maxShift), (uint)(tileCountX * tileCountY)); return(this.Run(inImg.DevicePointerRoi, outTiles.DevicePointer, inImg.WidthRoi, inImg.HeightRoi, inImg.Pitch, maxShift, tileSize, tileCountX, tileCountY, baseShift, baseRotation)); }