-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZ_StarReduction.js
541 lines (472 loc) · 17 KB
/
EZ_StarReduction.js
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Copyright (C) 2020-2023 S Dimant (aka darkarchon#4313)
// File name: EZ_StarReduction.js
/*
* ------------------- Version 2020-06-03 --------------------
* tested on PixInsight 1.8.8-5 Ripley
*
* This script allows easy star reduction on images.
* Hands-free flow is
* - create appropriate star mask
* - apply morphological transformation
*
* Copyright information
*
* Made by S Dimant (aka darkarchon#4313)
* Original idea by S Dimant
* Star replacement reduction by Adam Block
*/
// #region "IncludesAndDefines"
#feature-id EZ Processing Suite > EZ Star Reduction
var NAME = 'EZ Star Reduction';
var VERSION = '0.6HF2';
var AUTHOR = "S. Dimant";
#define StarReduction_Morph 0
#define StarReduction_AdamBlock 1
#include "EZ_Common.js"
// VARIABLE NAMING SCHEME
// - Generally, if applicable, variables end with the type of the variable (i.e. View, Window, etc.)
// - Global declared variables use UpperCamelCase
// - local used variables are lowerCamelCase
// - avoid using var inside functions, prefer let
// #region "Main"
function onInit() { }
function onExit() { }
function saveSettings() {
Settings.write("EZStarMask.DownsampleStarNet", 0, CurrentProcessingInfo.downsampleStarnet);
Settings.write("EZStarMask.ReductionIterations", 1, CurrentProcessingInfo.reductionIterations);
Settings.write("EZStarMask.Mode", 1, CurrentProcessingInfo.mode);
}
function generateProcessingInfo() {
let starReductionInfo = new StarReductionInfo();
starReductionInfo.mainViewId = null;
starReductionInfo.starMaskId = null;
starReductionInfo.downsampleStarnet = readFromSettingsOrDefault("EZStarMask.DownsampleStarNet", 0, false);
starReductionInfo.reductionIterations = readFromSettingsOrDefault("EZStarMask.ReductionIterations", 1, 5);
starReductionInfo.generated = false;
starReductionInfo.mode = readFromSettingsOrDefault("EZStarMask.Mode", 1, StarReduction_Morph);
return starReductionInfo;
}
function execute(window) {
if (CurrentProcessingInfo.starMaskId == null) {
writeMessageStart("Missing Star Mask, creating…");
createProcessedStarMask(CurrentProcessingInfo);
writeMessageEnd("Missing Star Mask created.");
}
if(!CurrentProcessingInfo.generated) {
processStarMask(CurrentProcessingInfo.starMaskId);
}
let mainView = window.mainView;
mainView.window.bringToFront();
writeMessageStart("Running StarReduction");
doStarReduction(mainView, View.viewById(CurrentProcessingInfo.starMaskId).window,
CurrentProcessingInfo.reductionIterations, CurrentProcessingInfo.mode);
writeMessageEnd("Done.");
}
function customizeDialog() {
dialog.infoBox.text = "<b>EZ Star Reduction:</b> A script to easily reduce star sizes in a given image. For star mask generation, StarNet++ needs to be installed. If the PixInsight StarNet++ module is not installed, a monochrome StarNet starmask of the image needs to be supplied. The script will automatically create a StarNet starmask if none is supplied.";
dialog.tutorialPrerequisites = ["Image is cropped properly", "Image is stretched to non-linear", "StarNet++ is installed or StarNet++ raw star mask is present"];
dialog.tutorialSteps = ["Select image to apply to", "Press 'Create mask for reduction' or select a raw mask generated by StarNet++", "Adjust Star Reduction iterations if desired", "Run EZ Star Reduction"];
dialog.onEmptyMainView = function() { }
dialog.onSelectedMainView = function(view) {
if(view.computeOrFetchProperty("Median").at(0) < 0.1) {
let result = dialog.showWarningDialog("Median lower 0.1. Unlikely this is a linear image. This script only works on non-linear images.", "Failed to load", "Continue anyway", true);
if(result != 1) {
dialog.mainViewSelector.remove(view);
CurrentProcessingInfo.mainViewId = null;
return;
}
}
if(dialog.starMaskImageSelector.currentView != null)
dialog.starMaskImageSelector.remove(dialog.starMaskImageSelector.currentView);
dialog.starMaskImageSelector.reload();
}
dialog.onExit = function() { }
dialog.canRun = function() {
let canRun = true;
canRun = canRun && CurrentProcessingInfo.mainViewId != null;
if(CurrentProcessingInfo.mode == StarReduction_AdamBlock) canRun = canRun && getStarNet() != null;
else canRun = canRun && (getStarNet() != null || CurrentProcessingInfo.starMaskId != null);
return canRun;
}
dialog.canEvaluate = function() { return dialog.canRun(); }
dialog.starMaskImageSelector = new ViewList(dialog);
with (dialog.starMaskImageSelector) {
toolTip = "Select StarNet generated Star Mask";
onViewSelected = function (value) {
if (value.isNull) {
CurrentProcessingInfo.starMaskId = null;
return;
}
if (value.image.isColor) {
dialog.showWarningDialog("Cannot use RGB Star Mask.");
dialog.starMaskImageSelector.remove(value);
} else {
let mainView = View.viewById(CurrentProcessingInfo.mainViewId);
if(value.image.width != mainView.image.width || value.image.height != mainView.image.height) {
dialog.showWarningDialog("Incompatible mask geometry");
dialog.starMaskImageSelector.remove(value);
return;
} else if(value.id.endsWith("Alt")) {
dialog.showWarningDialog("Cannot use 'Alt' Images. Those are for only for the script.");
dialog.starMaskImageSelector.remove(value);
} else {
CurrentProcessingInfo.starMaskId = value.isPreview ? value.fullId : value.id;
writeMessageBlock("Selected star mask '" + CurrentProcessingInfo.starMaskId + "'");
}
}
}
excludeIdentifiersPattern = "_ez_temp_*";
getMainViews();
}
dialog.starMaskImageLabel = new Label(dialog);
with (dialog.starMaskImageLabel) {
text = "Raw StarNet Star Mask";
scaledMaxWidth = 130;
}
dialog.starMaskImageSizer = new HorizontalSizer();
with (dialog.starMaskImageSizer) {
addItem(dialog.starMaskImageLabel);
addItem(dialog.starMaskImageSelector);
}
dialog.createProcessedStarMaskButton = new PushButton(dialog);
with (dialog.createProcessedStarMaskButton) {
toolTip = "Creates raw star mask";
text = "Create Star Mask for reduction";
icon = dialog.scaledResource(":/icons/forward.png");
bindings = function() {
this.enabled = getStarNet() != null;
}
onClick = function () {
startProcessing();
createRawStarMask(CurrentProcessingInfo);
dialog.starMaskImageSelector.currentView = View.viewById(CurrentProcessingInfo.starMaskId);
stopProcessing();
}
}
dialog.modeLabel = new Label(dialog);
with (dialog.modeLabel) {
text = "Star Reduction Mode";
scaledMaxWidth = 130;
}
dialog.modeComboBox = new ComboBox(dialog);
with (dialog.modeComboBox) {
editEnabled = false;
addItem("Morphological Selection");
addItem("Adam Blocks De-Emphasis Method");
bindings = function() {
this.currentItem = CurrentProcessingInfo.mode;
}
onItemSelected = function (itemIndex) {
CurrentProcessingInfo.mode = itemIndex;
};
}
dialog.modeSizer = new HorizontalSizer();
with (dialog.modeSizer) {
addItem(dialog.modeLabel);
addItem(dialog.modeComboBox);
}
dialog.pixelMathIterationsSlider = new NumericControl(dialog);
with (dialog.pixelMathIterationsSlider) {
label.text = "Star Reduction iterations";
label.minWidth = 160;
setRange(1, 10);
slider.setRange(0, 100);
slider.scaledMinWidth = 50;
setPrecision(0);
bindings = function() {
this.enabled = CurrentProcessingInfo.mode == StarReduction_Morph;
this.setValue(CurrentProcessingInfo.reductionIterations);
}
onValueUpdated = function (value) {
CurrentProcessingInfo.reductionIterations = value;
}
}
dialog.mainControlReplacement = new GroupBox(dialog);
with (dialog.mainControlReplacement) {
sizer = new VerticalSizer;
title = "Options";
sizer.spacing = 5;
sizer.margin = 5;
bindings = function() {
this.enabled = CurrentProcessingInfo.mainViewId != null;
}
sizer.addItem(dialog.starMaskImageSizer);
sizer.addItem(dialog.createProcessedStarMaskButton);
sizer.addItem(dialog.modeSizer);
sizer.addItem(dialog.pixelMathIterationsSlider);
sizer.addStretch();
}
dialog.controlSizer.insertItem(3, dialog.mainControlReplacement);
dialog.controlSizer.removeItem(dialog.mainControl);
dialog.mainControl.hide();
}
// #endregion "Main"
// #region "Do"
function doStarReduction(view, starMaskWindow, iterations, mode) {
view.window.setMask(starMaskWindow);
view.window.maskEnabled = true;
if(mode == StarReduction_Morph) {
writeMessageBlock("Reducing Sars with morphological transformation");
let morph = new MorphologicalTransformation;
morph.operator = MorphologicalTransformation.prototype.Selection;
morph.interlacingDistance = 1;
morph.lowThreshold = 0.000000;
morph.highThreshold = 0.000000;
morph.numberOfIterations = iterations;
morph.amount = 1;
morph.selectionPoint = 0.15;
morph.structureName = "5x5 Circular Structure";
morph.structureSize = 5;
morph.structureWayTable = [ // mask
[[
0x00, 0x01, 0x01, 0x01, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01,
0x00, 0x01, 0x01, 0x01, 0x00
]]
];
morph.executeOn(view);
} else {
writeMessageBlock("Creating starless image for edge replacement");
let orgView = cloneView(view, "_ez_StarReduction_"+view.id+"_StarLess", false);
let starNet = getStarNet();
starNet.mask = false;
starNet.stride = 0;
starNet.executeOn(orgView);
orgView.window.iconize();
writeMessageBlock("Replacing edges");
doPixelMath(view, orgView.id, true);
view.window.bringToFront();
}
view.window.removeMask();
}
function doConvolve(view, sigma, shape) {
let convolve = new Convolution;
convolve.mode = Convolution.prototype.Parametric;
convolve.sigma = sigma;
convolve.shape = shape;
convolve.aspectRatio = 1.00;
convolve.rotationAngle = 0.00;
convolve.filterSource = "";
convolve.rescaleHighPass = false;
convolve.viewId = "";
convolve.executeOn(view);
}
function doBoost(view) {
let boostCurves = new CurvesTransformation;
boostCurves.K = [ // x, y
[0.00000, 0.00000],
[0.41085, 0.67442],
[1.00000, 1.00000]
];
boostCurves.St = CurvesTransformation.prototype.AkimaSubsplines;
boostCurves.executeOn(view);
}
function doBinarize(view) {
let binarize = new Binarize;
binarize.thresholdRK = 0.02000000;
binarize.thresholdG = 0.02000000;
binarize.thresholdB = 0.02000000;
binarize.isGlobal = true;
binarize.executeOn(view);
}
function doDilate(view) {
var dilate = new MorphologicalTransformation;
dilate.operator = MorphologicalTransformation.prototype.Dilation;
dilate.interlacingDistance = 1;
dilate.lowThreshold = 0.000000;
dilate.highThreshold = 0.000000;
dilate.numberOfIterations = 1;
dilate.amount = 1.00;
dilate.selectionPoint = 0.50;
dilate.structureName = "5x5 Circular Structure";
dilate.structureSize = 5;
dilate.structureWayTable = [ // mask
[[
0x00,0x01,0x01,0x01,0x00,
0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,
0x00,0x01,0x01,0x01,0x00
]]
];
dilate.executeOn(view);
}
function doFinalBoost(view) {
let boostCurves = new CurvesTransformation;
boostCurves.K = [ // x, y
[0.00000, 0.00000],
[0.25, 0.5],
[0.5, 1],
[1.00000, 1.00000]
];
boostCurves.St = CurvesTransformation.prototype.AkimaSubsplines;
boostCurves.executeOn(view);
}
//#endregion "Do"
// #region "Create"
function createRawStarMask(starReductionInfo) {
writeMessageStart("Creating raw star mask based on " + starReductionInfo.mainViewId);
// clone main image
let mainImageView = View.viewById(starReductionInfo.mainViewId).window.mainView;
let clonedView = null;
if(mainImageView.image.isColor) {
writeMessageBlock("Original image is color, extracting luminance");
var lumExtraction = new ChannelExtraction;
lumExtraction.colorSpace = ChannelExtraction.prototype.CIELab;
lumExtraction.channels = [
[true, ""],
[false, ""],
[false, ""]
];
lumExtraction.sampleFormat = ChannelExtraction.prototype.SameAsSource;
lumExtraction.executeOn(mainImageView);
clonedView = ImageWindow.activeWindow.currentView;
} else {
clonedView = cloneView(mainImageView, "_ez_StarMask_temp");
}
// apply stf
//doSTF(clonedView);
// apply ht
//doHistogramTransformation(clonedView);
// check downsampling
// downsample if necessary
if (starReductionInfo.downsampleStarnet) {
writeMessageBlock("Downsampling " + clonedView.id);
var downSample = new IntegerResample;
downSample.zoomFactor = -2;
downSample.downsamplingMode = IntegerResample.prototype.Average;
downSample.xResolution = 72.000;
downSample.yResolution = 72.000;
downSample.metric = false;
downSample.forceResolution = false;
downSample.noGUIMessages = false;
downSample.executeOn(clonedView);
}
// run starnet
let starNet = getStarNet();
starNet.mask = true;
starNet.stride = 0;
writeMessageBlock("Running StarNet++ on " + clonedView.id + " - hold tight, this might take a while.");
starNet.executeOn(clonedView);
let oldCloned = clonedView;
clonedView = ImageWindow.activeWindow.currentView;
clonedView.id = oldCloned.id;
oldCloned.window.forceClose();
// upscale if necessary
if (starReductionInfo.downsampleStarnet) {
writeMessageBlock("Upscaling " + clonedView.id);
var upSample = new Resample;
upSample.xSize = mainImageView.image.width / clonedView.image.width;
upSample.ySize = mainImageView.image.height / clonedView.image.height;
upSample.mode = Resample.prototype.RelativeDimensions;
upSample.absoluteMode = Resample.prototype.ForceWidthAndHeight;
upSample.xResolution = 72.000;
upSample.yResolution = 72.000;
upSample.metric = false;
upSample.forceResolution = false;
upSample.interpolation = Resample.prototype.Auto;
upSample.clampingThreshold = 0.30;
upSample.smoothness = 1.50;
upSample.noGUIMessages = false;
upSample.executeOn(clonedView);
}
// reset stf
//doResetSTF(clonedView);
let newClonedView = cloneView(clonedView, "_ez_StarReduction_" + View.viewById(starReductionInfo.mainViewId).window.mainView.id + "_StarMask");
clonedView.window.forceClose();
starReductionInfo.starMaskId = newClonedView.id;
newClonedView.window.iconize();
writeMessageEnd("Raw Mask Creation done");
}
function createProcessedStarMask(starReductionInfo) {
writeMessageStart("Creating processed star mask based on " + starReductionInfo.mainViewId);
createRawStarMask(starReductionInfo);
processStarMask(starReductionInfo.starMaskId, starReductionInfo.mode);
writeMessageEnd("Mask processing done");
CurrentProcessingInfo.generated = true;
}
function processStarMask(starMaskId, mode) {
let modeString = mode == StarReduction_Morph ? "MorphologicalSelection" : "Adam Block Replacement";
writeMessageStart("Processing star mask, mode: " + modeString);
if(mode == StarReduction_Morph) {
applyBoost(starMaskId);
applyConvolute(starMaskId, 2.2, 4);
applyPixelMathMorph(starMaskId);
applyFinalBoost(starMaskId);
} else {
applyBinarize(starMaskId);
applyDilate(starMaskId);
applyConvolute(starMaskId, 4.5, 2.3);
applyPixelMath(starMaskId, CurrentProcessingInfo.mainViewId);
}
writeMessageEnd("Processing complete");
}
// #endregion "Create"
// #region "Apply"
function applyPixelMathMorph(viewId) {
writeMessageBlock("Applying PixelMath to " + viewId);
let view = View.viewById(viewId);
// apply pixelmath
doPixelMath(view, "$T*~$T");
}
function applyConvolute(viewId, sigma, shape) {
writeMessageBlock("Applying Convolute to " + viewId);
let view = View.viewById(viewId);
// apply convolution
// sigma 4.5, shape 2
doConvolve(view, sigma, shape);
}
function applyBoost(viewId) {
writeMessageBlock("Applying boost to " + viewId);
let view = View.viewById(viewId);
// apply curvestransformation
doBoost(view);
}
function applyFinalBoost(viewId) {
writeMessageBlock("Applying final boost to " + viewId);
let view = View.viewById(viewId);
// apply curvestransformation
doFinalBoost(view);
}
function applyBinarize(viewId) {
writeMessageBlock("Applying binarize to " + viewId);
let view = View.viewById(viewId);
doBinarize(view);
}
function applyDilate(viewId) {
writeMessageBlock("Applying dilate to " + viewId);
let view = View.viewById(viewId);
doDilate(view);
}
function applyPixelMath(viewId, mainViewId) {
writeMessageBlock("Applying PixelMath to " + viewId);
let view = View.viewById(viewId);
let mainView = View.viewById(mainViewId);
let clonedView = mainView;
if(mainView.image.isColor) {
writeMessageBlock("Original image is color, extracting luminance");
var lumExtraction = new ChannelExtraction;
lumExtraction.colorSpace = ChannelExtraction.prototype.CIELab;
lumExtraction.channels = [
[true, ""],
[false, ""],
[false, ""]
];
lumExtraction.sampleFormat = ChannelExtraction.prototype.SameAsSource;
lumExtraction.executeOn(mainView);
clonedView = ImageWindow.activeWindow.currentView;
}
doPixelMath(view, "$T-{0}".format(clonedView.id));
if(clonedView != mainView) clonedView.window.forceClose();
}
// #endregion "Apply"
function StarReductionInfo() {
this.mainViewId = null;
this.starMaskId = null;
this.downsampleStarnet = null;
this.reductionIterations = null;
}
StarReductionInfo.prototype = new ProcessingInfo();
main();