public static float[] FitAndGeneratePlane(float[] intensities, int2 dims) { float3[] Points = new float3[dims.Elements()]; for (int y = 0; y < dims.Y; y++) { for (int x = 0; x < dims.X; x++) { Points[y * dims.X + x] = new float3(x, y, intensities[y * dims.X + x]); } } float3 Plane = FitPlane(Points); float[] Result = new float[dims.Elements()]; for (int y = 0; y < dims.Y; y++) { for (int x = 0; x < dims.X; x++) { Result[y * dims.X + x] = x * Plane.X + y * Plane.Y + Plane.Z; } } return Result; }
public static int3[] GetEqualGridSpacing(int2 dimsImage, int2 dimsRegion, float overlapFraction, out int2 dimsGrid) { int2 dimsoverlap = new int2((int)(dimsRegion.X * (1.0f - overlapFraction)), (int)(dimsRegion.Y * (1.0f - overlapFraction))); dimsGrid = new int2(MathHelper.NextMultipleOf(dimsImage.X - (dimsRegion.X - dimsoverlap.X), dimsoverlap.X) / dimsoverlap.X, MathHelper.NextMultipleOf(dimsImage.Y - (dimsRegion.Y - dimsoverlap.Y), dimsoverlap.Y) / dimsoverlap.Y); int2 shift; shift.X = dimsGrid.X > 1 ? (int)((dimsImage.X - dimsRegion.X) / (float)(dimsGrid.X - 1)) : (dimsImage.X - dimsRegion.X) / 2; shift.Y = dimsGrid.Y > 1 ? (int)((dimsImage.Y - dimsRegion.Y) / (float)(dimsGrid.Y - 1)) : (dimsImage.Y - dimsRegion.Y) / 2; int2 offset = new int2((dimsImage.X - shift.X * (dimsGrid.X - 1) - dimsRegion.X) / 2, (dimsImage.Y - shift.Y * (dimsGrid.Y - 1) - dimsRegion.Y) / 2); int3[] h_origins = new int3[dimsGrid.Elements()]; for (int y = 0; y < dimsGrid.Y; y++) { for (int x = 0; x < dimsGrid.X; x++) { h_origins[y * dimsGrid.X + x] = new int3(x * shift.X + offset.X, y * shift.Y + offset.Y, 0); } } return(h_origins); }
public static void FitAndSubtractGrid(float[] intensities, int2 dims, int2 gridDims) { float2 GridSpacing = new float2(dims) / new float2(gridDims + 1); float2[] GridCentroids = Helper.Combine(Helper.ArrayOfFunction(y => Helper.ArrayOfFunction(x => new float2((x + 1) * GridSpacing.X, (y + 1) * GridSpacing.Y), gridDims.X), gridDims.Y)); float3[] Planes = new float3[gridDims.Elements()]; Parallel.For(0, gridDims.Elements(), ci => { float2 Centroid = GridCentroids[ci]; int2 PositionStart = int2.Max(new int2(Centroid - GridSpacing), 0); int2 PositionEnd = int2.Min(new int2(Centroid + GridSpacing + 1), dims); int2 PatchDims = PositionEnd - PositionStart; float3[] Points = new float3[PatchDims.Elements()]; for (int y = 0; y < PatchDims.Y; y++) { for (int x = 0; x < PatchDims.X; x++) { Points[y * PatchDims.X + x] = new float3(x - GridSpacing.X, y - GridSpacing.Y, intensities[(y + PositionStart.Y) * dims.X + x + PositionStart.X]); } } Planes[ci] = FitPlane(Points); }); float[] FittedGrid = new float[dims.Elements()]; Parallel.For(0, dims.Y, y => { for (int x = 0; x < dims.X; x++) { int2 Centroid0 = int2.Min(int2.Max(new int2((new float2(x, y) - GridSpacing) / GridSpacing), 0), gridDims - 1); int CentroidID00 = gridDims.ElementFromPosition(Centroid0); int CentroidID01 = gridDims.ElementFromPosition(int2.Min(Centroid0 + new int2(1, 0), gridDims - 1)); int CentroidID10 = gridDims.ElementFromPosition(int2.Min(Centroid0 + new int2(0, 1), gridDims - 1)); int CentroidID11 = gridDims.ElementFromPosition(int2.Min(Centroid0 + new int2(1, 1), gridDims - 1)); float2 Centroid00 = GridCentroids[CentroidID00]; float2 Centroid01 = GridCentroids[CentroidID01]; float2 Centroid10 = GridCentroids[CentroidID10]; float2 Centroid11 = GridCentroids[CentroidID11]; float Interp00 = (x - Centroid00.X) * Planes[CentroidID00].X + (y - Centroid00.Y) * Planes[CentroidID00].Y + Planes[CentroidID00].Z; float Interp01 = (x - Centroid01.X) * Planes[CentroidID01].X + (y - Centroid01.Y) * Planes[CentroidID01].Y + Planes[CentroidID01].Z; float Interp10 = (x - Centroid10.X) * Planes[CentroidID10].X + (y - Centroid10.Y) * Planes[CentroidID10].Y + Planes[CentroidID10].Z; float Interp11 = (x - Centroid11.X) * Planes[CentroidID11].X + (y - Centroid11.Y) * Planes[CentroidID11].Y + Planes[CentroidID11].Z; float fX = Math.Max(0, Math.Min(1, (x - Centroid00.X) / GridSpacing.X)); float fY = Math.Max(0, Math.Min(1, (y - Centroid00.Y) / GridSpacing.Y)); float Interp0 = Interp00 * (1 - fX) + Interp01 * fX; float Interp1 = Interp10 * (1 - fX) + Interp11 * fX; float Interp = Interp0 * (1 - fY) + Interp1 * fY; FittedGrid[y * dims.X + x] = Interp; } }); for (int i = 0; i < intensities.Length; i++) { intensities[i] -= FittedGrid[i]; } }
public static MapHeader ReadFromFile(BinaryReader reader, FileInfo info, int2 headerlessSliceDims, long headerlessOffset, Type headerlessType) { MapHeader Header = null; if (info.Extension.ToLower() == ".mrc" || info.Extension.ToLower() == ".mrcs" || info.Extension.ToLower() == ".ali" || info.Extension.ToLower() == ".rec" || info.Extension.ToLower() == ".st") Header = new HeaderMRC(reader); else if (info.Extension.ToLower() == ".em") Header = new HeaderEM(reader); else if (info.Extension.ToLower() == ".tif" || info.Extension.ToLower() == ".tiff") Header = new HeaderTiff(info.FullName); else if (info.Extension.ToLower() == ".dat") { long SliceElements = headerlessSliceDims.Elements() * ImageFormatsHelper.SizeOf(headerlessType); long Slices = (info.Length - headerlessOffset) / SliceElements; int3 Dims3 = new int3(headerlessSliceDims.X, headerlessSliceDims.Y, (int)Slices); Header = new HeaderRaw(Dims3, headerlessOffset, headerlessType); } else throw new Exception("File type not supported."); return Header; }