private void KMeansCPU(bool async = false) { DoubleColor[,] LABImageArray = new DoubleColor[SourceImageColorArray.GetLength(0), SourceImageColorArray.GetLength(1)]; var cp = SourceImageColorProfile; var start1 = DateTime.Now; ColorProfileConverter.ConvertImageToLAB(SourceImageColorArray, LABImageArray, cp); DoubleColor[,] result; if (async == false) { result = KMeansCalc.CalculateKMeans(LABImageArray, KMeansParam, Globals.max_iter, Globals.eps); } else { result = KMeansCalc.CalculateKMeansAsync(LABImageArray, KMeansParam, Globals.max_iter, Globals.eps); } ColorProfileConverter.ConvertImageFromLAB(result, DestImageColorArray, cp); var end = DateTime.Now; string s = ""; if (async == true) { s = "[ASYNC]"; } Debug.WriteLine($"CPU TIME{s}: {end - start1}"); Dispatcher.Invoke(() => { Paint.CopyToWriteableBitmap(DestImageWB, DestImageColorArray); }); }
public static void ConvertImageToDoubleColor(SimpleColor[,] imageSource, DoubleColor[,] imageDest) { Parallel.For(0, imageSource.GetLength(0), i => { for (int j = 0; j < imageSource.GetLength(1); j++) { imageDest[i, j] = new DoubleColor(imageSource[i, j]); } }); }
public static void SetGrayscale(SimpleColor[,] pixels) { Parallel.For(0, pixels.GetLength(0), y => { for (int j = 0; j < pixels.GetLength(1); j++) { pixels[y, j].ToGrayscale(); } }); }
public static void ConvertImageToLAB(SimpleColor[,] imageSource, DoubleColor[,] imageDest, ColorProfile from) { Parallel.For(0, imageSource.GetLength(0), i => { for (int j = 0; j < imageSource.GetLength(1); j++) { DoubleColor color = new DoubleColor(imageSource[i, j]); var c1 = ConvertColorToXYZ(color, from); var c2 = ConvertColorFromXYZToLAB(c1, from); imageDest[i, j] = c2; } }); }
public static void ConvertImage(SimpleColor[,] imageSource, SimpleColor[,] imageDest, ColorProfile from, ColorProfile to) { var bradfordMatrix = MyMatrix.GenerateBradfordMatrix(from, to); Parallel.For(0, imageSource.GetLength(0), i => { for (int j = 0; j < imageSource.GetLength(1); j++) { DoubleColor color = new DoubleColor(imageSource[i, j]); var c1 = ConvertColorToXYZ(color, from); var c2 = ConvertColorFromXYZ(c1, to, bradfordMatrix); imageDest[i, j] = c2.ToSimpleColor(); } }); }
// -------------------------------------------------------------------------- // Section 14.2.5 Parallelizing the application // Listing 14.17 Applying color filter in parallel public static SimpleColor[,] RunFilterParallel (this SimpleColor[,] arr, Func <SimpleColor, SimpleColor> f) { int hgt = arr.GetLength(0), wid = arr.GetLength(1); var res = new SimpleColor[hgt, wid]; // Parallelize the outer loop Parallel.For(0, hgt, y => { // Leave inner loop sequential for (int x = 0; x < wid; x++) { res[y, x] = f(arr[y, x]); } }); return(res); }
// Listing 14.11 Sequential method for applying filters (C#) public static SimpleColor[,] RunFilter (this SimpleColor[,] arr, Func <SimpleColor, SimpleColor> f) { int hgt = arr.GetLength(0), wid = arr.GetLength(1); // Create a new array as the result var res = new SimpleColor[hgt, wid]; // Calculate new color for every pixel for (int y = 0; y < hgt; y++) { for (int x = 0; x < wid; x++) { res[y, x] = f(arr[y, x]); } } return(res); }
public static void CopyToWriteableBitmap(WriteableBitmap writeableBitmap, SimpleColor[,] pixels) { writeableBitmap.Lock(); unsafe { Int64 writeablebpp = writeableBitmap.Format.BitsPerPixel / 8; Int64 writeableBuffer = (Int64)writeableBitmap.BackBuffer; Int64 bufferstride = writeableBitmap.BackBufferStride; Parallel.For(0, pixels.GetLength(0), y => { Int64 place = writeableBuffer + y * bufferstride; for (int x = 0; x < pixels.GetLength(1); x++) { *((int *)place) = pixels[y, x].ToInt(); place += writeablebpp; } }); } writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight)); writeableBitmap.Unlock(); }
public static void ReadColorsFromBitmap(WriteableBitmap writeableBitmap, SimpleColor[,] pixels) { writeableBitmap.Lock(); unsafe { Int64 writeablebpp = writeableBitmap.Format.BitsPerPixel / 8; Int64 writeableBuffer = (Int64)writeableBitmap.BackBuffer; Int64 bufferstride = writeableBitmap.BackBufferStride; for (Int64 y = 0; y < pixels.GetLength(0); y++) { for (Int64 x = 0; x < pixels.GetLength(1); x++) { int col = *((int *)writeableBuffer); //SimpleColor sc = new SimpleColor(col); pixels[y, x].ConvertFromInt(col); writeableBuffer += writeablebpp; } } } writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight)); writeableBitmap.Unlock(); }
// Convert array to .NET Bitmap type public static Bitmap ToBitmap(this SimpleColor[,] arr) { var bmp = new Bitmap(arr.GetLength(0), arr.GetLength(1)); var rect = new Rectangle(0, 0, bmp.Width, bmp.Height); var bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); var ptr0 = bmpData.Scan0; var stride = bmpData.Stride; int width = bmp.Width; int height = bmp.Height; for (int y = 0; y < width; y++) { for (int x = 0; x < height; x++) { var offset = y * 4 + stride * x; var clr = arr[y, x]; var clr2 = Color.FromArgb(clr.R, clr.G, clr.B).ToArgb(); System.Runtime.InteropServices.Marshal.WriteInt32(ptr0, offset, clr2); } } bmp.UnlockBits(bmpData); return(bmp); }
// Get pixel color after applying effect to the specified input public static SimpleColor[,] Blur(SimpleColor[,] arr, bool parallel) { int hgt = arr.GetLength(0), wid = arr.GetLength(1); var res = new SimpleColor[hgt, wid]; // The outer for loop can be parallelized, depending on the argument RunForLoop(parallel, 0, hgt, y => { // Leave inner loop sequential for (int x = 0; x < wid; x++) { // Sum colors close to the specified location var sum = SimpleColor.Zero; for (int dy = -2; dy <= 2; dy++) { for (int dx = -2; dx <= 2; dx++) { sum += CheckedRead(arr, y + dy, x + dx, hgt, wid); } } res[y, x] = sum / 25; } }); return(res); }
public static void CreateHSVBitmap(SimpleColor[,] pixels) { int middle_x = pixels.GetLength(1) / 2; int middle_y = pixels.GetLength(0) / 2; for (int y = 0; y < pixels.GetLength(0); y++) { for (int x = 0; x < pixels.GetLength(1); x++) { if (y < Globals.BorderWidth || y > pixels.GetLength(0) - Globals.BorderWidth || x < Globals.BorderWidth || x > pixels.GetLength(1) - Globals.BorderWidth) { pixels[y, x] = new SimpleColor(0, 0, 0); } else { pixels[y, x] = new SimpleColor(255, 255, 255); double dist = Distance(middle_x, middle_y, x, y); if (dist <= Globals.Radius) { //calculate HSV //double V = 1; double C = dist / (double)Globals.Radius; double m = 1 - C; double cos = Cosinus(middle_x, middle_y, x, y, dist); if (y <= middle_y)// angle between 0 and pi { double H = Math.Acos(cos); double angleH = H * 180d / Math.PI; double int_H = (angleH / 60d); //C to dist double X = C * (1 - Math.Abs((int_H % 2) - 1)); if (int_H <= 1) { var color = new DoubleColor(C + m, X + m, m); var simpleColor = color.ToSimpleColor(); //double R = 0.5 + C * Math.Cos(angleH) / Math.Cos(60 - H); //double B = 0.5 - C; //double G = 1.5 - B - R; //color = new DoubleColor(R, G, B); //simpleColor = color.ToSimpleColor(); pixels[y, x] = simpleColor; } else if (int_H <= 2) { var color = new DoubleColor(X + m, C + m, m); pixels[y, x] = color.ToSimpleColor(); } else { var color = new DoubleColor(m, C + m, X + m); pixels[y, x] = color.ToSimpleColor(); } } else // angle between pi and 2 pi { double H = Math.Acos(cos); double angleH = 360d - H * 180 / Math.PI; double int_H = (angleH / 60); //C to dist double X = C * (1 - Math.Abs((int_H % 2) - 1)); if (int_H <= 4) { var color = new DoubleColor(m, X + m, C + m); pixels[y, x] = color.ToSimpleColor(); } else if (int_H <= 5) { var color = new DoubleColor(X + m, m, C + m); pixels[y, x] = color.ToSimpleColor(); } else { var color = new DoubleColor(C + m, m, X + m); pixels[y, x] = color.ToSimpleColor(); } } } } } } }