forked from perivar/FindSimilar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscreteCosineTransform.cs
433 lines (389 loc) · 11.7 KB
/
DiscreteCosineTransform.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Windows.Forms ;
namespace DiscreteCosineTransform
{
/// <summary>
/// Implementation of 2D Discrete Cosine Transform
/// By Vinayak Bharadi, 16 Nov 2009
/// </summary>
/// <see cref="http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=43782">DCT Implementation in C#</see>
public static class DiscreteCosineTransform2D
{
/// <summary>
/// Initialize alpha coefficient array
/// </summary>
private static Double[,] InitCoefficientsMatrix(int dim)
{
Double[,] coefficientsMatrix = new double[dim, dim];
for (int i = 0; i < dim; i++)
{
coefficientsMatrix[i, 0] = System.Math.Sqrt(2.0) / dim;
coefficientsMatrix[0, i] = System.Math.Sqrt(2.0) / dim;
}
coefficientsMatrix[0, 0] = 1.0 / dim;
for (int i = 1; i < dim; i++)
{
for (int j = 1; j < dim; j++)
{
coefficientsMatrix[i, j] = 2.0 / dim;
}
}
return coefficientsMatrix;
}
private static bool IsQuadricMatrix<T>(T[,] matrix)
{
int columnsCount = matrix.GetLength(0);
int rowsCount = matrix.GetLength(1);
return (columnsCount == rowsCount);
}
public static Double[,] ForwardDCT(Double[,] input)
{
double sqrtOfLength = System.Math.Sqrt(input.Length);
if (DiscreteCosineTransform2D.IsQuadricMatrix<Double>(input) == false)
{
throw new ArgumentException("Matrix must be quadric");
}
int N = input.GetLength(0);
double[,] coefficientsMatrix = InitCoefficientsMatrix(N);
Double[,] output = new Double[N, N];
for (int u = 0; u <= N-1; u++)
{
for (int v = 0; v <= N-1; v++)
{
double sum = 0.0;
for (int x = 0; x <= N-1; x++)
{
for (int y = 0; y <= N-1; y++)
{
sum += input[x, y] * System.Math.Cos(((2.0 * x + 1.0) / (2.0 * N)) * u * System.Math.PI) * System.Math.Cos(((2.0 * y + 1.0) / (2.0 * N)) * v * System.Math.PI);
}
}
sum *= coefficientsMatrix[u, v];
output[u, v] = System.Math.Round(sum);
}
}
return output;
}
public static Double[,] InverseDCT(Double[,] input)
{
double sqrtOfLength = System.Math.Sqrt(input.Length);
if (DiscreteCosineTransform2D.IsQuadricMatrix<Double>(input) == false)
{
throw new ArgumentException("Matrix must be quadric");
}
int N = input.GetLength(0);
Double[,] coefficientsMatrix = InitCoefficientsMatrix(N);
Double[,] output = new Double[N, N];
for (int x = 0; x <= N-1; x++)
{
for (int y = 0; y <= N-1; y++)
{
double sum = 0.0;
for (int u = 0; u <= N-1; u++)
{
for (int v = 0; v <= N-1; v++)
{
sum += coefficientsMatrix[u, v] * input[u, v] * System.Math.Cos(((2.0 * x + 1.0) / (2.0 * N)) * u * System.Math.PI) * System.Math.Cos(((2.0 * y + 1.0) / (2.0 * N)) * v * System.Math.PI);
}
};
output[x, y] = System.Math.Round(sum);
}
}
return output;
}
}
public class FastDCT2D
{
public Bitmap Obj; // Input Object Image
public Bitmap DCTMap; // Colour DCT Map
public Bitmap IDCTImage;
public int[,] GreyImage; // GreyScale Image Array Generated from input Image
public double[,] Input; // Greyscale Image in Double Format
public double[,] DCTCoefficients;
public double[,] IDCTCoefficients;
private double[,] DCTkernel; // DCT Kernel to find Transform Coefficients
int Width, Height;
int Order;
/// <summary>
/// Parameterized Constructor for FFT Reads Input Bitmap to a Greyscale Array
/// </summary>
/// <param name="Input">Input Image</param>
/// <param name="DCTOrder">Dimension of the matrix (e.g. 256)</param>
public FastDCT2D(Bitmap Input, int DCTOrder)
{
Obj = Input;
Width = Input.Width;
Height = Input.Height;
Order = DCTOrder;
ReadImage();
}
/// <summary>
/// Parameterized Constructor for FFT
/// </summary>
/// <param name="Input">Greyscale Array</param>
public FastDCT2D(int[,] InputImageData, int order)
{
int i, j;
GreyImage = InputImageData;
Width = InputImageData.GetLength(0);
Height = InputImageData.GetLength(1);
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
Input[i, j] = (Double)(InputImageData[i, j]);
}
Order = order;//Order of Inverse 2D DCT
}
/// <summary>
/// Constructor For Inverse DCT
/// </summary>
/// <param name="InputData"></param>
public FastDCT2D(double[,] DCTCoeffInput)
{
DCTCoefficients = DCTCoeffInput;
Width = DCTCoeffInput.GetLength(0);
Height = DCTCoeffInput.GetLength(1);
}
/// <summary>
/// Read Bitmap Image to 2D Integer Grey Levels Array for Proccessing
/// </summary>
private void ReadImage()
{
int i, j;
GreyImage = new int[Width, Height]; //[Row,Column]
Input = new double [Width, Height]; //[Row,Column]
Bitmap image = Obj;
BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* imagePointer1 = (byte*)bitmapData1.Scan0;
for (i = 0; i < bitmapData1.Height; i++)
{
for (j = 0; j < bitmapData1.Width; j++)
{
GreyImage[j, i] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0);
Input [j,i]=(double)GreyImage[j,i];
//4 bytes per pixel
imagePointer1 += 4;
}//end for j
//4 bytes per pixel
imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
}//end for i
}//end unsafe
image.UnlockBits(bitmapData1);
return;
}
/// <summary>
/// Display Subroutine for Inverse DCT Image
/// </summary>
/// <param name="image">IDCT Coefficients Array</param>
/// <returns>Bitmap for DCT Inverse</returns>
public Bitmap DisplayImage(double[,] image)
{
int i, j;
Bitmap output = new Bitmap(image.GetLength(0), image.GetLength(1));
BitmapData bitmapData1 = output.LockBits(new Rectangle(0, 0, image.GetLength(0), image.GetLength(1)),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* imagePointer1 = (byte*)bitmapData1.Scan0;
for (i = 0; i < bitmapData1.Height; i++)
{
for (j = 0; j < bitmapData1.Width; j++)
{
imagePointer1[0] = (byte)image[j, i];
imagePointer1[1] = (byte)image[j, i];
imagePointer1[2] = (byte)image[j, i];
imagePointer1[3] = 255;
//4 bytes per pixel
imagePointer1 += 4;
}//end for j
//4 bytes per pixel
imagePointer1 += (bitmapData1.Stride - (bitmapData1.Width * 4));
}//end for i
}//end unsafe
output.UnlockBits(bitmapData1);
return output;// col;
}
public static Bitmap DisplayMap(int[,] output)
{
int i, j;
Bitmap image = new Bitmap(output.GetLength(0), output.GetLength(1));
BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, output.GetLength(0), output.GetLength(1)),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* imagePointer1 = (byte*)bitmapData1.Scan0;
for (i = 0; i < bitmapData1.Height; i++)
{
for (j = 0; j < bitmapData1.Width; j++)
{
if (output[j, i] < 0)
{
// Changing to Red Color
// Changing to Green Color
imagePointer1[0] = 0;
imagePointer1[1] = 255;
imagePointer1[2] = 0;
}
else if ((output[j, i] >= 0) && (output[j, i] < 50))
{ // Changing to Green Color
imagePointer1[0] = (byte)((output[j, i]) * 4);
imagePointer1[1] = 0;
imagePointer1[2] = 0;
}
else if ((output[j, i] >= 50) && (output[j, i] < 100))
{
imagePointer1[0] = 0;
imagePointer1[1] = (byte)(output[j, i] * 2);
imagePointer1[2] = (byte)(output[j, i] * 2);
}
else if ((output[j, i] >= 100) && (output[j, i] < 255))
{ // Changing to Green Color
imagePointer1[0] = 0;
imagePointer1[1] = (byte)(output[j, i]);
imagePointer1[2] = 0;
}
else if ((output[j, i] > 255))
{ // Changing to Green Color
imagePointer1[0] = 0;
imagePointer1[1] = 0;
imagePointer1[2] = (byte)((output[j, i]) * 0.7);
}
imagePointer1[3] = 255;
//4 bytes per pixel
imagePointer1 += 4;
} //end for j
//4 bytes per pixel
imagePointer1 += (bitmapData1.Stride - (bitmapData1.Width * 4));
} //end for i
} //end unsafe
image.UnlockBits(bitmapData1);
return image; // col;
}
/// <summary>
/// Fast 2D DCT of the Image Array
/// </summary>
public void FastDCT()
{
double[,] temp = new double[Width, Height];
DCTCoefficients = new double[Width, Height];
DCTkernel = new double[Width, Height];
DCTkernel = GenerateDCTmatrix(Order);
temp = Multiply(DCTkernel, Input);
DCTCoefficients = Multiply(temp, Transpose(DCTkernel));
DCTPlotGenerate();
return;
}
/// <summary>
/// Fast 2D IDCT of the stored DCTCoefficients
/// </summary>
public void FastInverseDCT()
{
double[,] temp = new double[Width, Height];
IDCTCoefficients = new double[Width, Height];
DCTkernel = new double[Width, Height];
DCTkernel = Transpose(GenerateDCTmatrix(Order));
temp = Multiply(DCTkernel, DCTCoefficients);
IDCTCoefficients = Multiply(temp, Transpose(DCTkernel));
IDCTImage = DisplayImage(IDCTCoefficients);
return;
}
/// <summary>
/// Generates DCT Matrix for Given Order
/// </summary>
/// <param name="order">Dimension of the matrix</param>
/// <returns>DCT Kernel for given Order</returns>
public double[,] GenerateDCTmatrix(int order)
{
int i, j;
int N;
N = order;
double alpha;
double denominator;
double[,] DCTCoeff = new double[N, N];
for (j = 0; j <= N - 1; j++)
{
DCTCoeff[0, j] = Math.Sqrt(1 / (double)N);
}
alpha = Math.Sqrt(2 / (double)N);
denominator = (double)2 * N;
for (j = 0; j <= N - 1; j++)
for (i = 1; i <= N - 1; i++)
{
DCTCoeff[i, j] = alpha * Math.Cos(((2 * j + 1) * i * 3.14159) / denominator);
}
return (DCTCoeff);
}
private double[,] Multiply(double[,] m1, double[,] m2)
{
int row, col, i, j, k;
row = col = m1.GetLength(0);
double[,] m3 = new double[row, col];
double sum;
for (i = 0; i <= row - 1; i++)
{
for (j = 0; j <= col - 1; j++)
{
Application.DoEvents();
sum = 0;
for (k = 0; k <= row - 1; k++)
{
sum = sum + m1[i, k] * m2[k, j];
}
m3[i, j] = sum;
}
}
return m3;
}
private double[,] Transpose(double[,] m)
{
int i, j;
int Width, Height;
Width=m.GetLength(0);
Height =m.GetLength(1);
double [,] mt=new double [m.GetLength(0),m.GetLength(1)];
for(i=0;i<=Height-1 ;i++)
for (j = 0; j <= Width-1 ; j++)
{
mt[j, i] = m[i, j];
}
return (mt);
}
private void DCTPlotGenerate()
{
int i, j;
int[,] temp = new int[Width, Height];
double[,] DCTLog = new double[Width, Height];
// Compressing Range By taking Log
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
DCTLog[i, j] = Math.Log(1 + Math.Abs((int)DCTCoefficients[i, j]));
}
//Normalizing Array
double min, max;
min = max = DCTLog[1, 1];
for (i = 1; i <= Width - 1; i++)
for (j = 1; j <= Height - 1; j++)
{
if (DCTLog[i, j] > max)
max = DCTLog[i, j];
if (DCTLog[i, j] < min)
min = DCTLog[i, j];
}
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
temp[i, j] = (int)(((float)(DCTLog[i, j] - min) / (float)(max - min)) * 750);
}
DCTMap = DisplayMap(temp);
}
}
}