-
Notifications
You must be signed in to change notification settings - Fork 9
/
stereo.py
1307 lines (1244 loc) · 58 KB
/
stereo.py
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
import time
import threading
import datetime
import numpy as np
import vtk
import cv2 as cv
CODE_TOUCHE_ECHAP = 27
CODE_TOUCHE_FIN = CODE_TOUCHE_ECHAP
MODE_AFFICHAGE = np.int(0x100)
MODE_REGLAGECAMERA = np.int(0x1000)
NBCAMERA = 2
CAMAPI = cv.CAP_DSHOW
LARGEUR_ECRAN = 1200
HAUTEUR_ECRAN = 900
MODE_CALIBRE = np.int(0x200)
MODE_MAstereo = np.int(0x400)
MODE_EPIPOLAIRE = np.int(0x800)
MODE_3D = np.int(0x2000)
class ParamAffichage:
def __init__(self):
self.index_camera = 0
self.mode_affichage = MODE_AFFICHAGE
self.frame = []
self.zoom = 1
self.code_touche_clavier = 0
self.algo_stereo = 0
self.type_distorsion = 0
self.taille_globale = [0, 0]
def init_fenetre(self, liste_webcam_ouvertes, liste_cam_calib):
taille_webcam = []
for idx, vid in enumerate(liste_webcam_ouvertes):
ret = fixer_resolution(vid[0], (liste_cam_calib[idx].taille_img[0],
liste_cam_calib[idx].taille_img[1]))
if not ret:
print("La resolution de la camera ", idx, "ne peut être fixée\nFin du programme")
exit()
taille_webcam.append((int(liste_cam_calib[idx].taille_img[0]),
int(liste_cam_calib[idx].taille_img[1])))
self.taille_globale[0] += taille_webcam[idx][0]
self.taille_globale[1] = int(max(liste_cam_calib[idx].taille_img[1],
self.taille_globale[1]))
if self.taille_globale[0] > LARGEUR_ECRAN or self.taille_globale[1] > HAUTEUR_ECRAN:
self.zoom = min(LARGEUR_ECRAN/self.taille_globale[0],
HAUTEUR_ECRAN/self.taille_globale[1])
self.frame = np.zeros((self.taille_globale[1], self.taille_globale[0], 3),
np.uint8)
return taille_webcam
class CalibrageCamera:
def __init__(self):
self.idx_usb = 0
self.index = 0
self.rvecs = None
self.tvecs = None
self.fic_donnees = None
self.mat_intrin = None
self.mat_dist = None
self.pts_grille = []
self.pts_camera = []
self.pts_objets = []
self.taille_img = (640, 480)
self.rms = 0
self.type_calib = [
cv.CALIB_FIX_PRINCIPAL_POINT+cv.CALIB_FIX_K1+\
cv.CALIB_FIX_K2 + cv.CALIB_FIX_K3 + cv.CALIB_FIX_K4+
cv.CALIB_FIX_K5 + cv.CALIB_FIX_K6,
cv.CALIB_USE_INTRINSIC_GUESS
]
def calibrer(self, taille):
if self.fic_donnees is None:
sauver_donnees_calibration(self, None, None)
if self.mat_intrin is None:
self.type_calib[0] = 0
else:
self.type_calib[0] = cv.CALIB_USE_INTRINSIC_GUESS
for param_otim in self.type_calib:
self.rms, self.mat_intrin, self.mat_dist, \
self.rvecs, self.tvecs = \
cv.calibrateCamera(self.pts_objets,
self.pts_camera,
taille,
self.mat_intrin,
self.mat_dist,
self.rvecs,
self.tvecs,
param_otim,
(cv.TermCriteria_COUNT + cv.TermCriteria_EPS, 5000000, 1e-8))
self.type_calib[0] = self.type_calib[0] | cv.CALIB_USE_EXTRINSIC_GUESS
def recherche_grille(self, img, mire):
grille, echiquier = cv.findChessboardCorners(
img, (mire.nb_col, mire.nb_lig),
cv.CALIB_CB_ADAPTIVE_THRESH + cv.CALIB_CB_FAST_CHECK
)
if grille:
img_gris = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
echiquier = cv.cornerSubPix(img_gris, echiquier, (5, 5), (-1, -1),
(cv.TERM_CRITERIA_EPS + cv.TermCriteria_COUNT, 100, 0.01)
)
else:
return False, []
return True, echiquier
def analyse_grille(self, img, frame, mire):
grille, echiquier = self.recherche_grille(img, mire)
if grille:
cv.drawChessboardCorners(frame, (mire.nb_col, mire.nb_lig), echiquier, False)
self.pts_objets.append(self.pts_grille)
self.pts_camera.append(echiquier)
else:
return False, frame
return True, frame
def analyse_charuco(self, img, frame, mire):
pts_aruco, ids, refus = cv.aruco.detectMarkers(image=img, dictionary=mire.dictionary,
parameters=mire.detectorParams)
pts_aruco, ids, refus, recover = cv.aruco.refineDetectedMarkers(
img, mire.board,
pts_aruco, ids,
rejectedCorners=refus)
if recover is not None:
pts_aruco = permutation_taille(pts_aruco)
if ids is not None:
retval, coin_charuco, ids_charuco = cv.aruco.interpolateCornersCharuco(
pts_aruco, ids, img, mire.board)
if retval == 0:
return False, frame, mire
cv.aruco.drawDetectedCornersCharuco(
frame, coin_charuco, ids_charuco)
cv.aruco.drawDetectedMarkers(frame, pts_aruco, ids)
pts_reel = None
echiquier = None
if False:
for ir in range(len(pts_aruco)):
if echiquier is None:
echiquier = pts_aruco[ir][0]
pts_reel = mire.board.objPoints[ids[ir, 0]]
else:
echiquier = np.vstack((echiquier, pts_aruco[ir][0]))
pts_reel = np.vstack((pts_reel, mire.board.objPoints[ids[ir, 0]]))
if True:
for ir in range(ids_charuco.shape[0]):
index = ids_charuco[ir][0]
if echiquier is None:
echiquier = coin_charuco[ir][0]
pts_reel = mire.board.chessboardCorners[index]
else:
echiquier = np.vstack((echiquier, coin_charuco[ir][0]))
pts_reel = np.vstack((pts_reel, mire.board.chessboardCorners[index]))
pts_reel = pts_reel.reshape((pts_reel.shape[0], 1, 3))
self.pts_objets.append(pts_reel)
self.pts_camera.append(echiquier)
else:
return False, frame
return True, frame
class ParamMire:
def __init__(self):
self.nb_lig = 6
self.nb_col = 9
self.nb_lig_aruco = 5
self.nb_col_aruco = 8
self.dim_carre = 0.0275
self.dim_aruco = 0.03455
self.sep_aruco = 0.02164
self.dict = cv.aruco.DICT_7X7_250
self.dictionary = cv.aruco.Dictionary_get(self.dict)
self.board = cv.aruco.CharucoBoard_create(self.nb_col_aruco, self.nb_lig_aruco,
self.dim_aruco, self.sep_aruco,
self.dictionary)
self.detectorParams = cv.aruco.DetectorParameters_create()
self.detectorParams.cornerRefinementMethod = cv.aruco.CORNER_REFINE_SUBPIX
class ParamAlgoStereo:
def __init__(self):
self.al_bm = None
self.al_sgbm = None
self.pre_filtre = cv.StereoBM_PREFILTER_XSOBEL
self.taille_bloc = 3
self.nb_disparite = 8
self.etendu_speck = 1
self.taille_speck = 10
self.unicite = 3
self.pre_filtre_cap = 31
def maj_stereo_bloc(self, x):
self.taille_bloc = x
self.al_bm.setBlockSize(2*self.taille_bloc+1)
self.al_sgbm.setBlockSize(2 * self.taille_bloc + 1)
def maj_stereo_disp(self, x):
self.nb_disparite = x
self.al_bm.setNumDisparities(16*self.nb_disparite)
self.al_sgbm.setNumDisparities(16*self.nb_disparite)
def maj_stereo_unicite(self, x):
self.unicite = x
self.al_bm.setUniquenessRatio(self.unicite)
self.al_sgbm.setUniquenessRatio(self.unicite)
def maj_stereo_taille_speck(self, x):
self.taille_speck = x
self.al_bm.setSpeckleWindowSize(self.taille_speck)
self.al_sgbm.setSpeckleWindowSize(self.taille_speck)
def maj_stereo_etendu_speck(self, x):
self.etendu_speck = x
self.al_bm.setSpeckleRange(self.etendu_speck)
self.al_sgbm.setSpeckleRange(self.etendu_speck)
class CalibrageCamera3D:
def __init__(self):
self.type_calib = [cv.CALIB_FIX_INTRINSIC + cv.CALIB_ZERO_DISPARITY,
cv.CALIB_ZERO_DISPARITY+cv.CALIB_USE_INTRINSIC_GUESS]
self.R = None
self.T = None
self.E = None
self.F = None
self.R1 = None
self.R2 = None
self.P1 = None
self.P2 = None
self.Q = None
self.m = None
self.d = None
self.epipolar = None
self.valid1 = ()
self.valid2 = ()
self.rms = 0
self.fic_donnees = None
self.pts_grille = []
self.pts_camera_gauche = []
self.pts_camera_droite = []
self.pts_objets = []
self.map11 = None
self.map12 = None
self.map21 = None
self.map22 = None
def analyse_grille(self, liste_img, frame, mire):
grille, echiquier_gauche = CalibrageCamera().recherche_grille(liste_img[0],mire)
if grille:
grille, echiquier_droite = CalibrageCamera().recherche_grille(liste_img[1],mire)
if grille:
self.pts_objets.append(np.asarray(self.pts_grille, dtype=np.float32))
self.pts_camera_gauche.append(echiquier_gauche)
self.pts_camera_droite.append(echiquier_droite)
else:
return False, frame
else:
return False, frame
largeur, hauteur = liste_img[0].shape[1], liste_img[0].shape[0]
cv.drawChessboardCorners(frame[0:hauteur, 0:largeur],
(mire.nb_col, mire.nb_lig), echiquier_gauche, False)
cv.drawChessboardCorners(frame[0:hauteur, largeur:2 * largeur],
(mire.nb_col, mire.nb_lig), echiquier_droite, False)
return True, frame
echiquier = []
grille, echiquier = cv.findChessboardCorners(
img, (mire.nb_col, mire.nb_lig),
cv.CALIB_CB_ADAPTIVE_THRESH + cv.CALIB_CB_FAST_CHECK + cv.CALIB_CB_NORMALIZE_IMAGE
)
if grille:
img_gris = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
echiquier = cv.cornerSubPix(
img_gris, echiquier, (5, 5), (-1, -1),
(cv.TERM_CRITERIA_EPS + cv.TermCriteria_COUNT, 100, 0.01))
cv.drawChessboardCorners(frame, (mire.nb_col, mire.nb_lig), echiquier, False)
if index == 0:
if self.pts_camera_gauche is None:
self.pts_objets = [np.asarray(self.pts_grille, dtype=np.float32)]
self.pts_camera_gauche = [echiquier]
else:
self.pts_objets.append(np.asarray(self.pts_grille, dtype=np.float32))
self.pts_camera_gauche.append(echiquier)
else:
if self.pts_camera_droite is None:
self.pts_camera_droite = [echiquier]
else:
self.pts_camera_droite.append(echiquier)
else:
return False, frame
return True, frame
def analyse_charuco(self, liste_images, mire, param_ecran):
pts_arucog, idsg, refusg = cv.aruco.detectMarkers(
image=liste_images[0], dictionary=mire.dictionary,
parameters=mire.detectorParams)
pts_arucog, idsg, refusg, recoverg = cv.aruco.refineDetectedMarkers(
liste_images[0], mire.board,
pts_arucog, idsg, rejectedCorners=refusg)
if recoverg is not None:
pts_arucog = permutation_taille(pts_arucog)
pts_arucod, idsd, refusd = cv.aruco.detectMarkers(
liste_images[1], dictionary=mire.dictionary,
parameters=mire.detectorParams)
pts_arucod, idsd, refusd, recoverd = cv.aruco.refineDetectedMarkers(
liste_images[1], mire.board,
pts_arucod, idsd, rejectedCorners=refusd)
if recoverd is not None:
pts_arucod = permutation_taille(pts_arucod)
if len(idsg) > 0 and len(idsd) > 0:
frame = param_ecran.frame
cv.aruco.drawDetectedMarkers(param_ecran.frame, pts_arucog, idsg)
r_dst = [liste_images[1].shape[1], 0,
liste_images[1].shape[1], liste_images[1].shape[0]]
y = np.zeros((liste_images[1].shape[0], liste_images[1].shape[1], 3),
np.uint8)
cv.aruco.drawDetectedMarkers(y, pts_arucod, idsd)
frame[r_dst[1]:r_dst[1]+r_dst[3], r_dst[0]:r_dst[0]+r_dst[2]] = y + frame[
r_dst[1]:r_dst[1]+r_dst[3], r_dst[0]:r_dst[0]+r_dst[2]]
param_ecran.frame = frame
pts_reel = None
for ir in range(len(pts_arucog)):
ind = np.where(idsd == idsg[ir])
if ind[0].shape[0] > 0:
id = ind[0][0]
if pts_reel is None:
echiquierg = pts_arucog[ir][0]
echiquierd = pts_arucod[id][0]
pts_reel = mire.board.objPoints[idsg[ir, 0]]
else:
echiquierg = np.vstack((echiquierg, pts_arucog[ir][0]))
echiquierd = np.vstack((echiquierd, pts_arucod[id][0]))
pts_reel = np.vstack((pts_reel, mire.board.objPoints[idsg[ir, 0]]))
pts_reel = pts_reel.reshape((pts_reel.shape[0], 1, 3))
self.pts_objets.append(pts_reel)
self.pts_camera_gauche.append(echiquierg)
self.pts_camera_droite.append(echiquierd)
def calibrer(self, liste_cam_calib):
sauver_donnees_calibration(None, None, self)
self.m = [[], []]
self.d = [[], []]
self.m[0] = liste_cam_calib[0].mat_intrin.copy()
self.m[1] = liste_cam_calib[1].mat_intrin.copy()
self.d[0] = liste_cam_calib[0].mat_dist.copy()
self.d[1] = liste_cam_calib[1].mat_dist.copy()
taille = liste_cam_calib[0].taille_img
for param in self.type_calib:
resultat = cv.stereoCalibrate(
self.pts_objets,
self.pts_camera_gauche,
self.pts_camera_droite,
self.m[0], self.d[0], self.m[1], self.d[1],
taille, self.R, self.T, self.E, self.F,
param,
(cv.TermCriteria_COUNT + cv.TermCriteria_EPS, 5000000, 1e-8))
self.rms, self.m[0], self.d[0], self.m[1],\
self.d[1], self.R, self.T, self.E, self.F = resultat
if self.R is not None:
resultat = cv.stereoRectify(self.m[0], self.d[0],
sys3d.m[1], sys3d.d[1],
taille, self.R,
self.T, self.R1,
self.R2, self.P1,
self.P2, self.Q,
cv.CALIB_ZERO_DISPARITY, -1)
self.R1, self.R2, self.P1, self.P2, self.Q, self.valid1, self.valid2 = resultat
return
def erreur_droite_epipolaire(self):
err = 0
nb_points = 0
for idx, pt in enumerate(sys3d.pts_camera_gauche):
nb_pt = len(sys3d.pts_camera_gauche[idx])
pt1 = cv.undistortPoints(pt, sys3d.m[0], sys3d.d[0], None, sys3d.m[0])
pt2 = cv.undistortPoints(sys3d.pts_camera_droite[idx], sys3d.m[1],
sys3d.d[1], None, sys3d.m[1])
droite1 = cv.computeCorrespondEpilines(pt1, 1, sys3d.F)
droite2 = cv.computeCorrespondEpilines(pt2, 2, sys3d.F)
for j in range(nb_pt):
errij1 = np.abs(pt1[j, 0, 0] * droite2[j, 0, 0] +
pt1[j, 0, 1] * droite2[j, 0, 1] + droite2[j, 0, 2])
errij2 = np.abs(pt2[j, 0, 0] * droite1[j, 0, 0] +
pt2[j, 0, 1]*droite1[j, 0, 1] + droite1[j, 0, 2])
err += errij1 + errij2
nb_points += nb_pt
return err / nb_points / 2
def permutation_taille(pts_aruco):
for ir in range(len(pts_aruco)):
d1, d2, d3 = pts_aruco[ir].shape
pts_aruco[ir] = pts_aruco[ir].reshape((d2, d1, d3))
return pts_aruco
def lire_images(liste_thread):
liste_images = []
nb_image_lue = 0
while nb_image_lue != len(liste_thread):
nb_image_lue = 0
for thread in liste_thread:
with thread.verrou:
if not thread.image_lue:
nb_image_lue = nb_image_lue + 1
for thread in liste_thread:
with thread.verrou:
image = thread.get_image()
if image is None:
print("Image vide")
return []
liste_images.append(image)
thread.image_lue = True
return liste_images
def zoom(x, w):
if w != 1:
y = cv.resize(src=x, dsize=None, fx=w, fy=w)
return y
return x
def ajouter_glissiere(nom_glissiere, nom_fenetre,
min_glissiere, max_glissiere,
val_defaut, fct_glissiere=None):
if fct_glissiere:
cv.createTrackbar(nom_glissiere, nom_fenetre,
val_defaut, max_glissiere - min_glissiere + 1,
fct_glissiere)
else:
cv.createTrackbar(nom_glissiere,
nom_fenetre,
val_defaut,
max_glissiere - min_glissiere + 1,
lambda *args: None)
cv.setTrackbarMin(nom_glissiere, nom_fenetre, min_glissiere)
cv.setTrackbarMax(nom_glissiere, nom_fenetre, max_glissiere)
cv.setTrackbarPos(nom_glissiere, nom_fenetre, val_defaut)
def lire_noeud(noeud):
if noeud.isSeq():
l = []
if noeud.at(0).isString():
for i in range(0, noeud.size()):
l.append(noeud.at(i).string())
if noeud.at(0).isReal():
for i in range(0, noeud.size()):
l.append(noeud.at(i).real())
if noeud.at(0).isInt():
for i in range(0, noeud.size()):
l.append(int(noeud.at(i).real()))
else:
if noeud.isString():
l = noeud.string()
elif noeud.isReal():
l = noeud.real()
elif noeud.isInt():
l = int(noeud.real())
else:
l = noeud.mat()
return l
def recherche_camera(api=CAMAPI):
liste_webcam_ouvertes = []
for i in range(NBCAMERA):
webcam = cv.VideoCapture(i + api)
if webcam.isOpened():
liste_webcam_ouvertes.append((webcam, i))
return liste_webcam_ouvertes
def install_threads(liste_webcam_ouvertes):
liste_thread = []
for webcam in liste_webcam_ouvertes:
liste_thread.append(GestionFlux(webcam[0], webcam[1]))
for thread in liste_thread:
thread.start()
return liste_thread
def charger_donnees_calibration(nom_fichier, pc, sys3d):
fichier = cv.FileStorage(nom_fichier, cv.FILE_STORAGE_READ)
if not fichier.isOpened():
return False, pc, sys3d
if pc is not None:
if fichier.getNode("nbGrille") is not None:
nb_img_grille = lire_noeud(fichier.getNode("nbGrille"))
else:
nb_img_grille = 0
for idx in range(nb_img_grille):
noeud = fichier.getNode("Grille"+str(idx))
if noeud is not None:
pc.pts_camera.append(lire_noeud(noeud))
noeud = fichier.getNode("Objet"+str(idx))
if noeud is not None:
pc.pts_objets.append(lire_noeud(noeud))
fichier.release()
return True, pc, None
elif sys3d is not None:
if fichier.getNode("nbGrille") is not None:
nb_grille = lire_noeud(fichier.getNode("nbGrille"))
else:
nb_grille = 0
for idx in range(nb_grille):
noeud = fichier.getNode("GrilleG"+str(idx))
if noeud is not None:
sys3d.pts_camera_gauche.append(lire_noeud(noeud))
noeud = fichier.getNode("GrilleD" + str(idx))
if noeud is not None:
sys3d.pts_camera_droite.append(lire_noeud(noeud))
noeud = fichier.getNode("Objets" + str(idx))
if noeud is not None:
sys3d.pts_objets.append(lire_noeud(noeud))
fichier.release()
return True, None, sys3d
fichier.release()
return False, None, None
def charger_configuration(nom_fichier):
sys3d = CalibrageCamera3D()
suivi_dst = SuiviDistance()
stereo = ParamAlgoStereo()
cam_calib = []
mire = ParamMire()
cam_calib.append(CalibrageCamera())
cam_calib.append(CalibrageCamera())
cam_calib[0].index = 0
cam_calib[1].index = 1
cam_calib[0].idx_usb = 0
cam_calib[1].idx_usb = 1
fichier = cv.FileStorage(nom_fichier, cv.FILE_STORAGE_READ)
if not fichier.isOpened():
fichier.release()
return False, mire, cam_calib, sys3d, stereo
if not fichier.getNode("Echiquiernb_lig").empty():
mire.nb_lig = lire_noeud(fichier.getNode("Echiquiernb_lig"))
if not fichier.getNode("Echiquiernb_col").empty():
mire.nb_col = lire_noeud(fichier.getNode("Echiquiernb_col"))
if not fichier.getNode("Echiquierdim_carre").empty():
mire.dim_carre = lire_noeud(fichier.getNode("Echiquierdim_carre"))
if not fichier.getNode("Aruconb_lig").empty():
mire.nb_lig_aruco = lire_noeud(fichier.getNode("Aruconb_lig"))
if not fichier.getNode("Aruconb_col").empty():
mire.nb_col_aruco = lire_noeud(fichier.getNode("Aruconb_col"))
if not fichier.getNode("ArucoDim").empty():
mire.dim_aruco = lire_noeud(fichier.getNode("ArucoDim"))
if not fichier.getNode("ArucoSep").empty():
mire.sep_aruco = lire_noeud(fichier.getNode("ArucoSep"))
if not fichier.getNode("ArucoDict").empty():
mire.dict = lire_noeud(fichier.getNode("ArucoDict"))
if not fichier.getNode("Cam0index").empty():
cam_calib[0].idx_usb = lire_noeud(fichier.getNode("Cam0index"))
if not fichier.getNode("Cam0Size").empty():
cam_calib[0].taille_img = lire_noeud(fichier.getNode("Cam0Size"))
if not fichier.getNode("Cam1Size").empty():
cam_calib[1].taille_img = lire_noeud(fichier.getNode("Cam1Size"))
if not fichier.getNode("Cam1index").empty():
cam_calib[1].idx_usb = lire_noeud(fichier.getNode("Cam1index"))
if not fichier.getNode("type_calib1").empty():
cam_calib[1].type_calib = lire_noeud(fichier.getNode("type_calib1"))
if type(cam_calib[1].type_calib) is np.ndarray:
nb_element = cam_calib[1].type_calib.size
cam_calib[1].type_calib = list(cam_calib[1].type_calib.reshape(nb_element))
else:
param_optim = cam_calib[1].type_calib
cam_calib[1].type_calib = []
cam_calib[1].type_calib.append(param_optim)
if not fichier.getNode("type_calib0").empty():
cam_calib[0].type_calib = lire_noeud(fichier.getNode("type_calib0"))
if type(cam_calib[0].type_calib) is np.ndarray:
nb_element = cam_calib[0].type_calib.size
cam_calib[0].type_calib = list(cam_calib[0].type_calib.reshape(nb_element))
else:
param_optim = cam_calib[0].type_calib
cam_calib[0].type_calib = []
cam_calib[0].type_calib.append(param_optim)
if not fichier.getNode("type_calib").empty():
sys3d.type_calib = lire_noeud(fichier.getNode("type_calib"))
if type(sys3d.type_calib) is np.ndarray:
nb_element = sys3d.type_calib.size
sys3d.type_calib = list(sys3d.type_calib.reshape(nb_element))
else:
param_optim = sys3d.type_calib
sys3d.type_calib = []
sys3d.type_calib.append(param_optim)
if not fichier.getNode("cameraMatrice0").empty():
cam_calib[0].mat_intrin = fichier.getNode("cameraMatrice0").mat()
if not fichier.getNode("cameraDistorsion0").empty():
cam_calib[0].mat_dist = fichier.getNode("cameraDistorsion0").mat()
if not fichier.getNode("cameraMatrice1").empty():
cam_calib[1].mat_intrin = fichier.getNode("cameraMatrice1").mat()
if not fichier.getNode("cameraDistorsion1").empty():
cam_calib[1].mat_dist = fichier.getNode("cameraDistorsion1").mat()
if not fichier.getNode("ficDonnee0").empty():
cam_calib[0].fic_donnees = lire_noeud(fichier.getNode("ficDonnee0"))
if cam_calib[0].fic_donnees is not None:
f, p, s = charger_donnees_calibration(cam_calib[0].fic_donnees, cam_calib[0], None)
if f:
cam_calib[0] = p
if not fichier.getNode("ficDonnee1").empty():
cam_calib[1].fic_donnees = lire_noeud(fichier.getNode("ficDonnee1"))
if cam_calib[1].fic_donnees is not None:
f, p, s = charger_donnees_calibration(cam_calib[1].fic_donnees, cam_calib[1], None)
if f:
cam_calib[1] = p
if not fichier.getNode("ficDonnee3d").empty():
sys3d.fic_donnees = lire_noeud(fichier.getNode("ficDonnee3d"))
if sys3d.fic_donnees is not None:
f, p, s = charger_donnees_calibration(sys3d.fic_donnees, None, sys3d)
if f:
sys3d = s
if not fichier.getNode("R").empty():
sys3d.R = fichier.getNode("R").mat()
sys3d.T = fichier.getNode("T").mat()
sys3d.R1 = fichier.getNode("R1").mat()
sys3d.R2 = fichier.getNode("R2").mat()
sys3d.P1 = fichier.getNode("P1").mat()
sys3d.P2 = fichier.getNode("P2").mat()
sys3d.Q = fichier.getNode("Q").mat()
sys3d.F = fichier.getNode("F").mat()
sys3d.E = fichier.getNode("E").mat()
sys3d.valid1 = lire_noeud(fichier.getNode("rect1"))
sys3d.valid2 = lire_noeud(fichier.getNode("rect2"))
sys3d.m = [np.array((1, 1), np.uint8), np.array((1, 1), np.uint8)]
sys3d.d = [np.array((1, 1), np.uint8), np.array((1, 1), np.uint8)]
sys3d.m[0] = fichier.getNode("M0").mat()
sys3d.d[0] = fichier.getNode("D0").mat()
sys3d.m[1] = fichier.getNode("M1").mat()
sys3d.d[1] = fichier.getNode("D1").mat()
if not fichier.getNode("algo_sgbm").empty():
stereo.pre_filtre = lire_noeud(fichier.getNode("preFilterType"))
stereo.pre_filtre_cap = lire_noeud(fichier.getNode("pre_filtre_cap"))
stereo.taille_bloc = lire_noeud(fichier.getNode("blockSize"))
stereo.nb_disparite = lire_noeud(fichier.getNode("numDisparities"))
stereo.unicite = lire_noeud(fichier.getNode("uniquenessRatio"))
stereo.etendu_speck = lire_noeud(fichier.getNode("speckleRange"))
stereo.taille_speck = lire_noeud(fichier.getNode("speckleSize"))
return True, mire, cam_calib, sys3d, stereo
def sauver_configuration(nom_fichier, mire, cam_calib, sys3d, stereo):
d = datetime.datetime.now()
nom_fichier = nom_fichier+str(d)+".yml"
nom_fichier = nom_fichier.replace(':', '_')
nom_fichier = nom_fichier.replace('-', '_')
fichier = cv.FileStorage(nom_fichier, cv.FILE_STORAGE_WRITE)
if not fichier.isOpened():
return
fichier.write("date", str(d))
fichier.write("Echiquiernb_lig", mire.nb_lig)
fichier.write("Echiquiernb_col", mire.nb_col)
fichier.write("Echiquierdim_carre", mire.dim_carre)
fichier.write("Aruconb_lig", mire.nb_lig_aruco)
fichier.write("Aruconb_col", mire.nb_col_aruco)
fichier.write("ArucoDim", mire.dim_aruco)
fichier.write("ArucoSep", mire.sep_aruco)
fichier.write("ArucoDict", mire.dict)
fichier.write("type_calib0", np.array(cam_calib[0].type_calib))
fichier.write("Cam0index", cam_calib[0].idx_usb)
fichier.write("Cam0Size", np.array(cam_calib[0].taille_img))
fichier.write("type_calib1", np.array(cam_calib[1].type_calib))
fichier.write("Cam1index", cam_calib[1].idx_usb)
fichier.write("Cam1Size", np.array(cam_calib[1].taille_img))
if cam_calib[0].mat_intrin is not None:
fichier.write("cameraMatrice0", cam_calib[0].mat_intrin)
fichier.write("cameraDistorsion0", cam_calib[0].mat_dist)
fichier.write("ficDonnee0", cam_calib[0].fic_donnees)
if cam_calib[1].mat_intrin is not None:
fichier.write("cameraMatrice1", cam_calib[1].mat_intrin)
fichier.write("cameraDistorsion1", cam_calib[1].mat_dist)
fichier.write("ficDonnee1", cam_calib[1].fic_donnees)
if sys3d.R is not None:
fichier.write("ficDonnee3d", sys3d.fic_donnees)
fichier.write("type_calib", np.array(sys3d.type_calib))
fichier.write("R", sys3d.R)
fichier.write("T", sys3d.T)
fichier.write("R1", sys3d.R1)
fichier.write("R2", sys3d.R2)
fichier.write("P1", sys3d.P1)
fichier.write("P2", sys3d.P2)
fichier.write("Q", sys3d.Q)
fichier.write("F", sys3d.F)
fichier.write("E", sys3d.E)
fichier.write("rect1", np.array(sys3d.valid1))
fichier.write("rect2", np.array(sys3d.valid2))
fichier.write("M0", sys3d.m[0])
fichier.write("D0", sys3d.d[0])
fichier.write("M1", sys3d.m[1])
fichier.write("D1", sys3d.d[1])
if stereo is not None:
fichier.write("algo_sgbm", 1)
fichier.write("preFilterType", stereo.pre_filtre)
fichier.write("pre_filtre_cap", stereo.pre_filtre_cap)
fichier.write("blockSize", stereo.taille_bloc)
fichier.write("numDisparities", stereo.nb_disparite)
fichier.write("uniquenessRatio", stereo.unicite)
fichier.write("speckleRange", stereo.etendu_speck)
fichier.write("speckleSize", stereo.taille_speck)
def sauver_donnees_calibration(cam_calib1, cam_calib2, sys3d):
d = datetime.datetime.now()
if sys3d is None:
if cam_calib1 is None:
cam_calib = cam_calib2
else:
cam_calib = cam_calib1
cam_calib.fic_donnees = "Echiquier_" + str(cam_calib.idx_usb) +\
"_" + str(d)+".yml"
cam_calib.fic_donnees = cam_calib.fic_donnees.replace(':', '_')
cam_calib.fic_donnees = cam_calib.fic_donnees.replace('-', '_')
fichier = cv.FileStorage(cam_calib.fic_donnees, cv.FILE_STORAGE_WRITE)
if not fichier.isOpened():
fichier.release()
fichier.write("nbGrille", len(cam_calib.pts_camera))
nb_pts = 0
for tab_pts in cam_calib.pts_camera:
nb_pts += tab_pts.shape[0]
fichier.write("nbPoints", nb_pts)
for idx, tab_pts in enumerate(cam_calib.pts_camera):
fichier.write("Grille" + str(idx), tab_pts)
for idx, tab_pts in enumerate(cam_calib.pts_objets):
fichier.write("Objet" + str(idx), tab_pts)
fichier.release()
else:
sys3d.fic_donnees = "EchiquierStereo_" + str(d) + ".yml"
sys3d.fic_donnees = sys3d.fic_donnees.replace(':', '_')
sys3d.fic_donnees = sys3d.fic_donnees.replace('-', '_')
fichier = cv.FileStorage(sys3d.fic_donnees, cv.FILE_STORAGE_WRITE)
if not fichier.isOpened():
fichier.release()
fichier.write("nbGrille", len(sys3d.pts_camera_gauche))
for idx, tab_pts in enumerate(sys3d.pts_camera_gauche):
fichier.write("GrilleG" + str(idx), tab_pts)
for idx, tab_pts in enumerate(sys3d.pts_camera_droite):
fichier.write("GrilleD" + str(idx), tab_pts)
for idx, tab_pts in enumerate(sys3d.pts_objets):
fichier.write("Objets" + str(idx), tab_pts)
fichier.release()
class NuageVtk:
def __init__(self, x_min=-5, x_max=5, y_min=-5, y_max=5, z_min=0.0, z_max=5, max_pts=1e6):
self.max_pts = max_pts
self.vtkPolyData = vtk.vtkPolyData()
self.clear_pts()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(self.vtkPolyData)
mapper.SetColorModeToDefault()
mapper.SetScalarRange(z_min, z_max)
mapper.SetScalarVisibility(1)
self.vtkActor = vtk.vtkActor()
self.vtkActor.SetMapper(mapper)
self.nuage = None
self.x_min = x_min
self.y_min = y_min
self.z_min = z_min
self.x_max = y_max
self.y_max = x_max
self.z_max = z_max
def add_pts(self, point, color):
pt_id = self.vtk_pts.InsertNextPoint(point[:])
self.vtkCells.InsertNextCell(1)
self.vtkCells.InsertCellPoint(pt_id)
#https://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/ColoredPoints
self.colors.InsertNextTuple3(color[2], color[1], color[0])
self.vtkCells.Modified()
self.vtk_pts.Modified()
self.vtkDepth.Modified()
def trace(self, img, p3):
p = None
z = np.reshape(p3, (p3.shape[0] * p3.shape[1], 3))
c = np.reshape(img, (img.shape[0] * img.shape[1], 3))
idx = (z[:, 2] >= self.z_min) & (z[:, 2] < self.z_max) &\
(z[:, 1] >= self.y_min) & (z[:, 1] < self.y_max) &\
(z[:, 0] >= self.x_min) & (z[:, 0] < self.x_max)
p = z[idx, :]
c = c[idx, :]
for i in range(len(p)):
couleur = c[i, :]
self.add_pts(p[i, :], couleur)
# Renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(self.vtkActor)
renderer.SetBackground(.2, .3, .4)
renderer.ResetCamera()
# Render Window
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
# Interactor
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
# Begin Interaction
renderWindow.Render()
renderWindowInteractor.Start()
def clear_pts(self):
self.vtk_pts = vtk.vtkPoints()
self.colors = vtk.vtkUnsignedCharArray()
self.colors.SetNumberOfComponents(3)
self.colors.SetName("Colors")
self.vtkCells = vtk.vtkCellArray()
self.vtkDepth = vtk.vtkDoubleArray()
self.vtkDepth.SetName('DepthArray')
self.vtkPolyData.SetPoints(self.vtk_pts)
self.vtkPolyData.SetVerts(self.vtkCells)
self.vtkPolyData.GetPointData().SetScalars(self.vtkDepth)
self.vtkPolyData.GetPointData().SetActiveScalars('DepthArray')
self.vtkPolyData.GetPointData().SetScalars(self.colors)
class GestionFlux(threading.Thread):
def __init__(self, fluxvideo, idx):
threading.Thread.__init__(self)
self.video = fluxvideo
self.pgm_fin = False
self.idx = idx
self.derniere_image = []
self.cmd = 0
self.verrou = threading.Lock()
self.image_lue = True
def run(self):
if self.video is None:
print("Le flux n est pas ouvert. Fin du thread")
return
cmd = 0
while True:
if self.pgm_fin:
break
ret, img = self.video.read()
if ret:
while not self.image_lue and not self.pgm_fin:
time.sleep(0.001)
with self.verrou:
self.derniere_image = img
self.image_lue = False
cmd = self.cmd
self.cmd = 0
if cmd > 0:
if 32 <= cmd < 256:
cmd = chr(cmd)
self.gestion_cmd_camera(cmd)
if cmd != 0:
cmd = 0
def get_image(self):
if self.image_lue:
return None
return self.derniere_image
def prop_camera(self, param):
valeur = self.video.get(param[0]) + param[1]
self.video.set(param[0], valeur)
def gestion_cmd_camera(self, cmd):
modif_param_camera = {
'G': (cv.CAP_PROP_GAIN, 1, 'CAP_PROP_GAIN'),
'g': (cv.CAP_PROP_GAIN, -1, 'CAP_PROP_GAIN'),
'B': (cv.CAP_PROP_BRIGHTNESS, 1, 'CAP_PROP_BRIGHTNESS'),
'b': (cv.CAP_PROP_BRIGHTNESS, -1, 'CAP_PROP_BRIGHTNESS'),
'E': (cv.CAP_PROP_EXPOSURE, 1, 'CAP_PROP_EXPOSURE'),
'e': (cv.CAP_PROP_EXPOSURE, -1, 'CAP_PROP_EXPOSURE'),
'C': (cv.CAP_PROP_SATURATION, 1, 'CAP_PROP_SATURATION'),
'c': (cv.CAP_PROP_SATURATION, -1, 'CAP_PROP_SATURATION'),
'w': (cv.CAP_PROP_SETTINGS, 0.1, 'CAP_PROP_SETTINGS')
}
if cmd in modif_param_camera:
self.prop_camera(modif_param_camera[cmd])
else:
print("Code inconnu", cmd)
class SuiviDistance:
def __init__(self):
self.stereo = CalibrageCamera3D()
self.disparite = None
self.m = None
self.p = None
self.zoom = 1
def mesure_distance(self, event, x, y, flags, data):
if self.disparite is None:
return
if event == cv.EVENT_FLAG_LBUTTON:
dd = self.disparite[int(y / self.zoom), int(x / self.zoom)]
if dd < 0:
return
p = np.array([[[x / self.zoom, y/ self.zoom, dd]]])
if self.p is None:
self.p = p
else:
self.p = np.concatenate((self.p, p), axis=0)
self.m = cv.perspectiveTransform(src=self.p, m=self.stereo.Q)
print("\n ++++++++++\n", self.p, "\n")
for i in range(self.m.shape[0]):
print(self.p[i, :], " = ", self.m[i, :], " --> ",
cv.norm(self.m[i, :]), "\n")
if event == cv.EVENT_FLAG_RBUTTON:
self.p = None
def lire_resolution(webcam):
largeur = webcam.get(cv.CAP_PROP_FRAME_WIDTH)
hauteur = webcam.get(cv.CAP_PROP_FRAME_HEIGHT)
return largeur, hauteur
def fixer_resolution(webcam, resolution):
largeur, hauteur = resolution
status = webcam.set(cv.CAP_PROP_FRAME_HEIGHT, hauteur)
status = status and webcam.set(cv.CAP_PROP_FRAME_WIDTH, largeur)
nouveau_x, nouveau_y = lire_resolution(webcam)
return status and largeur == nouveau_x and hauteur == nouveau_y
def gestion_cmd_camera(param_ecran, liste_thread):
cmd_traitee = True
if param_ecran.mode_affichage & MODE_REGLAGECAMERA != 0:
if ord('0') <= param_ecran.code_touche_clavier <= ord('9'):
idx_nouveau = param_ecran.code_touche_clavier - 48
with liste_thread[idx_nouveau].verrou:
liste_thread[idx_nouveau].cmd = \
liste_thread[idx_nouveau].cmd | (MODE_REGLAGECAMERA)
with liste_thread[param_ecran.index_camera].verrou:
liste_thread[param_ecran.index_camera].cmd = \
liste_thread[1].cmd & (~MODE_REGLAGECAMERA)
param_ecran.index_camera = idx_nouveau
elif param_ecran.code_touche_clavier == ord('R'):
param_ecran.mode_affichage = param_ecran.mode_affichage & \
(~MODE_REGLAGECAMERA)
with liste_thread[param_ecran.index_camera].verrou:
liste_thread[param_ecran.index_camera].cmd = \
liste_thread[param_ecran.index_camera].cmd & \
(~MODE_REGLAGECAMERA)
print("Mode stereo")
else:
with liste_thread[param_ecran.index_camera].verrou:
liste_thread[param_ecran.index_camera].cmd = \
liste_thread[param_ecran.index_camera].cmd | param_ecran.code_touche_clavier
else:
if param_ecran.code_touche_clavier == ord('R'):
param_ecran.mode_affichage = param_ecran.mode_affichage | \
(MODE_REGLAGECAMERA)
with liste_thread[param_ecran.index_camera].verrou:
liste_thread[param_ecran.index_camera].cmd = \
liste_thread[param_ecran.index_camera].cmd | \
(~MODE_REGLAGECAMERA)
print("Mode reglage")
elif param_ecran.code_touche_clavier == ord('+'):
if param_ecran.zoom < 8:
param_ecran.zoom *= 2
elif param_ecran.code_touche_clavier == ord('-'):
if param_ecran.zoom > 0.1:
param_ecran.zoom /= 2
else:
cmd_traitee = False
return cmd_traitee, param_ecran
def gestion_cmd_stereo(param_ecran, liste_thread, liste_cam_calib, sys3d, mire):
if param_ecran.code_touche_clavier <= 0:
return param_ecran, liste_thread, liste_cam_calib, sys3d
caractere = chr(param_ecran.code_touche_clavier)
if caractere == 'b':
param_ecran.frame = np.zeros(
(param_ecran.taille_globale[1], param_ecran.taille_globale[0], 3),
np.uint8)
for cam_calib in liste_cam_calib:
cam_calib.pts_objets = []
cam_calib.fic_donnees = ""
cam_calib.pts_camera = []
sys3d.pts_camera_droite = []
sys3d.pts_camera_gauche = []
sys3d.fic_donnees = ""
sys3d.pts_objets = []
if caractere == 'c':
if len(liste_cam_calib) <= param_ecran.index_camera or \
len(liste_cam_calib[param_ecran.index_camera].pts_camera) == 0:
print("Aucune grille pour la calibration\n")
liste_cam_calib[param_ecran.index_camera].calibrer(tailleWebcam[param_ecran.index_camera])
print("RMS = ", liste_cam_calib[param_ecran.index_camera].rms,
"\n", liste_cam_calib[param_ecran.index_camera].mat_intrin,
"\n", liste_cam_calib[param_ecran.index_camera].mat_dist, "\n")
sauver_configuration("config.yml", mire, liste_cam_calib, sys3d, stereo)
elif caractere == 'e':
cv.imwrite("imgL.png", liste_images[0])
cv.imwrite("imgR.png", liste_images[1])
if suivi_dst.disparite is not None:
disparite = suivi_dst.disparite.astype(np.float)/16
fichier = cv.FileStorage("disparite.yml", cv.FILE_STORAGE_WRITE)
if fichier.isOpened():
fichier.write("Image", disparite)
fichier2 = cv.FileStorage("disparitebrut.yml", cv.FILE_STORAGE_WRITE)
if fichier2.isOpened():
fichier2.write("Image", suivi_dst.disparite)
elif caractere == 'D' or caractere == 'd':
if param_ecran.index_camera != 1:
liste_cam_calib[param_ecran.index_camera].pts_camera = []
param_ecran.index_camera = 1
r_dst = [tailleWebcam[1][0], 0, tailleWebcam[1][0], tailleWebcam[1][1]]
y = np.zeros((tailleWebcam[1][1], tailleWebcam[1][0], 3), np.uint8)
frame = param_ecran.frame
if caractere == 'D':
ret, y, _ = liste_cam_calib[1].analyse_charuco(
liste_images[param_ecran.index_camera], y, mire)
if caractere == 'd':
ret, y = liste_cam_calib[1].analyse_grille(
liste_images[param_ecran.index_camera], y, mire)
if ret:
frame[r_dst[1]:r_dst[1]+r_dst[3], r_dst[0]:r_dst[0]+r_dst[2]] = y +\
frame[r_dst[1]:r_dst[1]+r_dst[3], r_dst[0]:r_dst[0]+r_dst[2]]