public static int GetOtsuThreshold(MyImage img) { float[] d = new float[256]; int[] hist = new int[256]; d.Initialize(); int threshold; long c1, c2, c12, m1, m2, diff; GetHistogram(img, hist); for (threshold = 1; threshold != 255; threshold++) { c1 = CalcCnt(1, threshold, hist); c2 = CalcCnt(threshold + 1, 255, hist); c12 = c1 * c2; if (c12 == 0) { continue; } m1 = CalcMean(1, threshold, hist); m2 = CalcMean(threshold + 1, 255, hist); diff = m1 * c2 - m2 * c1; d[threshold] = (float)diff * diff / c12; } threshold = FindMax(d); return(threshold); }
private static void GetHistogram(MyImage image, int[] hist) { hist.Initialize(); Parallel.For(0, image.Height, i => { Parallel.For(0, image.Width, j => { hist[(int)image.red[i, j]]++; }); }); }
public static MyImage Convert(MyImage source) { Parallel.For(0, source.Height, i => { Parallel.For(0, source.Width, j => { var res = 0.21 * source.red[i, j] + 0.72 * source.green[i, j] + 0.07 * source.blue[i, j]; source.SetValue(i, j, res); }); }); return(source); }
public static MyImage Convert(MyImage source) { var th = GetOtsuThreshold(source); var width = source.Width; var height = source.Height; Parallel.For(0, source.Height, i => { Parallel.For(0, source.Width, j => { var value = source.red[i, j] > th ? 255 : 0; source.SetValue(i, j, value); }); }); return(source); }
public static MyImage FromArray(double[] points, int height, int width, List <int> pointIndexsToKeep) { var a = new HashSet <int>(pointIndexsToKeep); var res = new MyImage(height, width); var c = 0; for (var i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (a.Contains(c)) { res.blue[i, j] = points[c]; } c += 1; } } return(res); }