-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperimental.cs
171 lines (158 loc) · 7.39 KB
/
Experimental.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
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using AstroPhoto.LibRaw;
namespace MyAstroPhotoLibrary
{
public partial class MainForm : Form
{
private void doExperiment_Click( object sender, EventArgs e )
{
/*using( var buffer = new RawBuffer32() ) {
foreach( var item in currentStack ) {
if( !item.IsExcluded ) {
using( var rawImage = libraw.load_raw( item.FilePath ) ) {
buffer.Add2( rawImage );
}
}
}
pictureBox.Image = buffer.GetResult().RenderBitmapHalfRes( currentCurve, 0 );
}*/
Rectangle rect = currentZoomRect;
int pcount = rect.Width * rect.Height;
int margin = 11;
int window = 2 * margin + 1;
var result = new ushort[pcount * window];
var sumSqr = new uint[pcount * window];
int count = 0;
int channel = 0;
foreach( var item in currentStack ) {
if( !item.IsExcluded ) {
using( var rawImage = libraw.load_raw( item.FilePath ) ) {
//rawImage.ApplyFlat( currentSession.Flat, 1 );
var pixels = rawImage.GetRawPixels( rect );
channel = rawImage.Channel( rect.X, rect.Y );
if( count < window ) {
for( int i = 0; i < pcount; i++ ) {
int i0 = window * i;
result[i0 + count] = pixels[i];
if( count + 1 == window ) {
Array.Sort( result, i0, window );
var p = result[i0 + margin];
sumSqr[i] = (uint)( p * p );
}
}
} else {
for( int i = 0; i < pcount; i++ ) {
int i0 = window * i;
int center = i0 + margin;
var p = pixels[i];
if( p > result[center - 1] && p < result[center + 1] ) {
result[center] += p;
sumSqr[i] += (uint) ( p * p );
} else {
int insertionPoint = i0;
for( int j = i0; j < i0 + window; j++ ) {
if( j == center ) {
continue;
}
if( p <= result[j] ) {
if( j < center ) {
insertionPoint = j;
}
break;
}
insertionPoint = j;
}
if( insertionPoint < center ) {
var _p = result[center - 1];
result[center] += _p;
sumSqr[i] += (uint) ( _p * _p );
for( int j = center - 1; j > insertionPoint; j-- ) {
result[j] = result[j - 1];
}
} else {
var _p = result[center + 1];
result[center] += _p;
sumSqr[i] += (uint) ( _p * _p );
for( int j = center + 1; j < insertionPoint; j++ ) {
result[j] = result[j + 1];
}
}
result[insertionPoint] = p;
}
}
}
count++;
}
}
}
double k = 3.0;
int n = 4;
int sumCount = count - window + 1;
var newpixels = new ushort[pcount];
var newpixels2 = new ushort[pcount];
int skipped = 0;
for( int i = 0; i < pcount; i++ ) {
int i0 = window * i;
int center = i0 + margin;
int sum = result[center];
uint sqr = sumSqr[i];
int _count = sumCount;
while( true ) {
double mean = (double) sum / _count;
double sigma = Math.Sqrt( (double) sqr / _count - mean * mean );
double delta = Math.Max( 1.0, k * sigma );
int min = (int) Math.Round( mean - delta );
int max = (int) Math.Round( mean + delta );
bool finished = true;
for( int j = i0; j < i0 + window; j++ ) {
if( j != center ) {
var p = result[j];
if( p != 0 && p >= min && p <= max) {
sum += p;
sqr += (uint) ( p * p );
result[j] = 0;
_count++;
finished = false;
}
}
}
if( finished || count == _count) {
int sum2 = 0;
int count2 = 0;
for( int j = i0; j < i0 + window; j++ ) {
if( j != center ) {
var p = result[j];
if( p != 0 ) {
sum2 += p;
skipped++;
count2++;
}
}
}
if( sum2 > 0 ) {
sum2 -= 128 * count2;
sum2 *= n;
sum2 /= count2;
}
newpixels2[i] = (ushort) ( sum2 + 128 );
break;
}
}
sum -= 128 *_count;
sum *= n;
sum /= _count;
newpixels[i] = (ushort)( sum + 128 );
}
MessageBox.Show( string.Format( "Skipped {0}", (double)skipped / ( pcount * count ) ) );
var raw = new RawImage( rect.Width, rect.Height, channel, newpixels );
stackedZoom = raw.ExtractRgbImage( new Rectangle( 3, 3, rect.Width - 6, rect.Height - 6 ) );
stackedZoomCount = n;
recalcCurve();
showZoomImage();
}
}
}