public Bitmap SetMask(Bitmap originalImage, Bitmap mask) { using (SingleBitmapData resultbitmapData = new SingleBitmapData(originalImage, PixelFormat.Format24bppRgb)) using (SingleBitmapData originalbitmapData = new SingleBitmapData(originalImage, false)) using (SingleBitmapData maskBitmapData = new SingleBitmapData(mask, false)) { for (int i = 0; i < originalbitmapData.HeightInPixels; i++) { for (int j = 0; j < originalbitmapData.WidthInBytes; j += originalbitmapData.BytesPerPixel) { var index = i * originalbitmapData.WidthInBytes + j; var resultIndex = 3 * i * originalbitmapData.WidthInBytes + j * 3; var originalColor = originalbitmapData.FirstPixelPtr[index]; var maskColor = maskBitmapData.FirstPixelPtr[index]; if (maskColor == 0) { resultbitmapData.FirstPixelPtr[resultIndex] = originalColor; resultbitmapData.FirstPixelPtr[resultIndex + 1] = originalColor; resultbitmapData.FirstPixelPtr[resultIndex + 2] = originalColor; } else { resultbitmapData.FirstPixelPtr[resultIndex] = 255; resultbitmapData.FirstPixelPtr[resultIndex + 1] = 0; resultbitmapData.FirstPixelPtr[resultIndex + 2] = 140; } } } return(resultbitmapData.Bitmap); } }
public static ImageComponents FFT2D(Bitmap bitmap) { using (SingleBitmapData bitmapData = new SingleBitmapData(bitmap, PixelFormat.Format8bppIndexed)) { var data = CollectionHelper.Create2DArray <Complex>(bitmapData.WidthInBytes, bitmapData.HeightInPixels); for (int y = 0; y < bitmapData.HeightInPixels; y++) { for (int x = 0; x < bitmapData.WidthInBytes; x += bitmapData.BytesPerPixel) { data[y][x] = bitmapData.FirstPixelPtr[x + y * bitmapData.WidthInBytes]; } } for (int i = 0; i < data.Length; i++) { FFT1D(data[i]); } var swappedData = CollectionHelper.SwapRowColumn(data); for (int i = 0; i < swappedData.Length; i++) { FFT1D(swappedData[i]); } return(new ImageComponents(swappedData, bitmap)); } }
public List <Bitmap> CreateMasks(Bitmap bitmap, int threshold, int minPixelsInRegion, string saveFolder) { var imagePixels = new int[bitmap.Height * bitmap.Width]; var regions = new List <HashSet <int> >(); using (SingleBitmapData bitmapData = new SingleBitmapData(bitmap, false)) { var seeds = GenerateSeeds(bitmapData); AllocateRegions(bitmapData, seeds, threshold, minPixelsInRegion, imagePixels, regions); ProcessUnallocatedPixels(bitmapData, imagePixels, regions); } var colors = GenerateColors(regions); var masks = new List <Bitmap>(); masks.Add(CreateFullMaskImage(bitmap, imagePixels, colors)); int counter = 1; foreach (var region in regions) { var mask = CreateMaskImage(bitmap, region); mask.Save($"{saveFolder}/{counter++}_mask.png"); masks.Add(mask); } return(masks); }
private Bitmap CreateMaskImage(Bitmap bitmap, HashSet <int> region) { using (SingleBitmapData bitmapData = new SingleBitmapData(bitmap, true)) { for (int i = 0; i < bitmapData.HeightInPixels; i++) { for (int j = 0; j < bitmapData.WidthInBytes; j += bitmapData.BytesPerPixel) { var index = i * bitmapData.WidthInBytes + j; bitmapData.FirstPixelPtr[index] = (byte)(region.Contains(index) ? 255 : 0); } } return(bitmapData.Bitmap); } }
public static Bitmap IFFT2D(Complex[][] data, Bitmap originalBitmap) { FourierHelper.Conjugate(data); for (int i = 0; i < data.Length; i++) { FFT1D(data[i]); } FourierHelper.Conjugate(data); CollectionHelper.ApplyOperation(data, c => c / data[0].Length); var swappedData = CollectionHelper.SwapRowColumn(data); FourierHelper.Conjugate(swappedData); for (int i = 0; i < swappedData.Length; i++) { FFT1D(swappedData[i]); } FourierHelper.Conjugate(swappedData); CollectionHelper.ApplyOperation(swappedData, c => c / swappedData[0].Length); using (var bitmapData = new SingleBitmapData(originalBitmap, PixelFormat.Format8bppIndexed)) { for (int y = 0; y < bitmapData.HeightInPixels; y++) { for (int x = 0; x < bitmapData.WidthInBytes; x += bitmapData.BytesPerPixel) { bitmapData.FirstPixelPtr[x + y * bitmapData.WidthInBytes] = (byte)ImageHelper.FixOverflow(swappedData[y][x].Real); } } return(bitmapData.Bitmap); } }
public static List <int> GetNeighbourhood(SingleBitmapData bitmapData, int position, HashSet <int> visited, int[] imagePixels) { int x = position % bitmapData.WidthInBytes; int y = position / bitmapData.WidthInBytes; var neighbourhood = new List <int>(); for (int j = -1; j < 2; j++) { for (int k = -1; k < 2; k++) { var currY = y + j; var currX = x + k; var currPos = currY * bitmapData.WidthInBytes + currX; if (currY >= 0 && currY < bitmapData.HeightInPixels && currX >= 0 && currX < bitmapData.WidthInBytes && !visited.Contains(currPos) && imagePixels[currPos] == 0) { neighbourhood.Add(currPos); } } } return(neighbourhood); }
public static List <int> GetProcessedNeighbourhood(SingleBitmapData bitmapData, int position, int[] imagePixels) { int x = position % bitmapData.WidthInBytes; int y = position / bitmapData.WidthInBytes; var neighbourhood = new List <int>(); for (int j = -2; j < 3; j++) { for (int k = -2; k < 3; k++) { var currY = y + j; var currX = x + k; var currPos = currY * bitmapData.WidthInBytes + currX; if (currY >= 0 && currY < bitmapData.HeightInPixels && currX >= 0 && currX < bitmapData.WidthInBytes && imagePixels[currPos] != 0) { neighbourhood.Add(currPos); } } } return(neighbourhood); }