-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAutoIntegrateBanding.js
More file actions
357 lines (314 loc) · 15.1 KB
/
Copy pathAutoIntegrateBanding.js
File metadata and controls
357 lines (314 loc) · 15.1 KB
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
/*
CanonBandingReduction.js v0.9.1
Reduces Banding in Canon DSLR images.
Copied from CanonBandingReduction.js script.
Renamed by Jarmo Ruuth for AutoIntegrate script.
Thanks to Ric Anderson for pointing the debug mode performance problem.
The problem has been discussed for example in http://tech.groups.yahoo.com/group/digital_astro/message/126102,
cannot be fixed by image normalization with flats or darks, is not caused by problems in power supplies or
electromagnetic noise, and affects different camera exemplars to differing extents.
The general idea is to fix this by flattening the background looking at the median of each row. Additional
tweaks try to avoid overcorrection caused by bright sections of the image. See BandingEngine.doit() for the details.
This works very nicely for my Canon EOS40D. Let me know how it works for yours. Feel free to improve the script!
Copyright (C) 2009-2013 Georg Viehoever
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// @class Does the work using the params in data.
///
/// GUI functions are in a separate class, so this engine
/// can be used on its own. Use the different set..() function
/// to set the parameters, because these also schedule necessary recomputations.
/// Use getResult() to trigger processing and retrieve
/// result
#ifndef AUTOINTEGRATEBANDING_JS
#define AUTOINTEGRATEBANDING_JS
class AutoIntegrateBandingEngine extends Object {
constructor() {
super();
this.DEBUGGING_MODE_ON = false;
// init members
this.targetImage=null; // image to which operation is done
this.convertedTargetImage=null; // target image converted to float or doubles
this.fixupImage=null; // image that contains the fix values, assuming dAmount 1.0
this.resultImage=null; //result image
//numeric params with their defaults
this.dAmount=1.0; // image is fixed by adding fixImage*dAmount
this.bDoHighlightProtect=false; // toggle for protection from bright pixels
this.dSigma=1.0; //factor for sigma in hihglight protection
/// retrieve settings from previous runs, stored in Parameters
this.importParameters = () => {
if ( Parameters.has( "amount" ) )
this.dAmount = Parameters.getReal( "amount" );
if ( Parameters.has( "highlightProtect" ) )
this.bDoHighlightProtect = Parameters.getBoolean( "highlightProtect" );
if ( Parameters.has( "sigma" ) )
this.dSigma = Parameters.getReal( "sigma" );
};
/// Store current settings for use with later runs.
this.exportParameters = () => {
Parameters.set( "amount", this.dAmount );
Parameters.set( "highlightProtect", this.bDoHighlightProtect );
Parameters.set( "sigma", this.dSigma );
};
// flags to show if something needs to be recomputed. Managed by set...() and do...() functions
this.bRedoConvert=true;
this.bRedoStatistics=true;
this.bRedoResult=true;
/// function to set new target image
this.setTargetImage=function(targetImage){
if (this.targetImage!=targetImage){
if ( this.DEBUGGING_MODE_ON ){
console.writeln("Setting targetImage=",targetImage);
}
this.targetImage=targetImage;
this.bRedoConvert=true;
this.bRedoStatistics=true;
this.bRedoResult=true;
} //if setting changed
}; //setTargetImage()
/// function to set new amount value
this.setAmount=function(amountValue){
if (this.dAmount!=amountValue){
this.dAmount=amountValue;
this.bRedoResult=true;
} //if setting changed
}; //setAmount()
/// function to set highlightProtect mode
this.setHighlightProtect=function(doHighlightProtect,sigmaValue){
if ( this.DEBUGGING_MODE_ON ){
console.writeln("SetHighlightProtect=",doHighlightProtect,sigmaValue);
}
if((this.bDoHighlightProtect!=doHighlightProtect)||
(doHighlightProtect &&(this.dSigma!=sigmaValue))){
//something relevant changed
this.bDoHighlightProtect=doHighlightProtect;
this.dSigma=sigmaValue;
this.bRedoStatistics=true;
this.bRedoResult=true;
} //if changed
}; //setHighlightProtect()
/// set status function that is called by time consuming operations for progress reporting and cancel query
///
/// Operations include the image statistics and the actual correction. Can be used for progress reporting and canceling long operations.
/// @param statusFunction is a function that receives a string (that can be displayed somewhere)
/// and that returns a boolean. The boolean is true if operation can continue, if false
/// the opertion is aborted ASAP. bForceUpdate can be used to force a GUI update.
/// If ==null, a default function doing nothing is set.
this.setStatusFunction=function(statusFunction) {
if (statusFunction==null){
// set default doing nothing
this.statusFunction=function(statusString,bForceUpdate) {
if ( this.DEBUGGING_MODE_ON ){
console.writeln("statusFunction, string=",statusString, "bForceUpdate=",bForceUpdate);
}
return true;
} //statusFunction()
}else{
this.statusFunction=statusFunction;
}
}; //function setStatusFunction()
// set default status function
this.setStatusFunction(null);
/// function converts target image to float type if necessary
this.doConvertImage=function(){
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.doConvertImage(), targetImage=",this.targetImage);
}
if(!this.statusFunction("Converting image to float type",true)) return;
if (this.bRedoConvert){
if (this.targetImage.isNull){
if ( this.DEBUGGING_MODE_ON ){
console.writeln("doConvertImage(). targetImage is null");
}
this.convertedImage=null;
}else{
// convert image to floating point format, since int would not handle negatives or overflows
if ( this.targetImage.sampleType == PixelSampleType.Integer ){
this.convertedImage = new Image( this.targetImage.width, this.targetImage.height,
this.targetImage.numberOfChannels, this.targetImage.colorSpace,
(this.targetImage.bitsPerSample < 32) ? 32 : 64, PixelSampleType.Float );
this.targetImage.resetSelections();
this.convertedImage.resetSelections()
this.convertedImage.assign( this.targetImage );
}else{
// no conversion required
this.targetImage.resetSelections();
this.convertedImage=this.targetImage;
} //if conversion necessary
} //if target=null
this.bRedoConvert=false;
this.statusFunction("Conversion done",true);
} // if redoConvert
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.doConvertImage() done");
}
}; //convertImage()
/// do statistics and medians if necessary. Create fixImage.
this.doStatistics=function(){
if ( this.DEBUGGING_MODE_ON ){
var now=Date.now();
console.writeln("BandingEngine.doStatistics()");
}
if (this.bRedoStatistics){
this.doConvertImage();
var targetImage=this.convertedImage;
if (targetImage.isNull) return;
this.fixImage=new Image( targetImage.width, targetImage.height,
targetImage.numberOfChannels, targetImage.colorSpace,
targetImage.bitsPerSample, PixelSampleType.Float );
var fixImage=this.fixImage;
targetImage.resetSelections();
fixImage.resetSelections();
var targetHeight=targetImage.height;
var targetWidth=targetImage.width;
// for each channel, determine global statistics (average, deviation).
// for each line, determine line average (bounded by possible highlight protection).
for (var chan=0; chan<targetImage.numberOfChannels;++chan){
targetImage.resetSelections();
targetImage.selectedChannel=chan;
fixImage.selectedChannel=chan;
var rGBGlobalMedian=targetImage.median();
//estimate noise
//var globalSigma=targetImage.stdDev();
//According to http://en.wikipedia.org/wiki/Median_absolute_deviation,
//this is a more robust estimator for sigma than stdDev(). Inspection of typical
//images appears to confirm that.
// FIXME maybe use new noise estimate functionality as used by imageIntegration.
var dGlobalSigma=1.4826*targetImage.avgDev();
// Chec image statistics. If highlight protect, ignore unusually bright pixels.
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.doStatistics(), dGlobalSigma=",dGlobalSigma,", dSigma=",this.dSigma, ", doHiglightProtect=",this.bDoHighlightProtect);
}
//now determine the medians for each row.
var lineRect=new Rect(targetWidth,1);
for (var row=0; row<targetHeight;++row) {
var statusString="Computing statistics for channel "+chan+", row "+row;
if(!this.statusFunction(statusString,false)) {
return;
}
lineRect.moveTo(0,row);
targetImage.selectedRect=lineRect;
fixImage.selectedRect=lineRect;
var rGBRowMedian=targetImage.median();
// store fix factor into fixImage
var fixFactor=rGBGlobalMedian-rGBRowMedian;
fixImage.fill(fixFactor); //much faster than apply()!
} // for row
} //for channel
targetImage.resetSelections();
fixImage.resetSelections();
this.bRedoStatistics=false;
// if you are interested in seeing the image used for fixing the banding, replace
// the false with true. Note that you can no longer use it for fixing due to the
// fixImage.normalize();
if ( false ){
fixImage.normalize();
var wtmp = new ImageWindow( 1000, 1000, 3,
fixImage.bitsPerSample, fixImage.sampleType == PixelSampleType.Float, fixImage.isColor,"FixImage" );
var v = wtmp.mainView;
v.beginProcess( UndoFlag.NoSwapFile );
v.image.assign( fixImage );
v.endProcess();
wtmp.bringToFront();
}
this.statusFunction("Statistics done",true)
} // if RedoStatistics
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.doStatistics() done");
console.writeln("BandingEngine.doStatistics() required time [ms]=",Date.now()-now);
}
}; //doStatistics()
/// compute the result, doing only the necessary recomputations.
this.doResult=function(){
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.doResult()");
}
if (this.bRedoResult){
this.doStatistics();
var targetImage=this.convertedImage;
if (targetImage.isNull) return;
if(!this.statusFunction("Fixing image",true)) {
return;
}
this.resultImage=new Image( targetImage.width, targetImage.height,
targetImage.numberOfChannels, targetImage.colorSpace,
targetImage.bitsPerSample, PixelSampleType.Float );
targetImage.resetSelections();
var resultImage=this.resultImage;
this.fixImage.resetSelections();
resultImage.resetSelections();
resultImage.assign(this.fixImage);
var dAmount=this.dAmount;
resultImage.apply(dAmount,ImageOp.Mul);
resultImage.apply(targetImage,ImageOp.Add);
// if necessary: rescale data into range from 0.0-1.0
if(!this.statusFunction("Normalizing image",true)) return;
//resultImage.normalize();
//I dont want a normalization here, since this drastically changes the range of the original image.
//I just clip the values between 0.0 and 1.1.
var clipImage=new Image( targetImage.width, targetImage.height,
targetImage.numberOfChannels, targetImage.colorSpace,
targetImage.bitsPerSample, PixelSampleType.Float );
clipImage.fill(0.0);
resultImage.apply(clipImage,ImageOp.Max);
clipImage.fill(1.0);
resultImage.apply(clipImage,ImageOp.Min);
this.statusFunction("Fixing image done",true);
this.bRedoResult=false;
} //if RedoResult
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.doResult() done");
}
}; //doResult()
/// get the current result, doing recomputations if necessary
this.getResult=function(){
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.getResult()");
}
this.doResult();
if(!this.statusFunction("Processing done",true)){
this.statusFunction("Processing aborted",true);
}
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.getResult() done");
}
return this.resultImage;
}; //getResult()
// get the last computed result. If there is no valid result,
// return null. No recompute is done, even if it would be necessary
this.getLastResult=function(){
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.getLastResult()");
}
return this.resultImage;
var result=null;
if (!this.bRedoResult){
result=this.getResult();
}
if ( this.DEBUGGING_MODE_ON ){
console.writeln("BandingEngine.getLastResult() done");
}
return result;
}; // function getLastResult()
/// do the actual work on target view
this.doit=function(targetView){
this.doResult();
// Tell the core application that we are going to change this view.
// Without doing this, we'd have just read-only access to the view's image.
targetView.beginProcess();
targetView.image.resetSelections();
targetView.image.assign( this.resultImage );
// end transaction
targetView.endProcess();
}; //function doit
} // constructor
} //class AutoIntegrateBandingEngine
#endif /* AUTOINTEGRATEBANDING_JS */