Repository navigation
Expand file tree
/
Copy pathplot_detect.py
More file actions
1375 lines (1275 loc) · 61.7 KB
/
Copy pathplot_detect.py
File metadata and controls
1375 lines (1275 loc) · 61.7 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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""plot_detect.py - find the plots of a field trial in an RGB orthomosaic.
A trial is a regular layout: ranges stacked along the crop rows, separated by bare
alleys, and in every range plots side by side across the rows, separated by a
wider gap than the row spacing. The plots are found in three passes, all on one
decimated read of the orthomosaic (pure numpy; the PyInstaller build has no scipy):
1 direction vegetation = excess green on chromatic coordinates above one Otsu
threshold (never below GREEN_FLOOR, so seedlings on bare soil work).
The crop rows give the direction: the angle whose across-row
profiles have the most power at seeder spacings (15-36 cm), summed
over short segments so slightly bent rows and plots drilled with a
different row phase still add up. Coarse 1 deg search, then 0.1 and
0.01 deg. With 4 clicked corners only +-4 deg around them is searched.
2 layout the vegetation is resampled into the trial frame (a = along the rows,
c = across). Along a, the share of vegetation drops in the alleys;
across c, after smoothing over one row spacing, it drops in the gaps
between plots. Crop = above half the plateau (half-height edges: for
rows this is half a row spacing outside the outer rows, the same plot
edge as 'width = rows x spacing'). Runs that are a whole number of
pitches long are split, slivers are dropped.
3 each plot every plot's own sides and ends are measured the same way in a window
around it (the alleys are not always straight, plots in a column can
drift a few cm). All plots get the median size; each plot keeps its
own measured centre, checked against the straight line through its
column. Each plot gets a confidence 0-1 and the reason when it is low:
unclear side / end, little crop (empty or failed plot), or out of line
with its neighbours.
Works best a few weeks after emergence up to just before the canopy closes; it needs
bare gaps between plots across the rows (a trial drilled continuously across has
none: then give 'Plots per range' or the plot width and the plots are laid out at
that pitch inside the detected crop).
Public API
----------
detect_plots(ortho_path, quad=None, n_ranges=0, n_cols=0, plot_w=0, plot_l=0,
log=print, progress=None, cancel=None) -> dict (see the function)
write_report(path, result) -> per-plot confidence CSV
"""
from __future__ import annotations
import csv
import math
import time
import numpy as np
from row_detect import GREEN_FLOOR, _otsu, _smooth
ROW_BAND_M = (0.15, 0.36) # seeder row spacings looked for
STEP_M = 0.02 # working sample (2 cm)
MAX_SAMPLES = 24e6 # cap on the decimated read (memory / speed)
FLAG_BELOW = 0.5 # confidence below this = "check this plot"
class DetectError(Exception):
"""A layout could not be found; the message says what to give instead."""
# ---------------------------------------------------------------------------
# reading
# ---------------------------------------------------------------------------
def _metres_per_unit(crs, lat):
try:
geo = bool(crs is not None and crs.is_geographic)
except Exception:
geo = False
if not geo:
return 1.0, 1.0
return 111320.0 * math.cos(math.radians(lat)), 110540.0
def read_field(path, quad=None, step_m=STEP_M, max_samples=MAX_SAMPLES):
"""One decimated read of the orthomosaic (inside the quad's bounding box when
given). Returns dict(veg bool HxW, valid bool HxW, transform, mx, my, step, crs)."""
import rasterio
from rasterio.enums import Resampling
from rasterio.windows import from_bounds, Window
with rasterio.open(path) as ds:
b = ds.bounds
if quad:
xs, ys = [p[0] for p in quad], [p[1] for p in quad]
px, py = 0.05 * (max(xs) - min(xs)), 0.05 * (max(ys) - min(ys))
x0, x1 = max(min(xs) - px, b.left), min(max(xs) + px, b.right)
y0, y1 = max(min(ys) - py, b.bottom), min(max(ys) + py, b.top)
if not (x1 > x0 and y1 > y0):
raise DetectError("the 4 corners are outside the orthomosaic")
else:
x0, y0, x1, y1 = b.left, b.bottom, b.right, b.top
mx, my = _metres_per_unit(ds.crs, (y0 + y1) / 2.0)
px_m = min(abs(ds.res[0]) * mx, abs(ds.res[1]) * my)
area = (x1 - x0) * mx * (y1 - y0) * my
step = max(step_m, px_m, math.sqrt(area / max_samples))
win = from_bounds(x0, y0, x1, y1, ds.transform)
c0, r0 = max(0, int(math.floor(win.col_off))), max(0, int(math.floor(win.row_off)))
c1 = min(ds.width, int(math.ceil(win.col_off + win.width)))
r1 = min(ds.height, int(math.ceil(win.row_off + win.height)))
win = Window(c0, r0, c1 - c0, r1 - r0)
ow = max(8, int(round((c1 - c0) * abs(ds.res[0]) * mx / step)))
oh = max(8, int(round((r1 - r0) * abs(ds.res[1]) * my / step)))
bands = [1, 2, 3] if ds.count >= 3 else [1, 1, 1]
rgb = ds.read(bands, window=win, out_shape=(3, oh, ow),
resampling=Resampling.average).astype(np.float32)
try:
msk = ds.read_masks(1, window=win, out_shape=(oh, ow)) > 0
except Exception:
msk = np.ones((oh, ow), bool)
tr = ds.window_transform(win) * rasterio.Affine.scale((c1 - c0) / ow, (r1 - r0) / oh)
crs = ds.crs
s = rgb.sum(0)
valid = msk & (s > 12)
exg = np.where(valid, (2 * rgb[1] - rgb[0] - rgb[2]) / np.maximum(s, 1.0), np.nan)
ev = exg[valid]
thr = _otsu(ev) if ev.size else GREEN_FLOOR
thr = max(thr, GREEN_FLOOR)
veg = valid & (exg > thr)
# graded greenness 0..1: 0 at the median pixel (soil while the crop covers less
# than half; inside the crop later - the gaps then stay 0 either way), 1 at the
# greenest. A hard threshold keeps only the brightest seedlings and breaks plots.
# The zero is the LOCAL median (2 m blocks): soil colour drifts across a field
# (moisture, sun angle, mosaic seams) by more than seedlings add.
med = float(np.median(ev)) if ev.size else 0.0
hi = max(float(np.percentile(ev, 99.5)) if ev.size else 1.0, med + 0.08)
bg = _block_median(exg, max(4, int(round(2.0 / step))))
bg = np.where(np.isfinite(bg), bg, med)
score = np.clip((np.nan_to_num(exg, nan=med) - bg) / (hi - med), 0, 1).astype(np.float32)
score[~valid] = np.nan
lum = (s / 3.0).astype(np.float32)
lum[~valid] = np.nan
return dict(veg=veg, score=score, lum=lum, valid=valid, transform=tr, mx=mx, my=my, step=step,
crs=crs, exg_threshold=float(thr),
cover=float(veg.sum() / max(valid.sum(), 1)))
def _block_median(a, b):
"""median of a (nan-aware) in b x b blocks, bilinearly expanded to a's shape"""
H, W = a.shape
nh, nw = int(math.ceil(H / b)), int(math.ceil(W / b))
pad = np.full((nh * b, nw * b), np.nan, np.float32)
pad[:H, :W] = a
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
m = np.nanmedian(pad.reshape(nh, b, nw, b).transpose(0, 2, 1, 3).reshape(nh, nw, b * b),
axis=2)
if not np.isfinite(m).any():
return np.full(a.shape, np.nan, np.float32)
m = np.where(np.isfinite(m), m, np.nanmedian(m))
# bilinear expansion: block centres at (i + 0.5) * b
yi = np.clip((np.arange(H) + 0.5) / b - 0.5, 0, nh - 1)
xi = np.clip((np.arange(W) + 0.5) / b - 0.5, 0, nw - 1)
y0, x0 = np.floor(yi).astype(int), np.floor(xi).astype(int)
y1, x1 = np.minimum(y0 + 1, nh - 1), np.minimum(x0 + 1, nw - 1)
fy, fx = (yi - y0)[:, None], (xi - x0)[None, :]
top = m[y0][:, x0] * (1 - fx) + m[y0][:, x1] * fx
bot = m[y1][:, x0] * (1 - fx) + m[y1][:, x1] * fx
return (top * (1 - fy) + bot * fy).astype(np.float32)
def _pixel_xy_m(F, rows, cols):
"""metric coordinates of raster pixel centres"""
tr = F["transform"]
x = tr.c + (cols + 0.5) * tr.a + (rows + 0.5) * tr.b
y = tr.f + (cols + 0.5) * tr.d + (rows + 0.5) * tr.e
return x * F["mx"], y * F["my"]
# ---------------------------------------------------------------------------
# 1 direction of the rows
# ---------------------------------------------------------------------------
def _row_power(xy, wt, theta, seg, step, band):
"""summed band power of the across-row profiles of segments seg long"""
ca, sa = math.cos(theta), math.sin(theta)
u = xy[:, 0] * ca + xy[:, 1] * sa
v = -xy[:, 0] * sa + xy[:, 1] * ca
iu = ((u - u.min()) / seg).astype(np.int64)
iv = ((v - v.min()) / step).astype(np.int64)
nu, nv = int(iu.max()) + 1, int(iv.max()) + 1
H = np.bincount(iu * nv + iv, weights=wt, minlength=nu * nv).reshape(nu, nv).astype(np.float32)
H -= H.mean(axis=1, keepdims=True)
n = 1 << int(math.ceil(math.log2(max(nv, 16))))
P = np.abs(np.fft.rfft(H, n, axis=1)) ** 2
f = np.fft.rfftfreq(n, d=step)
sel = (f >= 1.0 / band[1]) & (f <= 1.0 / band[0])
return float(P[:, sel].sum())
def find_direction(F, around=None, log=print):
"""angle (radians, metric x-y frame) of the crop rows, and how clear it was."""
sc = F["score"]
rr, cc = np.nonzero(sc > 0.05)
if rr.size < 500:
raise DetectError("almost no green crop in the orthomosaic (%.2f %% of the pixels)"
% (100 * F["cover"]))
rng = np.random.default_rng(0)
if rr.size > 1_500_000:
k = rng.choice(rr.size, 1_500_000, replace=False)
rr, cc = rr[k], cc[k]
wt = sc[rr, cc].astype(np.float64)
x, y = _pixel_xy_m(F, rr, cc)
xy = np.c_[x - x.mean(), y - y.mean()]
step = max(F["step"], 0.01)
if around is None:
stages = [(np.radians(np.arange(0.0, 180.0, 1.0)), 1.5),
(None, 0.1, 1.5, 5.0), (None, 0.01, 0.15, 10.0)]
else:
stages = [(np.radians(np.arange(-4.0, 4.01, 0.25)) + around, 2.0),
(None, 0.05, 0.4, 5.0), (None, 0.01, 0.08, 10.0)]
best, clarity = None, 0.0
for st in stages:
if st[0] is not None:
angs, seg = st
else:
_, inc, half, seg = st
angs = best + np.radians(np.arange(-half, half + inc / 2, inc))
pw = np.array([_row_power(xy, wt, t, seg, step, ROW_BAND_M) for t in angs])
best = float(angs[int(np.argmax(pw))])
if st[0] is not None:
clarity = float(pw.max() / max(np.median(pw), 1e-9))
return best % math.pi, clarity
# ---------------------------------------------------------------------------
# 2 trial frame + layout
# ---------------------------------------------------------------------------
def _resample(F, O, ah, ch, a_rng, c_rng, step, key="score"):
"""F[key] (greenness, or 'lum' brightness) in the trial frame: float32
(na x nc), nan = no data"""
tr = F["transform"]
inv = ~tr
a0, a1 = a_rng
c0, c1 = c_rng
na, nc = int((a1 - a0) / step) + 1, int((c1 - c0) / step) + 1
out = np.full((na, nc), np.nan, np.float32)
C = c0 + (np.arange(nc) + 0.5) * step
H, W = F[key].shape
for i0 in range(0, na, 512):
A = a0 + (np.arange(i0, min(na, i0 + 512)) + 0.5) * step
AA, CC = np.meshgrid(A, C, indexing="ij")
X = (O[0] + AA * ah[0] + CC * ch[0]) / F["mx"]
Y = (O[1] + AA * ah[1] + CC * ch[1]) / F["my"]
col, row = inv * (X, Y)
col, row = np.floor(col).astype(np.int64), np.floor(row).astype(np.int64)
ok = (col >= 0) & (col < W) & (row >= 0) & (row < H)
blk = np.full(AA.shape, np.nan, np.float32)
blk[ok] = F[key][row[ok], col[ok]]
out[i0:i0 + blk.shape[0]] = blk
return out
def _share(G, axis):
"""mean greenness per line of the trial raster (nan where mostly no data)"""
valid = np.isfinite(G)
n = valid.sum(axis=axis).astype(float)
s = np.nansum(G, axis=axis, dtype=np.float64) if G.size else np.zeros(0)
return np.where(n > 0.2 * G.shape[axis], s / np.maximum(n, 1), np.nan)
def _runs(mask):
"""(start, end) index pairs of True runs, end exclusive"""
m = np.r_[False, np.asarray(mask, bool), False]
d = np.diff(m.astype(np.int8))
return list(zip(np.nonzero(d == 1)[0], np.nonzero(d == -1)[0]))
def _envelope(p, win):
"""local plateau: running 90th percentile over win bins (block-decimated)"""
n = len(p)
d = max(1, int(win) // 40)
nb = int(math.ceil(n / d))
q = np.full(nb * d, np.nan)
q[:n] = p
with np.errstate(all="ignore"):
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
b = np.nanmean(q.reshape(nb, d), axis=1)
k = max(3, int(win) // d) | 1
pad = np.r_[np.full(k // 2, np.nan), b, np.full(k // 2, np.nan)]
w = np.lib.stride_tricks.sliding_window_view(pad, k)
env = np.nanpercentile(w, 90, axis=1)
return np.repeat(env, d)[:n]
def _fill_nan(v):
v = np.asarray(v, float)
ok = np.isfinite(v)
if ok.all() or not ok.any():
return np.nan_to_num(v)
return np.interp(np.arange(len(v)), np.nonzero(ok)[0], v[ok])
def _normalise(p, step, env_m=4.0):
"""profile / local plateau (running 90th percentile): 0 = bare, ~1 = crop"""
env = _envelope(p, env_m / step)
ok = np.isfinite(p) & np.isfinite(env) & (env > 0)
q = np.full(len(p), np.nan)
q[ok] = np.clip(p[ok] / env[ok], 0, 1.5)
return q
def _line_phase(Sn, nph, loc_frac=0.08):
"""phase (in samples, unwrapped) of every profile: the best straight line
through all profiles (offset + slope per profile) on the score table Sn
(profiles x nph), then each profile within +-loc_frac of the line."""
npf = Sn.shape[0]
if npf < 3:
return np.array([float(np.argmax(Sn[j])) for j in range(npf)])
best = (-np.inf, 0, 0)
jj = np.arange(npf)
for sl in range(-(nph // 4), nph // 4 + 1):
sh = (np.arange(nph)[None, :] + sl * jj[:, None]) % nph
tot = Sn[jj[:, None], sh].sum(axis=0)
t = int(np.argmax(tot))
if tot[t] > best[0]:
best = (tot[t], t, sl)
_, t0, sl = best
line = (t0 + sl * np.arange(npf)).astype(float)
loc = max(1, int(loc_frac * nph))
out = np.empty(npf)
for j in range(npf):
c = int(round(line[j]))
cand = [(c + d) % nph for d in range(-loc, loc + 1)]
k = max(cand, key=lambda q: Sn[j, q])
d = ((k - c + nph // 2) % nph) - nph // 2
out[j] = line[j] + d
return out
def _boxcar_comb(qs, step, lo_m, hi_m):
"""_boxcar_fit at the autocorrelation pitch and at 1/2 and 1/3 of it (a profile
that varies range by range - cloud shadow, two treatments - can make twice the
pitch the stronger peak)."""
P, acs = _pitch_ac(qs, step, lo_m, hi_m)
if not np.isfinite(P):
return None
fits = []
for d in (1, 2, 3):
if P / d < lo_m:
break
r = _boxcar_fit(qs, step, P / d, acs)
if r is not None:
fits.append(r)
if not fits:
return None
# the score is a mean per box, so a comb on every other range scores as well as
# one on all of them: take the shortest pitch that scores near the best
top = max(r["score"] for r in fits)
return [r for r in fits if r["score"] >= 0.75 * top][-1]
def _boxcar_fit(qs, step, P, acs):
"""The ranges as a repeating box: plots of length L every P along each column
(one P and L for the trial, a phase per column on a straight line through the
columns, each plot then nudged on its own). The score is mean signal inside the
box minus outside it, so both crop ends count and whatever grows in the middle of
an alley (a drilled strip, weeds on the wheel track) does not matter.
Returns None or dict(pitch, size, ph, score, cells=[(k, j, start, end, mean,
contrast)])."""
n = len(qs[0])
Pp = P / step
Cs = [np.r_[0.0, np.cumsum(np.nan_to_num(q))] for q in qs]
Cn = [np.r_[0.0, np.cumsum(np.isfinite(q))] for q in qs]
def wmean(j, a, b):
a = np.clip(a, 0, n)
b = np.clip(b, 0, n)
cnt = Cn[j][b] - Cn[j][a]
with np.errstate(all="ignore"):
m = (Cs[j][b] - Cs[j][a]) / cnt
return np.where(cnt >= 0.6 * np.maximum(b - a, 1), m, np.nan)
nph = max(8, int(round(Pp)))
K = int(n / Pp) + 3
st = (np.arange(nph)[:, None] + np.arange(-1, K)[None, :] * Pp) # nph x K
import warnings
def table(Ls):
# inside the box minus two narrow strips just outside its ends
gs = max(2.0, min(0.3 / step, (Pp - Ls) / 2))
S = np.full((len(qs), nph), np.nan)
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
for j in range(len(qs)):
a = st.astype(int)
b = (st + Ls).astype(int)
mi = wmean(j, a, b)
mo = np.nanmean([wmean(j, (st - gs).astype(int), a),
wmean(j, b, (st + Ls + gs).astype(int))], axis=0)
S[j] = np.nanmean(mi - mo, axis=1)
return np.nan_to_num(S, nan=-1.0)
# The shortest box that scores within 10 % of the best: cutting into the crop
# costs a lot, swallowing clutter at the plot end (a drilled strip in the alley,
# a headland turn) costs little, so the longest box is not the plot.
res = []
for frac in np.arange(0.4, 0.975, 0.01):
S = table(frac * Pp)
res.append((frac, float(S.max(axis=1).sum()), S))
top = max(r[1] for r in res)
frac, tot, S = next(r for r in res if r[1] >= 0.9 * top)
noise = float(np.nanmedian([np.nanstd(np.diff(q)) for q in qs])) or 1e-9
score = tot / len(qs) / noise
Ls = frac * Pp
ph = _line_phase(S, nph) * step # start of plot k = 0, metres
L = Ls * step
g = max(2 * step, min(0.3, (P - L) / 2))
cells = []
for j, q in enumerate(qs):
for k in range(-1, K):
s0 = ph[j] + k * P
if s0 + L < 0 or s0 > n * step:
continue
bestc = None
for dlt in np.arange(-0.1 * P, 0.1 * P + step / 2, step):
a, b = s0 + dlt, s0 + dlt + L
ia, ib = int(a / step), int(b / step)
mi = wmean(j, np.array([ia]), np.array([ib]))[0]
with warnings.catch_warnings(): # both flanks outside the data
warnings.simplefilter("ignore", RuntimeWarning)
fl = np.nanmean([wmean(j, np.array([int((a - g) / step)]), np.array([ia]))[0],
wmean(j, np.array([ib]), np.array([int((b + g) / step)]))[0]])
if not (np.isfinite(mi) and np.isfinite(fl)):
continue
sc_ = mi - fl
if bestc is None or sc_ > bestc[0]:
bestc = (sc_, a, b, mi, fl)
if bestc is None:
continue
sc_, a, b, mi, fl = bestc
cells.append((k, j, a, b, mi, float(np.clip(sc_ / mi, 0, 1)) if mi > 0 else 0.0))
return dict(pitch=P, size=L, ph=ph, cells=cells, ac=acs, score=score)
def _comb(qs, step, lo_m, hi_m, log=None):
"""The gaps between plots as a comb: one pitch for all profiles, one phase per
profile (unwrapped, so neighbouring profiles may drift - staggered alleys), then
every gap refined on its own (+-0.1 pitch; +-0.2 for a single profile).
A gap is a NARROW feature repeating at the pitch. It is usually a dip (bare soil
between plots) but can be a peak: at emergence a weedy or better-watered strip
between plots can be greener than the seedlings (NUE Northam 2026-07-03). The
filter is narrow mean minus pitch-wide mean; the sign with the stronger comb wins.
qs: profiles (nan = no data), smoothed so the crop rows are gone.
Returns None (no periodicity) or dict(pitch, sign, ph, ks, gaps (K x n), sf
(feature strength per gap), strength (median), gw (dip width or nan), f)."""
P, acs = _pitch_ac(qs, step, lo_m, hi_m)
if not np.isfinite(P):
return None
g = min(max(0.12 * P, 3 * step), 0.6)
kg, kP = max(1, int(round(g / step))), max(3, int(round(P / step)))
n = len(qs[0])
x = (np.arange(n) + 0.5) * step
fs = []
for q in qs:
v = _fill_nan(q)
f = _smooth(v, kg) - _smooth(v, kP)
f[~np.isfinite(q)] = np.nan
fs.append(f)
nph = max(8, int(round(P / step)))
K = int(n / (P / step)) + 2
T = np.arange(nph)[:, None] + np.arange(K)[None, :] * (P / step)
T = T.astype(int)
inr = T < n
S = np.full((len(fs), nph), np.nan)
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
for j, f in enumerate(fs):
vals = np.where(inr, f[np.minimum(T, n - 1)], np.nan)
S[j] = np.nanmean(vals, axis=1)
pos = np.nansum(np.nanmax(S, axis=1))
neg = np.nansum(-np.nanmin(S, axis=1))
sign = 1.0 if pos > neg else -1.0
# Alleys run in straight lines across the trial, even when staggered: fit one
# line (offset + slope per profile) through all profiles at once, maximising the
# summed comb score, then let each profile move a little off it. Phases picked
# profile by profile lock half a pitch off where the crop is thin.
Sn = np.nan_to_num(sign * S, nan=0.0)
npf = len(fs)
if npf >= 3:
best = (-np.inf, 0, 0)
jj = np.arange(npf)
for sl in range(-(nph // 4), nph // 4 + 1):
sh = (np.arange(nph)[None, :] + sl * jj[:, None]) % nph
tot = Sn[jj[:, None], sh].sum(axis=0)
t = int(np.argmax(tot))
if tot[t] > best[0]:
best = (tot[t], t, sl)
_, t0, sl = best
line = (t0 + sl * np.arange(npf)).astype(float) # in phase samples
loc = max(1, int(0.08 * nph))
ph = np.empty(npf)
for j in range(npf):
c = int(round(line[j]))
cand = [(c + d) % nph for d in range(-loc, loc + 1)]
k = max(cand, key=lambda q: Sn[j, q])
d = ((k - c + nph // 2) % nph) - nph // 2
ph[j] = (line[j] + d) * (P / nph)
else:
ph = np.array([np.argmax(Sn[j]) * (P / nph) for j in range(npf)], float)
ph[~np.isfinite(S).any(axis=1)] = np.nan
ok = np.isfinite(ph)
if not ok.any():
return None
ang = np.interp(np.arange(npf), np.nonzero(ok)[0], ph[ok] / P * 2 * np.pi)
ph = np.unwrap(ang) / (2 * np.pi) * P
kmin = int(math.floor(-ph.max() / P)) - 1
kmax = int(math.ceil((n * step - ph.min()) / P)) + 1
ks = np.arange(kmin, kmax + 1)
gaps = np.full((len(ks), len(fs)), np.nan)
sf = np.full((len(ks), len(fs)), np.nan)
widths = []
qsm = [_smooth(_fill_nan(q), max(1, kg // 2)) for q in qs]
for j, f in enumerate(fs):
for ii, k in enumerate(ks):
t = ph[j] + k * P
win = 0.2 * P if npf < 3 else 0.1 * P
i0, i1 = max(int((t - win) / step), 0), min(int((t + win) / step), n)
if i1 - i0 < 3 or np.isfinite(f[i0:i1]).sum() < 0.6 * (i1 - i0):
continue
w = sign * f[i0:i1]
m = int(np.nanargmax(w))
d = 0.0
if 0 < m < len(w) - 1 and np.isfinite(w[m - 1]) and np.isfinite(w[m + 1]):
den = w[m - 1] - 2 * w[m] + w[m + 1]
d = 0.5 * (w[m - 1] - w[m + 1]) / den if den < 0 else 0.0
gaps[ii, j] = x[i0 + m] + float(np.clip(d, -0.5, 0.5)) * step
sf[ii, j] = w[m]
if sign < 0:
# a dip: its edges at half depth; the centre is midway between them,
# which is well defined however wide and flat the alley is
q = qsm[j]
c = i0 + m
lo_i, hi_i = max(0, c - kP // 2), min(n, c + kP // 2)
plat = float(np.percentile(q[lo_i:hi_i], 75))
half = (plat + q[c]) / 2
a_, b_ = c, c
while a_ > lo_i and q[a_] < half:
a_ -= 1
while b_ < hi_i - 1 and q[b_] < half:
b_ += 1
if plat > q[c] and a_ > lo_i and b_ < hi_i - 1:
widths.append((b_ - a_) * step)
gaps[ii, j] = (x[a_] + x[b_]) / 2
strength = float(np.nanmedian(sf)) if np.isfinite(sf).any() else 0.0
gw = float(np.clip(np.median(widths), 2 * step, 0.45 * P)) if widths else np.nan
return dict(pitch=P, sign=sign, ph=ph, ks=ks, gaps=gaps, sf=sf, strength=strength,
gw=gw, f=fs, ac=acs)
def _cells(cb, qs, step, size):
"""plots between neighbouring gaps of every profile: list of (k, j, centre,
mean of the profile inside, strength of the weaker gap)"""
P, gaps, sf, ks = cb["pitch"], cb["gaps"], cb["sf"], cb["ks"]
n = len(qs[0])
out = []
for j, q in enumerate(qs):
for ii in range(len(ks) - 1):
a_, b_ = gaps[ii, j], gaps[ii + 1, j]
sa_, sb_ = sf[ii, j], sf[ii + 1, j]
if np.isfinite(a_) and not np.isfinite(b_):
b_, sb_ = a_ + P, 0.0
elif np.isfinite(b_) and not np.isfinite(a_):
a_, sa_ = b_ - P, 0.0
elif not np.isfinite(a_):
continue
if not (0.6 * P < b_ - a_ < 1.4 * P):
continue
c = (a_ + b_) / 2
i0, i1 = max(0, int((c - size / 2) / step)), min(n, int((c + size / 2) / step))
seg = q[i0:i1]
if i1 - i0 < 3 or np.isfinite(seg).sum() < 0.7 * size / step:
continue
out.append((int(ks[ii]), j, c, float(np.nanmean(seg)),
float(min(np.nan_to_num(sa_), np.nan_to_num(sb_)))))
return out
def _trim(present, n_want, label, log):
"""contiguous index block holding the plots; with a count, drop the ends with the
fewest plots first (outer columns are often buffers)"""
ks = sorted(present)
k0, k1 = ks[0], ks[-1]
if n_want and (k1 - k0 + 1) > n_want:
while (k1 - k0 + 1) > n_want:
if present.get(k0, 0) <= present.get(k1, 0):
k0 += 1
else:
k1 -= 1
log(" %s: %d found, kept %d as asked (the outer ones are taken as buffers - "
"check them)" % (label, ks[-1] - ks[0] + 1, n_want))
elif n_want and (k1 - k0 + 1) < n_want:
log(" [!] %s: only %d found, %d asked for" % (label, k1 - k0 + 1, n_want))
return k0, k1
def _col_profiles(G, runs_c, step, row_pitch=None):
"""along-row signal of every column, each divided by its local plateau
(0 = bare, ~1 = crop). With visible rows the signal is the ROW AMPLITUDE: the
greenness across the column correlated with the rows at the trial's spacing and
phase, summed over 20 cm along. Only rows parallel to the plot's add up, so
weeds, stubble and headland turns drilled at another angle (often in the alleys)
drop out. Without rows it is the mean greenness (smoothed 10 cm)."""
out = []
for cs, ce in runs_c:
w = 0.1 * (ce - cs)
ic0, ic1 = max(0, int((cs + w) / step)), int(math.ceil((ce - w) / step))
ic1 = max(ic1, ic0 + 1)
B = G[:, ic0:ic1].astype(np.float64)
valid = np.isfinite(B).sum(axis=1) > 0.2 * B.shape[1]
if row_pitch:
cpos = (np.arange(ic0, ic1) + 0.5) * step
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
B = np.nan_to_num(B - np.nanmean(B, axis=1, keepdims=True))
Z = B @ np.exp(-2j * np.pi * cpos / row_pitch) / max(B.shape[1], 1)
k = max(1, int(round(0.2 / step)))
p = np.abs(np.convolve(Z, np.ones(k) / k, "same"))
else:
prof = _share(G[:, ic0:ic1], 1)
p = _smooth(prof, 0.10 / step)
p[~valid] = np.nan
env = _envelope(p, 4.0 / step)
ok = np.isfinite(p) & np.isfinite(env) & (env > 0)
q = np.full(len(p), np.nan)
q[ok] = np.clip(p[ok] / env[ok], 0, 1.5)
out.append(q)
return out
def _dark_profiles(GL, strips, step):
"""soil darkness along every column: (local bright level - brightness) / it,
smoothed 10 cm and normalised like the other profiles (bare alleys ~0)"""
out = []
for cs, ce in strips:
ic0, ic1 = max(0, int(cs / step)), int(math.ceil(ce / step))
b = _share(GL[:, ic0:max(ic1, ic0 + 1)], 1)
b = _smooth(b, 0.10 / step)
hi = _envelope(b, 6.0 / step)
d = np.where(np.isfinite(b) & np.isfinite(hi) & (hi > 0), (hi - b) / hi, np.nan)
out.append(_normalise(d, step))
return out
def _pitch_ac(qs, step, lo_m=1.5, hi_m=40.0):
"""pitch: peak of the summed autocorrelation of the profiles in [lo_m, hi_m],
preferring the shortest strong harmonic. Returns (pitch, mean peak height)."""
n = max(len(q) for q in qs)
nf = 1 << int(math.ceil(math.log2(2 * n)))
ac = np.zeros(nf // 2)
for q in qs:
x = np.nan_to_num(q - np.nanmean(q)) if np.isfinite(q).any() else np.zeros(len(q))
f = np.fft.rfft(x, nf)
a = np.fft.irfft(f * np.conj(f), nf)[: nf // 2]
if a[0] > 0:
ac += a / a[0]
# unbiased: fewer overlapping samples at long lags
lag = np.arange(len(ac))
ac = ac / np.maximum(1 - lag / n, 0.2)
i0, i1 = int(lo_m / step), min(len(ac) - 1, int(min(hi_m, 0.6 * n * step) / step))
if i1 <= i0 + 2:
return np.nan, 0.0
seg = _smooth(ac[i0:i1], max(1, 0.1 / step))
pk = [i for i in range(1, len(seg) - 1) if seg[i] >= seg[i - 1] and seg[i] > seg[i + 1]]
if not pk:
return np.nan, 0.0
best = max(pk, key=lambda i: seg[i])
top = seg[best]
for i in sorted(pk): # shortest peak nearly as strong
if seg[i] >= 0.75 * top:
best = i
break
return (i0 + best) * step, float(seg[best] / max(len(qs), 1))
def _row_pitch(profile, step):
from row_detect import row_pitch_from_profile
p = np.nan_to_num(profile, nan=float(np.nanmean(profile)) if np.isfinite(profile).any() else 0)
det = p - _smooth(p, 0.5 / step)
pitch, share = row_pitch_from_profile(det, step, *ROW_BAND_M)
return pitch, share
# ---------------------------------------------------------------------------
# 3 each plot
# ---------------------------------------------------------------------------
def _edge(p, x, target, search, level, outward):
"""half-height crossing of profile p (positions x) nearest to target within
+-search. outward = -1 for a low edge (crop on the right), +1 for a high edge.
Returns position or None."""
m = np.isfinite(p)
best = None
for i in range(len(p) - 1):
if not (m[i] and m[i + 1]):
continue
a, b = p[i] - level, p[i + 1] - level
if outward < 0 and not (a < 0 <= b):
continue
if outward > 0 and not (a >= 0 > b):
continue
t = a / (a - b) if a != b else 0.5
pos = x[i] + t * (x[i + 1] - x[i])
if abs(pos - target) <= search and (best is None or abs(pos - target) < abs(best - target)):
best = pos
return best
def _measure(G, a_lo, a_hi, c_lo, c_hi, step, axis, smooth_m, plateau_band, target,
search, gap):
"""sides (axis=1, across) or ends (axis=0, along) of one plot.
G window rows = a, cols = c. Returns (lo, hi, contrast_lo, contrast_hi, cover)."""
na, nc = G.shape
ia0, ia1 = max(0, int(a_lo / step)), min(na, int(math.ceil(a_hi / step)))
ic0, ic1 = max(0, int(c_lo / step)), min(nc, int(math.ceil(c_hi / step)))
if ia1 - ia0 < 4 or ic1 - ic0 < 4:
return None, None, 0.0, 0.0, np.nan
sub = G[ia0:ia1, ic0:ic1]
prof = _share(sub, 0 if axis == 1 else 1)
x0 = (ic0 if axis == 1 else ia0) * step
x = x0 + (np.arange(len(prof)) + 0.5) * step
ps = _smooth(prof, smooth_m / step)
ps[~np.isfinite(prof)] = np.nan
lo_t, hi_t = target
inside = (x > lo_t + plateau_band * (hi_t - lo_t)) & (x < hi_t - plateau_band * (hi_t - lo_t))
if not np.isfinite(ps[inside]).any():
return None, None, 0.0, 0.0, np.nan
plateau = float(np.nanmedian(ps[inside]))
cover = float(np.nanmean(prof[(x > lo_t) & (x < hi_t)]))
if plateau <= 0.02:
return None, None, 0.0, 0.0, cover
level = 0.5 * plateau
lo = _edge(ps, x, lo_t, search, level, -1)
hi = _edge(ps, x, hi_t, search, level, +1)
def contrast(e, side):
if e is None:
return 0.0
w = (x > e - gap) & (x < e) if side < 0 else (x > e) & (x < e + gap)
if not np.isfinite(ps[w]).any():
return 0.0
return float(np.clip((plateau - np.nanmin(ps[w])) / plateau, 0, 1))
return lo, hi, contrast(lo, -1), contrast(hi, +1), cover
def _ends_1d(q, step, target, search, gap):
"""plot ends on a column profile (already smoothed and normalised): half-height
crossings nearest the expected ends. Returns (lo, hi, contrast_lo, contrast_hi)."""
n = len(q)
x = (np.arange(n) + 0.5) * step
lo_t, hi_t = target
inside = (x > lo_t + 0.15 * (hi_t - lo_t)) & (x < hi_t - 0.15 * (hi_t - lo_t))
if not np.isfinite(q[inside]).any():
return None, None, 0.0, 0.0
plateau = float(np.nanmedian(q[inside]))
if plateau <= 0.05:
return None, None, 0.0, 0.0
level = 0.5 * plateau
i0 = max(0, int((lo_t - search - gap) / step))
i1 = min(n, int((hi_t + search + gap) / step) + 1)
lo = _edge(q[i0:i1], x[i0:i1], lo_t, search, level, -1)
hi = _edge(q[i0:i1], x[i0:i1], hi_t, search, level, +1)
def contrast(e, side):
if e is None:
return 0.0
w = (x > e - gap) & (x < e) if side < 0 else (x > e) & (x < e + gap)
if not np.isfinite(q[w]).any():
return 0.0
return float(np.clip((plateau - np.nanmin(q[w])) / plateau, 0, 1))
return lo, hi, contrast(lo, -1), contrast(hi, +1)
def _crop_width(pc, step, col_c, Pc):
"""median half-height width of the crop of every column on the trial-wide
across profile (rows smoothed away); nan when unclear"""
x = (np.arange(len(pc)) + 0.5) * step
ws = []
for c in col_c:
m = (x > c - 0.3 * Pc) & (x < c + 0.3 * Pc)
if not np.isfinite(pc[m]).any():
continue
lvl = 0.5 * float(np.nanmedian(pc[m]))
lo = _edge(pc, x, c - 0.4 * Pc, 0.3 * Pc, lvl, -1)
hi = _edge(pc, x, c + 0.4 * Pc, 0.3 * Pc, lvl, +1)
if lo is not None and hi is not None and hi > lo:
ws.append(hi - lo)
return float(np.median(ws)) if len(ws) >= max(2, len(col_c) // 3) else np.nan
def _rows_between_gaps(sc, step, col_c, Pc, row_pitch):
"""crop rows per plot: the row peaks of the trial-wide across profile (every
range adds up, so even seedlings show) inside each column, kept half a row
spacing clear of the gap centres (a green strip between plots is not a row).
Returns (rows, columns that agree) or (0, 0)."""
p = _smooth(_fill_nan(sc), max(1, int(round(0.04 / step))))
p[~np.isfinite(sc)] = np.nan
x = (np.arange(len(p)) + 0.5) * step
sep = 0.6 * row_pitch / step
ns = []
for c in col_c:
m = np.nonzero((x > c - Pc / 2 + 0.5 * row_pitch) & (x < c + Pc / 2 - 0.5 * row_pitch))[0]
if m.size < 5 or not np.isfinite(p[m]).all():
continue
v = p[m]
lo, hi = float(np.percentile(v, 10)), float(v.max())
if not hi > lo:
continue
pk = [i for i in range(1, len(v) - 1) if v[i] >= v[i - 1] and v[i] > v[i + 1]
and v[i] > lo + 0.35 * (hi - lo)]
pk.sort(key=lambda i: -v[i])
kept = []
for i in pk:
if all(abs(i - k) >= sep for k in kept):
kept.append(i)
if kept:
ns.append(len(kept))
if len(ns) < max(2, len(col_c) // 3):
return 0, 0
vals, cnt = np.unique(ns, return_counts=True)
return int(vals[np.argmax(cnt)]), int(cnt.max())
def _count_rows(ortho_path, F, O, ah, ch, a0, c0, cc, ac, col_c, EA, Pc, row_pitch,
L, log, n_sample=9):
"""crop rows per plot from row_detect on a few plots spread over the trial; each
window is one plot pitch less one row wide, centred on the plot, so it holds the
plot's rows and none of its neighbours'. Returns (rows, spacing) or (0, nan)."""
import crop_rows
nr, ncol = cc.shape
cand = [(i, j) for i in range(nr) for j in range(ncol)
if np.isfinite(cc[i, j]) and np.isfinite(ac[i, j])]
if not cand:
return 0, np.nan
pick = [cand[int(k)] for k in np.linspace(0, len(cand) - 1, min(n_sample, len(cand)))]
half_w = max(Pc - row_pitch, 2 * row_pitch) / 2
rings, ids = [], []
for i, j in pick:
a_, c_ = ac[i, j], cc[i, j]
ring = []
for d_a, d_c in ((-0.3 * L, -half_w), (-0.3 * L, half_w), (0.3 * L, half_w),
(0.3 * L, -half_w)):
Pt = O + (a0 + a_ + d_a) * ah + (c0 + c_ + d_c) * ch
ring.append((float(Pt[0] / F["mx"]), float(Pt[1] / F["my"])))
rings.append(ring)
ids.append("%d_%d" % (i, j))
try:
crs = F["crs"].to_wkt() if F["crs"] is not None else None
_, rep = crop_rows.detect_rows_in_grid(rings, ids, ortho_path, grid_crs=crs,
pitch_m=row_pitch, pad_frac=0.0,
log=lambda *a: None)
except Exception as e:
log(" [!] counting the rows failed: %s" % e)
return 0, np.nan
ns = [r["n_rows"] for r in rep if r.get("ok") and r.get("n_rows")]
ps = [float(r["pitch_m"]) for r in rep if r.get("ok") and r.get("pitch_m")]
if len(ns) < max(2, len(pick) // 2):
return 0, np.nan
vals, cnt = np.unique(ns, return_counts=True)
n = int(vals[np.argmax(cnt)])
p = float(np.median(ps)) if ps else row_pitch
log(" %d crop rows per plot every %.1f cm (%d of %d sample plots agree)"
% (n, 100 * p, int(cnt.max()), len(ns)))
return n, p
def _robust_line(t, y, w):
"""y ~ k t + d, iteratively reweighted (Huber-like); returns predict(t)"""
t, y, w = map(lambda q: np.asarray(q, float), (t, y, w))
ok = np.isfinite(y) & (w > 0)
if ok.sum() == 0:
return lambda q: np.full(np.shape(q), np.nan)
if ok.sum() < 3:
m = float(np.average(y[ok], weights=w[ok]))
return lambda q: np.full(np.shape(q), m)
ww = w.copy()
k, d = 0.0, float(np.median(y[ok]))
for _ in range(6):
A = np.c_[t[ok], np.ones(ok.sum())] * np.sqrt(ww[ok])[:, None]
sol = np.linalg.lstsq(A, y[ok] * np.sqrt(ww[ok]), rcond=None)[0]
k, d = float(sol[0]), float(sol[1])
r = np.abs(y - (k * t + d))
s = max(1.4826 * float(np.median(r[ok])), 0.01)
ww = w * np.where(r < 2 * s, 1.0, 2 * s / np.maximum(r, 1e-9))
return lambda q: k * np.asarray(q, float) + d
# ---------------------------------------------------------------------------
# main entry
# ---------------------------------------------------------------------------
def detect_plots(ortho_path, quad=None, n_ranges=0, n_cols=0, plot_w=0.0, plot_l=0.0,
log=print, progress=None, cancel=None):
"""Find the plots of one trial.
ortho_path RGB orthomosaic (any CRS; lon/lat is handled in local metres)
quad optional 4 trial corners BL, BR, TR, TL in the ortho's CRS: limits the
search to the trial and fixes the numbering (range 1 at BL-BR, plot 1
of each range at BL-TL, as in 1a). Without it the whole orthomosaic is
used, ranges are numbered from the south (or west) end and plots from
the left looking along the rows.
n_ranges, n_cols optional counts (0 = find them). With more plots found than
asked for, the middle block is kept (outer ones = buffers).
plot_w, plot_l optional plot size in metres (0 = median of the measured plots)
Returns dict(plots=[{ring, props, conf, flags, ...}], quad=[BL, BR, TR, TL] world,
info={...}) - rings in the orthomosaic's CRS, in range-major order.
"""
t0 = time.time()
def prog(f, msg):
if progress:
progress(f, msg)
if cancel is not None and cancel():
raise DetectError("cancelled")
prog(0.02, "reading the orthomosaic")
F = read_field(ortho_path, quad)
log("Auto-detect: orthomosaic read at %.1f cm, %.1f %% green (ExG > %.3f)"
% (100 * F["step"], 100 * F["cover"], F["exg_threshold"]))
if F["cover"] < 0.02 and F["step"] > 0.012:
F = read_field(ortho_path, quad, step_m=max(0.01, F["step"] / 2))
log(" little green - re-read at %.1f cm, %.1f %% green" % (100 * F["step"], 100 * F["cover"]))
mx, my = F["mx"], F["my"]
# ---- 1 direction --------------------------------------------------------
prog(0.1, "finding the row direction")
around = None
Q = None
if quad:
Q = [np.array([p[0] * mx, p[1] * my]) for p in quad] # BL BR TR TL
up = (Q[3] - Q[0]) + (Q[2] - Q[1])
around = math.atan2(up[1], up[0])
theta, clarity = find_direction(F, around, log)
ah = np.array([math.cos(theta), math.sin(theta)]) # along the rows
if Q is not None:
if float(ah @ up) < 0:
ah = -ah
elif ah[1] < -1e-6 or (abs(ah[1]) <= 1e-6 and ah[0] < 0):
ah = -ah # point north / east
ch = np.array([ah[1], -ah[0]]) # across: a turned -90
if Q is not None and float(ch @ ((Q[1] - Q[0]) + (Q[2] - Q[3]))) < 0:
ch = -ch
log(" rows run at %.2f deg from east (row signal %.1fx background)"
% (math.degrees(math.atan2(ah[1], ah[0])), clarity))
# ---- 2 trial frame ------------------------------------------------------
if Q is not None:
pts = np.array(Q)
# clicks a little inside the outer plots still keep them; a buffer column
# beyond the corners does not come in (it would be a sliver, then dropped)
pad = min(0.6, 0.02 * max(np.ptp(pts @ ah), np.ptp(pts @ ch)))
else:
H, W = F["veg"].shape
rr = np.array([0, 0, H, H]) - 0.5
cc = np.array([0, W, 0, W]) - 0.5
x, y = _pixel_xy_m(F, rr, cc)
pts = np.c_[x, y]
pad = 0.0
O = np.zeros(2)
a_rng = (float((pts @ ah).min() - pad), float((pts @ ah).max() + pad))
c_rng = (float((pts @ ch).min() - pad), float((pts @ ch).max() + pad))
step = F["step"]
prog(0.2, "resampling into the trial frame")
G = _resample(F, O, ah, ch, a_rng, c_rng, step)
GL = _resample(F, O, ah, ch, a_rng, c_rng, step, "lum")
if Q is not None:
# outside the clicked quad = no data (other trials, headlands)
qa = np.array([q @ ah for q in Q]) - a_rng[0]
qc = np.array([q @ ch for q in Q]) - c_rng[0]
A = (np.arange(G.shape[0]) + 0.5) * step
C = (np.arange(G.shape[1]) + 0.5) * step
AA, CC = np.meshgrid(A, C, indexing="ij")
# point-in-convex-quad test (sign consistent with the quad's turn)
s = None
inside = np.ones(G.shape, bool)
for k in range(4):
p0 = (qa[k], qc[k])
p1 = (qa[(k + 1) % 4], qc[(k + 1) % 4])
cr = (p1[0] - p0[0]) * (CC - p0[1]) - (p1[1] - p0[1]) * (AA - p0[0])
if s is None:
ctr = (qa.mean(), qc.mean())
s = np.sign((p1[0] - p0[0]) * (ctr[1] - p0[1]) - (p1[1] - p0[1]) * (ctr[0] - p0[0]))
# grow the quad by the pad so clicks just inside the crop still keep it
inside &= (cr * s) >= -pad * math.hypot(p1[0] - p0[0], p1[1] - p0[1])
G[~inside] = np.nan
GL[~inside] = np.nan
del AA, CC
# ---- 2a across the rows: the columns ---------------------------------------
# a column of plots is one seeder pass, straight through all the ranges, so one
# profile over the whole trial (rows smoothed away) holds every gap between plots
prog(0.3, "finding the plots across the rows")
sc = _share(G, 0)
row_pitch, row_share = _row_pitch(sc, step)
rows_seen = bool(np.isfinite(row_pitch))
if not rows_seen:
row_pitch = 0.25