private ImageFileDirectory SetMembers() { //Raw Data: ImageFileDirectory raw = mIfds[0]; IFDExif exif = raw.GetEntry <IFDExif>(); mISO = exif.GetEntry <ExifISOSpeedRatings>().Value; mExposureTime = exif.GetEntry <ExifExposureTime>().Value; mRecordingDate = exif.GetEntry <ExifDateTimeDigitized>().Value; mBitDepth = raw.GetEntry <IFDBitsPerSample>().Value[0]; ExifMakerNote makernote = exif.GetEntry <ExifMakerNote>(); //needed to decompress image data: mHhuffmanTable = makernote.Value.GetEntry <MNHuffmanTable>(); ExifCFAPattern.BayerColor[] bayer = exif.GetEntry <ExifCFAPattern>().Value; mBayerPattern = new BayerColor[bayer.Length]; for (int i = 0; i < bayer.Length; i++) { mBayerPattern[i] = (BayerColor)(int)bayer[i]; } MNWhiteLevel whiteLevel = makernote.Value.GetEntry <MNWhiteLevel>(); MNWhitePoint whitePoint = makernote.Value.GetEntry <MNWhitePoint>(); MNBlackPoint blackPoint = makernote.Value.GetEntry <MNBlackPoint>(); MNDataScaling scaling = makernote.Value.GetEntry <MNDataScaling>(); float whiteLevelAll = (float)Math.Pow(2, mBitDepth); if (whiteLevel != null) { whiteLevelAll = whiteLevel.Value; } mBlackLevel = new float[3]; if (blackPoint != null) { //only one value for all colors if (blackPoint.Value.Length == 1) { mBlackLevel[0] = (float)blackPoint.Value[0]; mBlackLevel[1] = (float)blackPoint.Value[0]; mBlackLevel[2] = (float)blackPoint.Value[0]; } //values per color channel if (blackPoint.Value.Length == 3) { mBlackLevel[0] = (float)blackPoint.Value[0]; mBlackLevel[1] = (float)blackPoint.Value[1]; mBlackLevel[2] = (float)blackPoint.Value[2]; } //values per color bayer pattern if (blackPoint.Value.Length == 4) { //red int indexR = -1; for (int i = 0; i < mBayerPattern.Length; i++) { if (mBayerPattern[i] == BayerColor.Red) { indexR = i; break; } } mBlackLevel[0] = (float)blackPoint.Value[indexR]; //blue int indexB = -1; for (int i = 0; i < mBayerPattern.Length; i++) { if (mBayerPattern[i] == BayerColor.Blue) { indexB = i; break; } } mBlackLevel[2] = (float)blackPoint.Value[indexB]; //green, the two remaining indices int indexG1 = -1, indexG2 = -1; for (int i = 0; i < mBayerPattern.Length; i++) { if (mBayerPattern[i] == BayerColor.Green && indexG1 == -1) { indexG1 = i; } if (mBayerPattern[i] == BayerColor.Green && indexG1 != -1) { indexG2 = i; } } float g1 = (float)blackPoint.Value[indexG1]; float g2 = (float)blackPoint.Value[indexG2]; mBlackLevel[1] = Math.Max(g1, g2); //well, one could distinguish the two greens, but what for? } } mWhiteLevel = new float[] { whiteLevelAll, whiteLevelAll, whiteLevelAll }; mWhiteLevel[0] -= mBlackLevel[0]; mWhiteLevel[1] -= mBlackLevel[1]; mWhiteLevel[2] -= mBlackLevel[2]; float scale = scaling.Value; mWhiteBalance = new float[] { whitePoint.Value[0] / scale, whitePoint.Value[1] / scale, whitePoint.Value[3] / scale }; if (makernote.Value.GetEntry <MNLevelInfo>() != null) { mRollAngle = makernote.Value.GetEntry <MNLevelInfo>().Value.RollAngle; mRollAnglePresent = true; } mWidth = (int)raw.GetEntry <IFDImageWidth>().Value; mHeight = (int)raw.GetEntry <IFDImageLength>().Value; //look for orientation tag. if (raw.GetEntry <IFDOrientation>() != null) { mOrientation = new DNGOrientation(raw.GetEntry <IFDOrientation>().Value); } else { //no tag found, use default mOrientation = new DNGOrientation(DNGOrientation.Orientation.Normal); } //we always crop at least two pixels because of our algos... mCropLeft = 2; mCropTop = 2; mCroppedWidth = mWidth - 4; mCroppedHeight = mHeight - 4; mMake = raw.GetEntry <IFDMake>().Value; mUniqueModelName = raw.GetEntry <IFDModel>().Value; //missing data, like noise model, crop area, etc..., must be loaded afterwards! double[] colorMatrix = new double[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; mColorSpec = new DNGColorSpec(colorMatrix, colorMatrix, IFDDNGCalibrationIlluminant.Illuminant.D50, IFDDNGCalibrationIlluminant.Illuminant.D50, mWhiteBalance); return(raw); }
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 ImageFileDirectory SetMembers() { ImageFileDirectory rawIFD = null; //find the IFD with the RAW Bayer image for (int i = 0; i < mIfds.Count; i++) { //well, it should actually be somewhere in IFD0... if (mIfds[i].GetEntry <IFDPhotometricInterpretation>() != null) { if (mIfds[i].GetEntry <IFDPhotometricInterpretation>().Value == IFDPhotometricInterpretation.PhotometricInterpretation.CFA) { rawIFD = mIfds[i]; break; } } } //no root IFD seems to contain RAW bayer, search for Sub-IFDs if (rawIFD == null) { //find the IFD with the RAW Bayer image for (int i = 0; i < mIfds.Count; i++) { IFDSubIFDs subIFD = mIfds[i].GetEntry <IFDSubIFDs>(); if (subIFD == null) { continue; } for (int j = 0; j < subIFD.Value.Count; j++) { if (subIFD.Value[j].GetEntry <IFDPhotometricInterpretation>().Value == IFDPhotometricInterpretation.PhotometricInterpretation.CFA) { rawIFD = subIFD.Value[j]; break; } } } } if (rawIFD == null) { throw new ArgumentException("Can't find IFD with Bayer RAW image."); } mLinearizationTable = rawIFD.GetEntry <IFDDNGLinearizationTable>(); mWidth = (int)rawIFD.GetEntry <IFDImageWidth>().Value; mHeight = (int)rawIFD.GetEntry <IFDImageLength>().Value; if (mIfds[0].GetEntry <IFDDateTime>() != null) { mRecordingDate = mIfds[0].GetEntry <IFDDateTime>().Value; } //in case of Pentax this will have succes: IFDDNGPrivateData privateData = mIfds[0].GetEntry <IFDDNGPrivateData>(); if (privateData != null) { MNLevelInfo levelInfo = privateData.PentaxMakerNotes.GetEntry <MNLevelInfo>(); if (levelInfo != null) { mRollAngle = levelInfo.Value.RollAngle; mRollAnglePresent = true; } } IFDExif exif = mIfds[0].GetEntry <IFDExif>(); if (exif != null) { mISO = exif.GetEntry <ExifISOSpeedRatings>().Value; mExposureTime = exif.GetEntry <ExifExposureTime>().Value; mRecordingDate = (exif.GetEntry <ExifDateTimeDigitized>()?.Value).GetValueOrDefault(mRecordingDate); } else if (rawIFD.GetEntry <IFDISOSpeedRatings>() != null) { mISO = rawIFD.GetEntry <IFDISOSpeedRatings>().Value; //very likely that exposure time is also present mExposureTime = rawIFD.GetEntry <IFDExposureTime>().Value; } else if (mIfds[0].GetEntry <IFDISOSpeedRatings>() != null) { mISO = mIfds[0].GetEntry <IFDISOSpeedRatings>().Value; //very likely that exposure time is also present mExposureTime = mIfds[0].GetEntry <IFDExposureTime>().Value; } mBitDepth = rawIFD.GetEntry <IFDBitsPerSample>().Value[0]; int bayerWidth = rawIFD.GetEntry <IFDCFARepeatPatternDim>().Value[0]; int bayerHeight = rawIFD.GetEntry <IFDCFARepeatPatternDim>().Value[1]; if (bayerHeight != 2 || bayerWidth != 2) { throw new ArgumentException("This file has a bayer pattern size different than 2x2. Can't decode that."); } ExifCFAPattern.BayerColor[] bayer = rawIFD.GetEntry <IFDCFAPattern>().Value; mBayerPattern = new BayerColor[bayer.Length]; for (int i = 0; i < bayer.Length; i++) { mBayerPattern[i] = (BayerColor)(int)bayer[i]; } IFDDNGCFAPlaneColor planeColor = rawIFD.GetEntry <IFDDNGCFAPlaneColor>(); int[] planeColorHelper = new int[] { 0, 1, 2 }; if (planeColor != null) //did it ever differ from 0,1,2? { //0 = red, 1 = gree, 2 = blue. //The debayer algo creates images with plane order red/green/blue. //If this order differs, we need to re-order the planes in order to //have the color matrices correct. //reset colorTwist matrix: mColorTwist = new float[3, 4]; if (planeColor.Value.Length > 3 || planeColor.Value.Length < 3) { throw new ArgumentException("This image doesn't contain three color planes."); } for (int i = 0; i < planeColor.Value.Length; i++) { int color = planeColor.Value[i]; planeColorHelper[i] = color; if (color > 2) { throw new ArgumentException("This image contains colors different than red/green/blue."); } mColorTwist[color, i] = 1; if (color != i) { mColorTwistIsIdentity = false; } } } mColorSpec = new DNGColorSpec(3, mIfds[0], rawIFD); IFDDNGWhiteLevel whiteLevel = rawIFD.GetEntry <IFDDNGWhiteLevel>(); IFDDNGBlackLevel blackLevel = rawIFD.GetEntry <IFDDNGBlackLevel>(); mBlackLevel = new float[3]; if (blackLevel != null) { //only one value for all colors if (blackLevel.Value.Length == 1) { mBlackLevel[0] = (float)blackLevel.Value[0].Value; mBlackLevel[1] = (float)blackLevel.Value[0].Value; mBlackLevel[2] = (float)blackLevel.Value[0].Value; } //values per color channel if (blackLevel.Value.Length == 3) { mBlackLevel[planeColorHelper[0]] = (float)blackLevel.Value[0].Value; mBlackLevel[planeColorHelper[1]] = (float)blackLevel.Value[1].Value; mBlackLevel[planeColorHelper[2]] = (float)blackLevel.Value[2].Value; } //values per color bayer pattern if (blackLevel.Value.Length == 4) { //red int indexR = -1; for (int i = 0; i < mBayerPattern.Length; i++) { if (mBayerPattern[i] == BayerColor.Red) { indexR = i; break; } } mBlackLevel[0] = (float)blackLevel.Value[indexR].Value; //blue int indexB = -1; for (int i = 0; i < mBayerPattern.Length; i++) { if (mBayerPattern[i] == BayerColor.Blue) { indexB = i; break; } } mBlackLevel[2] = (float)blackLevel.Value[indexB].Value; //green, the two remaining indices int indexG1 = -1, indexG2 = -1; for (int i = 0; i < mBayerPattern.Length; i++) { if (mBayerPattern[i] == BayerColor.Green && indexG1 == -1) { indexG1 = i; } if (mBayerPattern[i] == BayerColor.Green && indexG1 != -1) { indexG2 = i; } } float g1 = (float)blackLevel.Value[indexG1].Value; float g2 = (float)blackLevel.Value[indexG2].Value; mBlackLevel[1] = Math.Max(g1, g2); //well, one could distinguish the two greens, but what for? } } mWhiteLevel = new float[3]; mWhiteLevel[0] = (float)whiteLevel.Value[0]; mWhiteLevel[1] = (float)whiteLevel.Value[0]; mWhiteLevel[2] = (float)whiteLevel.Value[0]; //subtract black level from white level mWhiteLevel[0] -= mBlackLevel[0]; mWhiteLevel[1] -= mBlackLevel[1]; mWhiteLevel[2] -= mBlackLevel[2]; //get white balance from color spec DNGVector wb = mColorSpec.CameraWhite; mWhiteBalance = new float[3]; mWhiteBalance[planeColorHelper[0]] = 1.0f / (float)wb[0]; mWhiteBalance[planeColorHelper[1]] = 1.0f / (float)wb[1]; mWhiteBalance[planeColorHelper[2]] = 1.0f / (float)wb[2]; //look for orientation tag. If RAW ifd has the tag, choose that one if (rawIFD.GetEntry <IFDOrientation>() != null) { mOrientation = new DNGOrientation(rawIFD.GetEntry <IFDOrientation>().Value); } else if (mIfds[0].GetEntry <IFDOrientation>() != null) { mOrientation = new DNGOrientation(mIfds[0].GetEntry <IFDOrientation>().Value); } else { //no tag found, use default mOrientation = new DNGOrientation(DNGOrientation.Orientation.Normal); } //default Values: int cropLeft = 0; int cropTop = 0; int croppedWidth = mWidth; int croppedHeight = mHeight; IFDDNGActiveArea activeArea = rawIFD.GetEntry <IFDDNGActiveArea>(); //if active area is defined: if (activeArea != null) { int top, left, bottom, right; top = (int)activeArea.Value[0]; left = (int)activeArea.Value[1]; bottom = (int)activeArea.Value[2]; right = (int)activeArea.Value[3]; cropLeft += left; cropTop += top; croppedWidth = right - left; croppedHeight = bottom - top; //CFA pattern is defined on active area. If top/left is uneven we need to shift the CFA pattern accordingly if (top % 2 != 0) { BayerColor bayer0 = BayerPattern[0]; BayerColor bayer1 = BayerPattern[1]; BayerPattern[0] = BayerPattern[2]; BayerPattern[1] = BayerPattern[3]; BayerPattern[2] = bayer0; BayerPattern[3] = bayer1; } if (left % 2 != 0) { BayerColor bayer0 = BayerPattern[0]; BayerColor bayer2 = BayerPattern[2]; BayerPattern[0] = BayerPattern[1]; BayerPattern[2] = BayerPattern[3]; BayerPattern[1] = bayer0; BayerPattern[3] = bayer2; } } IFDDNGDefaultCropOrigin cropOrigin = rawIFD.GetEntry <IFDDNGDefaultCropOrigin>(); IFDDNGDefaultCropSize cropSize = rawIFD.GetEntry <IFDDNGDefaultCropSize>(); if (cropOrigin != null && cropSize != null) { int top, left, width, height; left = (int)(cropOrigin.Value[0].Value); top = (int)(cropOrigin.Value[0].Value); width = (int)(cropSize.Value[0].Value); height = (int)(cropSize.Value[0].Value); cropLeft += left; cropTop += top; croppedWidth = width; croppedHeight = height; } //we always crop at least two pixels because of our algos... mCropLeft = Math.Max(2, cropLeft); mCropTop = Math.Max(2, cropTop); mCroppedWidth = croppedWidth - Math.Max(0, (cropLeft + croppedWidth) - (mWidth - 2)); mCroppedHeight = croppedHeight - Math.Max(0, (cropTop + croppedHeight) - (mHeight - 2)); IFDDNGNoiseProfile noise = rawIFD.GetEntry <IFDDNGNoiseProfile>(); if (noise == null) { noise = mIfds[0].GetEntry <IFDDNGNoiseProfile>(); } if (noise != null) { //if noise level is given for all channels, //take the green one as it is usually scalled to one if (noise.Value.Length > 2) { mNoiseModelAlpha = (float)noise.Value[planeColorHelper[1] * 2]; mNoiseModelBeta = (float)noise.Value[planeColorHelper[1] * 2 + 1]; } else { mNoiseModelAlpha = (float)noise.Value[0]; mNoiseModelBeta = (float)noise.Value[1]; } } mMake = mIfds[0].GetEntry <IFDMake>().Value; mUniqueModelName = mIfds[0].GetEntry <IFDDNGUniqueCameraModel>().Value; return(rawIFD); }
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(); }