Yeah, but the 5% and 95% levels according to -linear-stretch will be different for different images, depending on each image's histogram, right? This is not what I want. I need the exact same gray values for -level applied to each image. In some images, the black_point I use might only correspond to the 2% level according to -linear-stretch, for example. I guess the equivalent of what I want is to combine all my images into one big image, apply -level, then split them back into individual images.
If you do not have too many images, you can append them all, apply -linear-stretch, then crop them back. Not a good solution as you lose track of where to crop afterwards, unless all the images are the same size. Then cropping should not be that hard.
I understand now why you want to get the gray levels from the cumulative percentile of the total of all the images. However, I do not think that applying the same graylevels using -level will be optimal for all the images. But that is where I lose you on your ultimate goal.
This will compute a 256 bin cumulative histogram vs graylevel on unix platform using 8-bit grayscale range. The graylevel is first, then the percentile
img="zelda3.jpg"
totpix=`convert $img -format "%[fx:w*h]" info:`
convert $img -depth 8 -format "%c" -define histogram:unique-colors=true histogram:info:- |\
tr -cs '0-9\012' ' ' |\
awk -v totpix="$totpix" '# AWK to generate a cumulative histogram
{ bin[int($2)] += $1 }
END { for (i=0;i<256;i++) {cum += bin
/totpix; print i, 100*cum; } } '
Code: Select all
0 0.0305176
1 0.0488281
2 0.0488281
3 0.0793457
4 0.0793457
5 0.0854492
6 0.109863
7 0.115967
8 0.128174
9 0.146484
10 0.177002
11 0.195312
12 0.22583
13 0.244141
14 0.268555
15 0.280762
16 0.311279
17 0.311279
18 0.32959
19 0.341797
20 0.360107
21 0.390625
22 0.396729
23 0.415039
24 0.445557
25 0.482178
26 0.531006
27 0.585938
28 0.646973
29 0.701904
30 0.726318
31 0.769043
32 0.817871
33 0.878906
34 0.933838
35 0.964355
36 1.00098
37 1.0437
38 1.08032
39 1.12915
40 1.2146
41 1.25732
42 1.28174
43 1.33667
44 1.3855
45 1.44653
46 1.52588
47 1.61133
48 1.64795
49 1.68457
50 1.75781
51 1.85547
52 1.9104
53 1.95923
54 2.05688
55 2.13013
56 2.18506
57 2.22778
58 2.33765
59 2.39258
60 2.44751
61 2.53906
62 2.62451
63 2.69165
64 2.78931
65 2.86255
66 2.948
67 3.04565
68 3.13721
69 3.21655
70 3.32642
71 3.41797
72 3.52783
73 3.6377
74 3.75366
75 3.91846
76 4.06494
77 4.21143
78 4.35791
79 4.52271
80 4.66919
81 4.82788
82 4.96826
83 5.08423
84 5.24902
85 5.37109
86 5.51758
87 5.60303
88 5.70679
89 5.88379
90 6.04248
91 6.19507
92 6.31104
93 6.48193
94 6.62842
95 6.89087
96 7.01904
97 7.32422
98 7.49512
99 7.73315
100 8.01392
101 8.20312
102 8.38623
103 8.61206
104 8.83789
105 9.0332
106 9.25293
107 9.52148
108 9.80835
109 10.0464
110 10.2539
111 10.5835
112 10.907
113 11.145
114 11.4014
115 11.676
116 11.9324
117 12.2437
118 12.5183
119 12.793
120 13.1287
121 13.4827
122 13.7268
123 14.0381
124 14.2456
125 14.4836
126 14.8071
127 15.0391
128 15.3198
129 15.6799
130 16.0156
131 16.3635
132 16.7419
133 17.2058
134 17.6025
135 18.0664
136 18.4387
137 18.927
138 19.4092
139 19.9829
140 20.5139
141 21.0388
142 21.5942
143 22.1313
144 22.7112
145 23.3215
146 23.9624
147 24.5667
148 25.1892
149 25.9338
150 26.8738
151 27.7832
152 28.4424
153 29.2603
154 30.0476
155 30.9448
156 31.897
157 32.8247
158 33.9722
159 34.9854
160 36.2366
161 37.4146
162 38.5742
163 39.7278
164 40.7593
165 41.7725
166 42.7002
167 43.7317
168 44.8364
169 46.0999
170 47.1069
171 48.2483
172 49.3103
173 50.2197
174 50.8179
175 51.5747
176 52.2583
177 52.8015
178 53.5461
179 54.4067
180 55.127
181 55.957
182 56.7932
183 57.6355
184 58.4656
185 59.2834
186 60.022
187 60.7788
188 61.615
189 62.3718
190 63.1775
191 64.0381
192 64.8071
193 65.741
194 66.626
195 67.2974
196 68.1946
197 68.8843
198 69.6289
199 70.343
200 70.9534
201 71.6797
202 72.3938
203 73.0774
204 73.7061
205 74.3896
206 75.1953
207 75.9216
208 76.7212
209 77.4597
210 78.1433
211 78.7598
212 79.3884
213 80.1819
214 80.8167
215 81.4148
216 82.196
217 82.8674
218 83.5876
219 84.2468
220 84.8633
221 85.3943
222 85.968
223 86.4319
224 86.9934
225 87.5366
226 88.147
227 88.7207
228 89.2212
229 89.7461
230 90.1855
231 90.6128
232 91.1255
233 91.5894
234 92.0349
235 92.511
236 92.9504
237 93.396
238 93.7622
239 94.0735
240 94.397
241 94.6899
242 94.9524
243 95.2332
244 95.4712
245 95.7275
246 95.9961
247 96.2158
248 96.4478
249 96.6553
250 96.8689
251 97.0886
252 97.3145
253 97.5647
254 97.7966
255 100
Or this will convert the 256 graylevel values to 16-bit range
img="zelda3.jpg"
totpix=`convert $img -format "%[fx:w*h]" info:`
convert $img -depth 8 -format "%c" -define histogram:unique-colors=true histogram:info:- |\
tr -cs '0-9\012' ' ' |\
awk -v totpix="$totpix" '# AWK to generate a cumulative histogram
{ bin[int($2)] += $1 }
END { for (i=0;i<256;i++) {cum += bin
/totpix; print 65535*i/255, 100*cum; } } '
Code: Select all
0 0.0305176
257 0.0488281
514 0.0488281
771 0.0793457
1028 0.0793457
1285 0.0854492
1542 0.109863
1799 0.115967
2056 0.128174
2313 0.146484
2570 0.177002
2827 0.195312
3084 0.22583
3341 0.244141
3598 0.268555
3855 0.280762
4112 0.311279
4369 0.311279
4626 0.32959
4883 0.341797
5140 0.360107
5397 0.390625
5654 0.396729
5911 0.415039
6168 0.445557
6425 0.482178
6682 0.531006
6939 0.585938
7196 0.646973
7453 0.701904
7710 0.726318
7967 0.769043
8224 0.817871
8481 0.878906
8738 0.933838
8995 0.964355
9252 1.00098
9509 1.0437
9766 1.08032
10023 1.12915
10280 1.2146
10537 1.25732
10794 1.28174
11051 1.33667
11308 1.3855
11565 1.44653
11822 1.52588
12079 1.61133
12336 1.64795
12593 1.68457
12850 1.75781
13107 1.85547
13364 1.9104
13621 1.95923
13878 2.05688
14135 2.13013
14392 2.18506
14649 2.22778
14906 2.33765
15163 2.39258
15420 2.44751
15677 2.53906
15934 2.62451
16191 2.69165
16448 2.78931
16705 2.86255
16962 2.948
17219 3.04565
17476 3.13721
17733 3.21655
17990 3.32642
18247 3.41797
18504 3.52783
18761 3.6377
19018 3.75366
19275 3.91846
19532 4.06494
19789 4.21143
20046 4.35791
20303 4.52271
20560 4.66919
20817 4.82788
21074 4.96826
21331 5.08423
21588 5.24902
21845 5.37109
22102 5.51758
22359 5.60303
22616 5.70679
22873 5.88379
23130 6.04248
23387 6.19507
23644 6.31104
23901 6.48193
24158 6.62842
24415 6.89087
24672 7.01904
24929 7.32422
25186 7.49512
25443 7.73315
25700 8.01392
25957 8.20312
26214 8.38623
26471 8.61206
26728 8.83789
26985 9.0332
27242 9.25293
27499 9.52148
27756 9.80835
28013 10.0464
28270 10.2539
28527 10.5835
28784 10.907
29041 11.145
29298 11.4014
29555 11.676
29812 11.9324
30069 12.2437
30326 12.5183
30583 12.793
30840 13.1287
31097 13.4827
31354 13.7268
31611 14.0381
31868 14.2456
32125 14.4836
32382 14.8071
32639 15.0391
32896 15.3198
33153 15.6799
33410 16.0156
33667 16.3635
33924 16.7419
34181 17.2058
34438 17.6025
34695 18.0664
34952 18.4387
35209 18.927
35466 19.4092
35723 19.9829
35980 20.5139
36237 21.0388
36494 21.5942
36751 22.1313
37008 22.7112
37265 23.3215
37522 23.9624
37779 24.5667
38036 25.1892
38293 25.9338
38550 26.8738
38807 27.7832
39064 28.4424
39321 29.2603
39578 30.0476
39835 30.9448
40092 31.897
40349 32.8247
40606 33.9722
40863 34.9854
41120 36.2366
41377 37.4146
41634 38.5742
41891 39.7278
42148 40.7593
42405 41.7725
42662 42.7002
42919 43.7317
43176 44.8364
43433 46.0999
43690 47.1069
43947 48.2483
44204 49.3103
44461 50.2197
44718 50.8179
44975 51.5747
45232 52.2583
45489 52.8015
45746 53.5461
46003 54.4067
46260 55.127
46517 55.957
46774 56.7932
47031 57.6355
47288 58.4656
47545 59.2834
47802 60.022
48059 60.7788
48316 61.615
48573 62.3718
48830 63.1775
49087 64.0381
49344 64.8071
49601 65.741
49858 66.626
50115 67.2974
50372 68.1946
50629 68.8843
50886 69.6289
51143 70.343
51400 70.9534
51657 71.6797
51914 72.3938
52171 73.0774
52428 73.7061
52685 74.3896
52942 75.1953
53199 75.9216
53456 76.7212
53713 77.4597
53970 78.1433
54227 78.7598
54484 79.3884
54741 80.1819
54998 80.8167
55255 81.4148
55512 82.196
55769 82.8674
56026 83.5876
56283 84.2468
56540 84.8633
56797 85.3943
57054 85.968
57311 86.4319
57568 86.9934
57825 87.5366
58082 88.147
58339 88.7207
58596 89.2212
58853 89.7461
59110 90.1855
59367 90.6128
59624 91.1255
59881 91.5894
60138 92.0349
60395 92.511
60652 92.9504
60909 93.396
61166 93.7622
61423 94.0735
61680 94.397
61937 94.6899
62194 94.9524
62451 95.2332
62708 95.4712
62965 95.7275
63222 95.9961
63479 96.2158
63736 96.4478
63993 96.6553
64250 96.8689
64507 97.0886
64764 97.3145
65021 97.5647
65278 97.7966
65535 100