public override void Initialize() { _FrameNow = 0; _MainImages = new LearningImage[MainMax]; _TmpImages = new LearningImage[MainMax]; for (int m = 0; m < MainMax; m++) { _MainImages[m] = new LearningImage(Height, Width); _TmpImages[m] = new LearningImage(Height, Width); } }
public LearningImage BackProject(List<double> list) { LearningImage image = new LearningImage(Height, Width); LearningImage tmp = new LearningImage(Height, Width); for (int m = 0; m < MainMax; m++) { double length = LearningImage.EuclideanLength(_MainImages[m]); LearningImage.Sacle(_MainImages[m], tmp, list[m] / length); LearningImage.Add(image, tmp, image); } return image; }
public List<double> Project(LearningImage image) { List<double> results = new List<double>(); LearningImage amt = new LearningImage(Height, Width, image.Data); LearningImage tmp = new LearningImage(Height, Width); for (int m = 0; m < MainMax; m++) { double length = LearningImage.EuclideanLength(_MainImages[m]); double result = LearningImage.DotProduct(amt, _MainImages[m]) / length; LearningImage.Sacle(_MainImages[m], tmp, result / length); LearningImage.Sub(amt, tmp, amt); results.Add(result); } return results; }
private void Update(LearningImage imgIn) { Array.Copy(imgIn.Data, _TmpImages[0].Data, Length); long iterateMax = MainMax - 1; if (MainMax > _FrameNow) iterateMax = _FrameNow; LearningImage imgA = new LearningImage(Height, Width); LearningImage imgB = new LearningImage(Height, Width); LearningImage imgC = new LearningImage(Height, Width); double scalerA, scalerB, scalerC; double nrmV; double l = DynamicAmnesic; //!<忘却の値、2ぐらいがよい for (int i = 0; i <= iterateMax; i++) { if (i == _FrameNow) { Array.Copy(_TmpImages[_FrameNow].Data, _MainImages[_FrameNow].Data, Length); continue; } ///// Vi(n) = [a= (n-1-l)/n * Vi(n-1)] + [b= (1+l)/n * Ui(n)T Vi(n-1)/|Vi(n-1)| * Ui(n) ] nrmV = Norm.Euclidean(_MainImages[i].Data); scalerA = (double)(_FrameNow - 1 - l) / (double)_FrameNow; LearningImage.Sacle(_MainImages[i], imgA, scalerA); double dotUV = Matrix.InnerProduct(_TmpImages[i].Data, _MainImages[i].Data); scalerB = ((double)(1 + l) * dotUV) / ((double)_FrameNow * nrmV); LearningImage.Sacle(_TmpImages[i], imgB, scalerB); LearningImage.Add(imgA, imgB, _MainImages[i]); ///// Ui+1(n) = Ui(n) - [c= Ui(n)T Vi(n)/|Vi(n)| * Vi(n)/|Vi(n)| ] if (i == iterateMax || i >= MainMax - 1) continue; nrmV = Norm.Euclidean(_MainImages[i].Data); dotUV = Matrix.InnerProduct(_TmpImages[i].Data, _MainImages[i].Data); scalerC = dotUV / (nrmV * nrmV); LearningImage.Sacle(_MainImages[i], imgC, scalerC); LearningImage.Sub(_TmpImages[i], imgC, _TmpImages[i + 1]); } _FrameNow++; }
public static List<double> HighLow(LearningImage a) { return new List<double>() { a.Data.Max(), a.Data.Min() }; }
public static LearningImage LoadBin(string path) { BinaryReader reader = null; try { if (!File.Exists(path)) return null; byte[] bytes = File.ReadAllBytes(path); int shift = 0; int height = BitConverter.ToInt32(bytes, shift); shift += sizeof(int); int width = BitConverter.ToInt32(bytes, shift); shift += sizeof(int); LearningImage image = new LearningImage(height, width); for (int l = 0; l < image.Length; l++) image.Data[l] = BitConverter.ToDouble(bytes, l * sizeof(double) + shift); return image; } catch(Exception ex) { Log.Instance.Error(ex.Message + "@" + ex.StackTrace); return null; } finally { if (reader != null) reader.Close(); reader = null; } }
public static double DotProduct(LearningImage a, LearningImage b) { return Matrix.InnerProduct(a.Data, b.Data); }
public static double EuclideanLength(LearningImage a) { return Norm.Euclidean(a.Data); }
public LearningImage(LearningImage image) : this(image.Height, image.Width, image.Data) { }
public static void Add(LearningImage a, LearningImage b, LearningImage o) { for (int l = 0; l < o.Length; l++) o.Data[l] = a.Data[l] + b.Data[l]; }
public void Sub(LearningImage image) { Sub(this, image, this); }
public LearningImage Shrink(int scale = 2) { if (scale <= 1) return this; LearningImage i = new LearningImage(this.Height / scale, this.Width / scale); for (int h = 0; h < i.Height; h++) { int hs = h * scale; for (int w = 0; w < i.Width; w++) { int ws = w * scale; List<int> list = new List<int>(); for (int hh = hs; hh < hs + scale; hh++) for (int ww = ws; ww < ws + scale; ww++) list.Add(this.Width * hh + ww); int postion = (i.Width * h + w) * i.Plane; for (int p = 0; p < i.Plane; p++) i.Data[postion + p] = Average(list, i.Plane, p); } } return i; }
public void Add(LearningImage image) { Add(this, image, this); }
public static void Sacle(LearningImage i, LearningImage o, double scale = 1, double bias = 0) { for (int l = 0; l < o.Length; l++) o.Data[l] = i.Data[l] * scale + bias; }
public static unsafe LearningImage LoadPng(string path) { if (!File.Exists(path)) return null; try { Bitmap src = new Bitmap(path); BitmapData srcData = src.LockBits(new Rectangle(Point.Empty, src.Size), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); LearningImage i = new LearningImage(src.Height, src.Width); for (int h = 0; h < srcData.Height; h++) { byte* ps = (byte*)srcData.Scan0 + srcData.Stride * h; for (int w = 0; w < srcData.Width; w++, ps += i.Plane) { int postion = (i.Width * h + w) * i.Plane; for (int p = 0; p < i.Plane; p++) i.Data[postion + p] = (double)ps[p] / 255; } } src.UnlockBits(srcData); return i; } catch(Exception ex) { Log.Instance.Error(ex.Message + "@" + ex.StackTrace); return null; } }