public Bitmap BitmapFromClusterMap() { PixelTypeInfo pixelInfo = PixelInfo.GetPixelTypeInfo(clusterImage); Rectangle imageSize = new Rectangle(0, 0, clusterImage.Width, clusterImage.Height); BitmapData originalBitmapData = clusterImage.LockBits(imageSize, ImageLockMode.WriteOnly, clusterImage.PixelFormat); unsafe { byte *rowPtr = (byte *)originalBitmapData.Scan0; int pixelIndex = 0; for (int y = 0; y < originalBitmapData.Height; y++) { byte *pixelPtr = rowPtr; for (int x = 0; x < originalBitmapData.Width; x++) { int clusterNumber = ClusterMap[pixelIndex]; if (ViewType == ClusterViewTypes.Clusters) { int colorIndex = (clusterNumber % (colors.Length / 3)) * 3; pixelPtr[0] = colors[colorIndex + 0]; pixelPtr[1] = colors[colorIndex + 1]; pixelPtr[2] = colors[colorIndex + 2]; } else if (ViewType == ClusterViewTypes.PixelDistances) { byte d1 = LabDistances[pixelIndex * 4 + 0]; byte d2 = LabDistances[pixelIndex * 4 + 1]; byte d3 = LabDistances[pixelIndex * 4 + 2]; byte d4 = LabDistances[pixelIndex * 4 + 3]; byte sum = (byte)((d1 + d2 + d3 + d4) * 50); byte value = Math.Min(sum, byte.MaxValue); pixelPtr[0] = value; pixelPtr[1] = value; pixelPtr[2] = value; } else if (ViewType == ClusterViewTypes.Image) { pixelPtr[(int)RGBAColor.Red] = RGBPixels[pixelIndex * 3 + 0]; pixelPtr[(int)RGBAColor.Green] = RGBPixels[pixelIndex * 3 + 1]; pixelPtr[(int)RGBAColor.Blue] = RGBPixels[pixelIndex * 3 + 2]; } pixelIndex++; pixelPtr += 3; } rowPtr += originalBitmapData.Stride; } } clusterImage.UnlockBits(originalBitmapData); return(clusterImage); }
private void ToLabPixels(Bitmap image) { PixelTypeInfo pixelInfo = PixelInfo.GetPixelTypeInfo(image); if (pixelInfo.GetBytesForColor(RGBAColor.RGB) != 1) { throw new Exception("Pixeltype is not supported."); } Rectangle imageSize = new Rectangle(0, 0, image.Width, image.Height); BitmapData originalBitmapData = image.LockBits(imageSize, ImageLockMode.ReadOnly, image.PixelFormat); unsafe { byte *rowPtr = (byte *)originalBitmapData.Scan0; int index = 0; for (int y = 0; y < originalBitmapData.Height; y++) { byte *pixelPtr = rowPtr; for (int x = 0; x < originalBitmapData.Width; x++) { RGBPixels[index + 0] = pixelPtr[(int)RGBAColor.Red]; RGBPixels[index + 1] = pixelPtr[(int)RGBAColor.Green]; RGBPixels[index + 2] = pixelPtr[(int)RGBAColor.Blue]; index += 3; pixelPtr += 3; } rowPtr += originalBitmapData.Stride; } } image.UnlockBits(originalBitmapData); if (UseGaussBlur) { gpuAccel.Invoke("GaussianBlur", 0, ImageWidth * ImageHeight, RGBPixels, GaussedRGBPixels, ImageWidth, ImageHeight); var t = RGBPixels; RGBPixels = GaussedRGBPixels; GaussedRGBPixels = t; } gpuAccel.Invoke("RGBToLab", 0, LabPixels.Length / 3, RGBPixels, LabPixels, 255f); gpuAccel.Invoke("LabDistances", 0, ImageWidth * ImageHeight, LabPixels, LabDistances, ImageWidth, ImageHeight, MaxColorDistanceForMatch); if (UseNoiseRemoval) { gpuAccel.Invoke("RemoveNoise", 0, ImageWidth * ImageHeight, LabDistances, ImageWidth, ImageHeight); } }