-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
213 lines (177 loc) · 8.53 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
readonly int N =8;
readonly int CoefsN = 64;
public int KSVD_Depth = 64;
//----------------------------------
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);
}
}
//----------------------------------
private void DrawCoefs(Bitmap Coefsbmp, Matrix[] coefs, int rib,int count)
{
int i = 0;
for (int x = 0; x < rib; x++)
for (int y = 0; y < rib; y++)
{
if (i <= count - 1)
{
Matrix minmax = coefs[i].MinMax();
for (int x2 = 0; x2 < N; x2++)
for (int y2 = 0; y2 < N; y2++)
{
int c = (int)(minmax.Values[x2 + y2 * N]);
if (c < 0) { c = 0; }
if (c > 255) { c = 255; }
Coefsbmp.SetPixel(x * (N + 1) + x2, y * (N + 1) + y2, Color.FromArgb(255, c, c, c));
}
i += 1;
}
}
PicCoefs.Image = Helper.scaler(Coefsbmp, 256, 256, InterpolationMode.NearestNeighbor);
PicCoefs.Refresh();
}
//----------------------------------
private Matrix GetMemory(points A, int[,] dat)
{
Matrix M = new Matrix(N);
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
M.Values[x + y * N] = dat[A.x * N + x, A.y * N + y];
return M;
}
//----------------------------------
private void DrawPatch(points A, Bitmap bmp, Matrix Outp)
{
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
{
int c = (int)(Outp.Values[x + y * N]);
if (c < 0) { c = 0; }
if (c > 255) { c = 255; }
bmp.SetPixel(A.x * N + x, A.y * N + y, Color.FromArgb(255, c, c, c));
}
}
private void setPatch(points A, double[,] fpixels, Matrix Outp)
{
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
fpixels[A.x * N + x, A.y * N + y] = Outp.Values[x + y * N];
}
private void getpatch(points A, double[,] fpixels,ref Matrix Outp)
{
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
Outp.Values[x + y * N] = fpixels[A.x * N + x, A.y * N + y];
}
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);
}
//----------------------------------
}
}