/
Program.cs
266 lines (251 loc) · 11.6 KB
/
Program.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
using Emgu.CV;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace RasDenoise
{
class Program
{
static void Main(string[] args)
{
Debug.Listeners.Add(new ConsoleTraceListener());
AppDomain appCurr = AppDomain.CurrentDomain;
appCurr.AssemblyResolve += (object sender, ResolveEventArgs eventArgs) => {
Console.WriteLine("## "+eventArgs.Name);
return null;
};
appCurr.AssemblyLoad += (object sender, AssemblyLoadEventArgs eventArgs) => {
Console.WriteLine("!! "+eventArgs.LoadedAssembly.FullName);
};
if (args.Length < 1) {
Usage();
return;
}
if (!ParseMethod(args)) {
return;
}
//windows pops up a dialog on a crash - using try-catch to suppress that
try {
MainMain(args);
} catch(SEHException se) {
//SEHException is annoying .. not sure how to get the 'native' underlying crash information yet.
Console.Error.WriteLine(se.ToString());
} catch(Exception e) {
Console.Error.WriteLine(e.ToString());
}
}
enum MethodType { None = -1, Help=0, NlMeans=1, NlMeansColored=2, Dct=3, TVL1=4, DFTForward=5, DFTInverse=6 }
//http://www.emgu.com/wiki/files/3.0.0/document/html/a4912b79-7f4b-b67a-1822-08e0bff036bd.htm
static void Usage(MethodType help = MethodType.None)
{
StringBuilder sb = new StringBuilder();
sb
.AppendLine("Usage: "+nameof(RasDenoise)+" (method) [options] [file1 file2 ...]")
.AppendLine(" Methods: ")
.AppendLine(" [0] Help Show full help")
.AppendLine(" [1] NlMeans Denoising using Non-local Means algorithm")
.AppendLine(" [2] NlMeansColored Denoising using Non-local Means algorithm (modified for color)")
.AppendLine(" [3] Dct Simple dct-based denoising")
.AppendLine(" [4] TVL1 Denoising via primal-dual algorithm")
.AppendLine(" [5] DFTForward Transform image using forward fourier transform")
.AppendLine(" [6] DFTInverse Transform image(s) using inverse fourier transform")
;
if (help >= MethodType.Help) {
sb
.AppendLine()
.AppendLine(" Additional Information:")
;
if (help == MethodType.Help || help == MethodType.NlMeans) {
sb
.AppendLine()
.AppendLine(" [1] NlMeans")
.Append (" ").AppendWrap(2,"Perform image denoising using Non-local Means Denoising algorithm: http://www.ipol.im/pub/algo/bcm_non_local_means_denoising/ with several computational optimizations. Noise expected to be a gaussian white noise.")
.AppendLine()
.Append (" (input image) ").AppendWrap(24,"Input 8-bit 1-channel, 2-channel or 3-channel image.")
.Append (" [output image] ").AppendWrap(24,"Output image")
.Append (" -h [float = 3.0] ").AppendWrap(24,"Parameter regulating filter strength. Big h value perfectly removes noise but also removes image details, smaller h value preserves details but also preserves some noise.")
.Append (" -t [int = 7] ").AppendWrap(24,"Size in pixels of the template patch that is used to compute weights. Should be odd.")
.Append (" -s [int = 21] ").AppendWrap(24,"Size in pixels of the window that is used to compute weighted average for given pixel. Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time.")
;}
if (help == MethodType.Help || help == MethodType.NlMeansColored) {
sb
.AppendLine()
.AppendLine(" [2] NlMeansColored")
.Append (" ").AppendWrap(2,"Perform image denoising using Non-local Means Denoising algorithm (modified for color image): http://www.ipol.im/pub/algo/bcm_non_local_means_denoising/ with several computational optimizations. Noise expected to be a gaussian white noise. The function converts image to CIELAB colorspace and then separately denoise L and AB components with given h parameters using fastNlMeansDenoising function.")
.AppendLine()
.Append (" (input image) ").AppendWrap(24,"Input 8-bit 1-channel, 2-channel or 3-channel image.")
.Append (" [output image] ").AppendWrap(24,"Output image")
.Append (" -h [float = 3.0] ").AppendWrap(24,"Parameter regulating filter strength. Big h value perfectly removes noise but also removes image details, smaller h value preserves details but also preserves some noise.")
.Append (" -c [float = 3.0] ").AppendWrap(24,"The same as -h but for color components. For most images value equals 10 will be enought to remove colored noise and do not distort colors.")
.Append (" -t [int = 7] ").AppendWrap(24,"Size in pixels of the template patch that is used to compute weights. Should be odd.")
.Append (" -s [int = 21] ").AppendWrap(24,"Size in pixels of the window that is used to compute weighted average for given pixel. Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time.")
;}
if (help == MethodType.Help || help == MethodType.Dct) {
sb
.AppendLine()
.AppendLine(" [3] Dct")
.Append (" ").AppendWrap(2,"The function implements simple dct-based denoising, link: http://www.ipol.im/pub/art/2011/ys-dct/.")
.AppendLine()
.Append (" (input image) ").AppendWrap(24,"Source image")
.Append (" [output image] ").AppendWrap(24,"Output image")
.Append (" -s (double) ").AppendWrap(24,"Expected noise standard deviation")
.Append (" -p [int = 16] ").AppendWrap(24,"Size of block side where dct is computed")
;}
if (help == MethodType.Help || help == MethodType.TVL1) {
sb
.AppendLine()
.AppendLine(" [4] TVL1")
.Append (" ").AppendWrap(2,"The function implements simple dct-based denoising, link: http://www.ipol.im/pub/art/2011/ys-dct/.")
.AppendLine()
.Append (" (file) [file] [...] ").AppendWrap(24,"One or more noised versions of the image that is to be restored.")
.Append (" -o [output image] ").AppendWrap(24,"Output image")
.Append (" -l (double) ").AppendWrap(24,"Corresponds to in the formulas above. As it is enlarged, the smooth (blurred) images are treated more favorably than detailed (but maybe more noised) ones. Roughly speaking, as it becomes smaller, the result will be more blur but more sever outliers will be removed.")
.Append (" -n (int) ").AppendWrap(24,"Number of iterations that the algorithm will run. Of course, as more iterations as better, but it is hard to quantitatively refine this statement, so just use the default and increase it if the results are poor.")
;}
if (help == MethodType.Help || help == MethodType.DFTForward) {
sb
.AppendLine()
.AppendLine(" [5] DFTForward")
.Append (" ").AppendWrap(2,"Decomposes an input image into frequency magnitude and phase components.")
.AppendLine()
.Append (" (input image) ").AppendWrap(24,"Source image")
.Append (" -o [output image] ").AppendWrap(24,"Output image (produces 2 files)")
;}
if (help == MethodType.Help || help == MethodType.DFTInverse) {
sb
.AppendLine()
.AppendLine(" [6] DFTInverse")
.Append (" ").AppendWrap(2,"Recomposes an image from frequency magnitude and phase components.")
.AppendLine()
.Append (" [output image] ").AppendWrap(24,"Output image")
.Append (" -m (input image) ").AppendWrap(24,"Input magnitude image")
.Append (" -p (input image) ").AppendWrap(24,"Input phase image")
.Append (" -mi (number) ").AppendWrap(24,"magnitude range min")
.Append (" -mx (number) ").AppendWrap(24,"magnitude range max")
.Append (" -pi (number) ").AppendWrap(24,"phase range min")
.Append (" -px (number) ").AppendWrap(24,"phase range max")
;}
}
Console.WriteLine(sb.ToString());
}
static MethodType Method = MethodType.None;
//options variables and their defaults
//static List<string> fileList = new List<string>();
//static string outFile = null;
//static double m1h = 3.0;
//static int m1templateWindowSize = 7;
//static int m1searchWindowSize = 21;
//static double m2h = 3.0;
//static double m2hColor = 3.0;
//static int m2templateWindowSize = 7;
//static int m2searchWindowSize = 21;
//static double? m3sigma;
//static int m3psize = 16;
//static double? m4lambda;
//static int? m4niters;
//static double? m6mi;
//static double? m6mx;
//static double? m6pi;
//static double? m6px;
static bool ParseMethod(string[] args)
{
string m = args[0];
if (m == "--help" || m == "/?" || m.EqualsIC("help") || m == "0") {
MethodType meth = MethodType.Help;
if (args.Length > 1) {
if (!Enum.TryParse(args[1],true,out meth)) {
meth = MethodType.Help;
}
}
Usage(meth);
return false;
}
if (!Enum.TryParse(m,true,out Method)) {
Console.WriteLine("Bad method "+m);
return true;
}
return true;
}
static void MainMain(string[] args)
{
//not sure how beneficial this is
CvInvoke.RedirectError(CvInvoke.CvErrorHandlerIgnoreError,IntPtr.Zero,IntPtr.Zero);
//string inFile = null;
//inFile = fileList.FirstOrDefault();
//if (String.IsNullOrWhiteSpace(inFile)) {
// Console.WriteLine("Missing input file");
// return;
//}
//if(String.IsNullOrEmpty(outFile))
//{
// if (fileList.Count > 1) {
// outFile = fileList[1];
// } else {
// outFile = Path.GetFileNameWithoutExtension(inFile)
// +".out"+Path.GetExtension(inFile);
// }
//}
if (Method == MethodType.NlMeans) {
var ments = NlMeansArgs.Parse(args);
Methods.NlMeans(ments);
}
else if (Method == MethodType.NlMeansColored) {
var ments = NlMeansColoredArgs.Parse(args);
Methods.NlMeansColored(ments);
}
else if (Method == MethodType.Dct) {
var ments = DctArgs.Parse(args);
if (!ments.sigma.HasValue) {
Console.WriteLine("option -s is required");
return;
}
Methods.Dct(ments);
}
else if (Method == MethodType.TVL1) {
var ments = TVL1Args.Parse(args);
if (!ments.lambda.HasValue) {
Console.WriteLine("option -l is required");
return;
}
if (!ments.niters.HasValue) {
Console.WriteLine("option -n is required");
return;
}
Methods.TVL1(ments);
}
//else if (Method == MethodType.DFTForward) {
// Methods.DFTForward(inFile,outFile);
//}
//else if (Method == MethodType.DFTInverse) {
// if (fileList.Count < 2 || fileList[0] == null || fileList[1] == null) {
// Console.WriteLine("your must specify both a magnitude image and a phase image");
// return;
// }
// if (fileList.Count > 2) {
// outFile = fileList[2];
// } else {
// outFile = Path.GetFileNameWithoutExtension(fileList[0])+".inv.png";
// }
//
// Debug.WriteLine("mi="+m6mi.GetValueOrDefault()
// +" mx="+m6mx.GetValueOrDefault()
// +" pi="+m6pi.GetValueOrDefault()
// +" px="+m6px.GetValueOrDefault()
// );
//
// if (!m6mi.HasValue || !m6mx.HasValue || !m6pi.HasValue || !m6px.HasValue) {
// Console.WriteLine("you must specify magnitude and phase ranges");
// return;
// }
//
// Methods.DFTInverse(fileList,outFile,m6mi.Value,m6mx.Value,m6pi.Value,m6px.Value);
// return;
//}
}
}
}