private void button1_Click(object sender, EventArgs e) { int rib = (int)Math.Sqrt(KSVD_Depth); if (rib * rib < KSVD_Depth) { rib = (int)Math.Sqrt(KSVD_Depth) + 1; } Bitmap InputBitmap = (Bitmap)Bitmap.FromFile("Kawasaki_Valencia_2007_09_320x240.bmp"); Bitmap SparseDicBitmap = new Bitmap((N + 1) * rib, (N + 1) * rib, System.Drawing.Imaging.PixelFormat.Format24bppRgb); int[,] memory = new int[InputBitmap.Width, InputBitmap.Height]; for (points A = new points(InputBitmap.Width, InputBitmap.Height); A.DoIt; A.Inc()) { memory[A.x, A.y] = (int)(InputBitmap.GetPixel(A.x, A.y).GetBrightness() * 255.0); } Matrix[] Dictionairy = new Matrix[N * N]; Matrix PatchOut = new Matrix(N, 0); double[,] pixels = new double[InputBitmap.Width, InputBitmap.Height]; for (int i = 0; i < N * N; i++) { Dictionairy[i] = new Matrix(N, 0); } for (int p = 0; p < KSVD_Depth; p++) { Dictionairy[p].Fill(0); for (points A = new points(InputBitmap.Width / N, InputBitmap.Height / N); A.DoIt; A.Inc()) { Matrix PatchIn = GetMemory(A, memory); if (p == 0) { PatchOut = PatchIn.Average(); setPatch(A, pixels, PatchOut); } else { getpatch(A, pixels, ref PatchOut); PatchOut += (Matrix.Dot(PatchIn - PatchOut, Dictionairy[p - 1]) * Dictionairy[p - 1]); setPatch(A, pixels, PatchOut); } Matrix d = (PatchIn - PatchOut); d.Normalize(); Dictionairy[p] = Dictionairy[p] + Matrix.Sign(d, Dictionairy[p]); DrawPatch(A, InputBitmap, PatchOut); } PicImage.Image = Helper.scaler(InputBitmap, 4, InterpolationMode.NearestNeighbor); PicImage.Refresh(); Dictionairy[p].Normalize(); } DrawCoefs(SparseDicBitmap, Dictionairy, rib, KSVD_Depth); }
//---------------------------------- private void ButtonStart_Click(object sender, EventArgs e) { KSVD_Depth = System.Convert.ToInt32(cbDepth.Text); int rib = (int)Math.Sqrt(CoefsN); if (rib * rib < CoefsN) { rib = (int)Math.Sqrt(CoefsN) + 1; } Bitmap InputBitmap = (Bitmap)Bitmap.FromFile("Kawasaki_Valencia_2007_09_320x240.bmp"); Bitmap SparseDicBitmap = new Bitmap((N + 1) * rib, (N + 1) * rib, System.Drawing.Imaging.PixelFormat.Format24bppRgb); PicImage.Image = Helper.scaler(InputBitmap, 2, InterpolationMode.NearestNeighbor); PicCoefs.Image = Helper.scaler(SparseDicBitmap, 256, 256, InterpolationMode.NearestNeighbor); int[,] memory = new int[InputBitmap.Width, InputBitmap.Height]; for (points A = new points(InputBitmap.Width, InputBitmap.Height); A.DoIt; A.Inc()) { memory[A.x, A.y] = (int)(InputBitmap.GetPixel(A.x, A.y).GetBrightness() * 255.0); } Random rnd = new Random(); Matrix[] CopyDictionairy = Matrix.GetMatrixArrayRandom(N, CoefsN, rnd); Matrix[] Dictionairy = Matrix.GetMatrixArrayRandom(N, CoefsN, rnd); Matrix PatchOut; Int32 index = 0; Int32[] Cnt = new Int32[CoefsN]; for (int i = 0; i < CoefsN; i++) { Cnt[i] = 0; } double quant = System.Convert.ToInt32(cbQuant.Text); for (int repeat = 0; repeat < 50; repeat++) { for (points A = new points(InputBitmap.Width / N, InputBitmap.Height / N); A.DoIt; A.Inc()) { Matrix PatchIn = GetMemory(A, memory); PatchOut = PatchIn.Average(); for (int p = 0; p < KSVD_Depth; p++) { Matrix Norm = PatchIn - PatchOut; Matrix Atom = Matrix.MaxDot(Norm, CopyDictionairy, ref index); double pickvalue = Math.Round(Matrix.Dot(PatchIn - PatchOut, Atom) / quant) * quant; PatchOut += (pickvalue * Atom); //FeedBack Norm.Normalize(); Dictionairy[index] = Dictionairy[index] + pickvalue * Norm; Cnt[index] += 1; } DrawPatch(A, InputBitmap, PatchOut); } PicImage.Image = Helper.scaler(InputBitmap, 4, InterpolationMode.NearestNeighbor); PicImage.Refresh(); for (int i = 0; i < CopyDictionairy.Length; i++) { if (Cnt[i] > 2) { Dictionairy[i].Normalize(); CopyDictionairy[i].CopyFrom(Dictionairy[i]); } else { Dictionairy[i].FillRnd(rnd, memory); Dictionairy[i].Normalize(); CopyDictionairy[i].CopyFrom(Dictionairy[i]); } } DrawCoefs(SparseDicBitmap, CopyDictionairy, rib, CoefsN); } }