Exemple #1
0
		static public Color ProcessPixel(string mode, Color[] values)
		{
			int n = values.Length;
			Color ret;
			Color mean;
			Color stddev;
			float temp;
			switch (mode)
			{
				case "mean":
					ret = new Color();
					ret.R = values.GetRChannel().Average();
					ret.G = values.GetGChannel().Average();
					ret.B = values.GetBChannel().Average();
					return ret;
				case "stddev":
					mean = ProcessPixel("mean", values);
					ret = new Color();
					ret.R = values.GetRChannel().Select(i => (float)Math.Pow(i - mean.R, 2)).Average();
					ret.R = (float)Math.Sqrt(ret.R);
					if (ret.R < .000001) ret.R = .000001f;
					ret.G = values.GetGChannel().Select(i => (float)Math.Pow(i - mean.G, 2)).Average();
					ret.G = (float)Math.Sqrt(ret.G);
					if (ret.G < .000001) ret.G = .000001f;
					ret.B = values.GetBChannel().Select(i => (float)Math.Pow(i - mean.B, 2)).Average();
					ret.B = (float)Math.Sqrt(ret.B);
					if (ret.B < .000001) ret.B = .000001f;
					return ret;
				case "xi":
				{
					mean = ProcessPixel("mean", values);
					stddev = ProcessPixel("stddev", values);
					ret = new Color();
					temp = xi(values[0], mean, stddev);
					ret.R = ret.G = ret.B = temp;
					return ret;
				}
				case "CDi":
				{
					mean = ProcessPixel("mean", values);
					stddev = ProcessPixel("stddev", values);
					float Xi = ProcessPixel("xi", values).R;
					Func<Color, Color, Color, float, float> func =
						(I, U, Sig, xi) =>
						{
							return (float)Math.Sqrt(
								(float)Math.Pow((I.R - xi * U.R) / Sig.R, 2)
								+ (float)Math.Pow((I.G - xi * U.G) / Sig.G, 2)
								+ (float)Math.Pow((I.B - xi * U.B) / Sig.B, 2));
						};
					ret = new Color();
					temp = func(values[0], mean, stddev, Xi) / 10;
					ret.R = ret.G = ret.B = temp;
					return ret;
				}
				case "bi":
				{
					mean = ProcessPixel("mean", values);
					stddev = ProcessPixel("stddev", values);
					Func<Color, Color, Color, float, float> func =
						(I, U, Sig, xi) =>
						{
							return (float)Math.Sqrt(
								(float)Math.Pow((I.R - xi * U.R) / Sig.R, 2)
								+ (float)Math.Pow((I.G - xi * U.G) / Sig.G, 2)
								+ (float)Math.Pow((I.B - xi * U.B) / Sig.B, 2));
						};
					ret = new Color();
					temp = values
						.Select(i => new Tuple<Color, Color, Color, float>(i, mean, stddev, xi(i, mean, stddev)))
						.Select(i => func(i.Item1, i.Item2, i.Item3, i.Item4))
						.Sum(i => (float)Math.Pow(i, 2));
					temp = (float)Math.Sqrt(temp / n) / 10;
					ret.R = ret.G = ret.B = temp;
					return ret;
				}
				case "ai":
				{
					mean = ProcessPixel("mean", values);
					stddev = ProcessPixel("stddev", values);
					ret = new Color();
					temp = values
						.Select(i => new Tuple<Color, Color, Color>(i, mean, stddev))
						.Select(i => xi(i.Item1, i.Item2, i.Item3))
						.Sum(i => (float)Math.Pow(i - 1f, 2));
					temp = (float)Math.Sqrt(temp / n);
					//temp = temp / n;
					ret.R = ret.G = ret.B = temp;
					return ret;
				}
				default:
					throw new System.Exception("The passed mode was not valid!!");
			}
		}