Skip to content

Commit

Permalink
apib11: refactor examples
Browse files Browse the repository at this point in the history
  • Loading branch information
amrosu authored and jmmuller committed Mar 11, 2024
1 parent 98bda6c commit fa11a42
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 83 deletions.
6 changes: 3 additions & 3 deletions MMVII/apib11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ See 'examples' directory for use cases.

Binding Conventions
-------------------
- Implement python binding in a .cpp files that matches th C++ header.<p> (i.e. a binding for a C++ class that is declared in MMVII_MyClass.h we'll be implemented in py_MMVII_MyClass.cpp)
- Class names are the same than in C++ but without the leading 'c'
- functions, methods, properties are the same than in C++ but with initial letter in lower case (and 'm' removed from class variables name)
- implement python binding in a .cpp files that matches th C++ header.<p> (i.e. a binding for a C++ class that is declared in MMVII_MyClass.h we'll be implemented in py_MMVII_MyClass.cpp)
- class names are the same as in C++ but without the leading 'c'
- functions, methods, properties are the same as in C++ but with initial letter in lower case (and 'm' removed from class variables names)

9 changes: 6 additions & 3 deletions MMVII/apib11/examples/ex_camera_ori.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from MMVII import *
import MMVII

# Prepare data: see MMVII/MMVII-TestDir/Input/Saisies-MMV1/readme.md

dirData = '../../MMVII-TestDir/Input/Saisies-MMV1/'
cam = SensorCamPC.fromFile(dirData + 'MMVII-PhgrProj/Ori/toto/Ori-PerspCentral-IMGP4168.JPG.xml')
imPath = dirData + 'MMVII-PhgrProj/Ori/toto/Ori-PerspCentral-IMGP4168.JPG.xml'

cam = MMVII.SensorCamPC.fromFile(imPath)

pose = cam.pose
icalib = cam.internalCalib
Expand All @@ -22,7 +24,7 @@

i2pp=pp2i.mapInverse()
inv_proj=icalib.inv_Proj()
inv_dist=DataInvertOfMapping2D(icalib.dir_DistInvertible())
inv_dist=MMVII.DataInvertOfMapping2D(icalib.dir_DistInvertible())
pCam = inv_proj.value(inv_dist.value(i2pp.value((pid[0], pid[1]))))
pg = pose.value(pCam * (pid[2] / pCam[2]))

Expand All @@ -49,3 +51,4 @@

print (pids, "=>" )
print (pid_calcs)

37 changes: 21 additions & 16 deletions MMVII/apib11/examples/ex_test.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
from MMVII import *
import MMVII
import numpy as np

# Prepare data: see MMVII/MMVII-TestDir/Input/Saisies-MMV1/readme.md

dirData = '../../MMVII-TestDir/Input/Saisies-MMV1/'
calib=PerspCamIntrCalib.fromFile(dirData + 'MMVII-PhgrProj/Ori/toto/Calib-PerspCentral-Foc-28000_Cam-PENTAX_K5.xml')
imPath = '../../MMVII-TestDir/Input/EPIP/Tiny/ImL.tif'
dirOri = dirData + 'MMVII-PhgrProj/Ori/toto/'
calibFilePath = dirOri + 'Calib-PerspCentral-Foc-28000_Cam-PENTAX_K5.xml'
oriFilePath = dirOri + 'Ori-PerspCentral-IMGP4168.JPG.xml'

calib=MMVII.PerspCamIntrCalib.fromFile(calibFilePath)
print(calib.infoParam())

pp0=(1,2,3)
p0=(0,0)
p1=np.array((5,6))

box=Box2di(p0,p1)
box2=Box2di((-1,-1),(2,2))
box3=Box3dr((-3,-3,-2.5),(3,3,2.5))
box=MMVII.Box2di(p0,p1)
box2=MMVII.Box2di((-1,-1),(2,2))
box3=MMVII.Box3dr((-3,-3,-2.5),(3,3,2.5))

r=Rect2(box2)
r=MMVII.Rect2(box2)


m=Matrixr(10,10,ModeInitImage.eMIA_Rand)
mf=Matrixf(10,10,ModeInitImage.eMIA_Rand)
m=MMVII.Matrixr(10,10,MMVII.ModeInitImage.eMIA_Rand)
mf=MMVII.Matrixf(10,10,MMVII.ModeInitImage.eMIA_Rand)
a=np.array(m,copy=False)

im=Im2Di.fromFile("../../MMVII-TestDir/Input/EPIP/Tiny/ImL.tif")
im=MMVII.Im2Di.fromFile(imPath)
im_np=np.array(im,copy=False)

scpc=SensorCamPC.fromFile(dirData + 'MMVII-PhgrProj/Ori/toto/Ori-PerspCentral-IMGP4168.JPG.xml')
scpc=MMVII.SensorCamPC.fromFile(oriFilePath)
p=np.array([10,20,100])
diff=scpc.ground2ImageAndDepth(scpc.imageAndDepth2Ground(p))-p
print("diff = ",diff)


k=Isometry3D((0,0,0),((1,0,0),(0,1,0),(0,0,1)))
r=Rotation3D(((1,0,0),(0,1,0),(0,0,1)))
k=MMVII.Isometry3D((0,0,0),((1,0,0),(0,1,0),(0,0,1)))
r=MMVII.Rotation3D(((1,0,0),(0,1,0),(0,0,1)))

array = k.rot.array()

print(array)

l = Isometry3D((0,0,0),array)
l = MMVII.Isometry3D((0,0,0),array)

print (k == l)
print (k.rot == r)
Expand All @@ -49,19 +54,19 @@
print (np.array_equal(k.tr,(0,0,0)))

n = np.array( [ [1, 2, 3], [4, 5, 6] ] )
m = Matrixr(n)
m = MMVII.Matrixr(n)

print( np.array_equal( (n-m), (m-n) ))

m = Matrixr( [ [1, 2], [3, 4] ] )
m = MMVII.Matrixr( [ [1, 2], [3, 4] ] )
n = np.array(m)

print(n@m)
(m@n).show()
(m@m).show()

p = ([8,9])
v = Vectorr(p)
v = MMVII.Vectorr(p)
print(v)
print(m@p)
print(n@p)
Expand Down
108 changes: 47 additions & 61 deletions MMVII/apib11/examples/ex_tp_cam.py
Original file line number Diff line number Diff line change
@@ -1,121 +1,103 @@
from MMVII import *
import MMVII
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

"""
TP photogra
TP photogrammetry
Data from MurSaintMartin
Prepare data: see MMVII/MMVII-TestDir/Input/Saisies-MMV1/readme.md
"""

dirData = '../../MMVII-TestDir/Input/Saisies-MMV1/'
dirOri = dirData + 'MMVII-PhgrProj/Ori/toto/'
dirPts = dirData + 'MMVII-PhgrProj/PointsMeasure/Saisies_MMVII/'
imNames = ('IMGP4168.JPG', 'IMGP4169.JPG')

# from Ground-Pts3D.xml
pts3d = {
'I-of-I': (0.754045221758523621, 0.496427193197002514, 0.431416432371162006),
'Acute': (0.898750585424819226, 1.10409507249266081, 0.444680581722013746),
'Finger-1': (1.31771333753878528, 0.989188464947304724, 0.370627429238321537),
'Stone-6': (0.150697933447584281, 1.10818337179321613, 0.773464897817899177),
'Stone-7': (0.256396378205514475, 2.35318759021626445, 2.23887785258234207),
'Grille': (1.40207519252831658, 2.1945458898399548, 0.40382985984277503)
}

# from GroundMeasure.xml
pts2d = {
'IMGP4168.JPG' : {
'I-of-I': (430.52658272147653, 227.090326619720145),
'u-of-You': (495.717626755149013, 454.825546116027397),
'Acute': (431.735281413390453, 510.222991095907105),
'Finger-1': (316.373353330086218, 471.678176822389275),
'Stone-6': (667.402028214032839, 479.731591690534685),
'Stone-7': (1284.05120739189965, 981.021140912616147),
'Grille': (362.602015084684353, 1078.96450589692108)
},
'IMGP4169.JPG' : {
'I-of-I': (475.161154787777321, 516.404520623212079),
'u-of-You': (538.999416573275425, 732.995393503311675),
'Acute': (475.850364860953221, 802.548038024082189),
'Finger-1': (362.721106696891582, 789.307120972098687),
'Stone-6': (706.139274179003905, 741.525951210447488)
}
}
# 3D points
pts3d = MMVII.SetMesGCP.fromFile(dirPts + 'MesGCP-FromV1-Ground-Pts3D.xml')

# 2D points
pts2d = {}
for imName in imNames:
pts2d[imName] = MMVII.SetMesPtOf1Im.fromFile(dirPts + 'MesIm-' + imName + '.xml')

########################################################
# 1 - Read Calib, apply it
########################################################


imName1 = 'IMGP4168.JPG'

imName1 = imNames[0]
# Show 2d points
plt.title('Read 2d points')
allX, allY = zip(*pts2d[imName1].values())
allX, allY = zip(*[ mes.pt for mes in pts2d[imName1].measures()])
img = mpimg.imread(dirData + imName1)
plt.imshow(img)
plt.scatter(allX, allY, 30)
plt.show()

# apply calibration
calib = PerspCamIntrCalib.fromFile(dirData + 'MMVII-PhgrProj/Ori/toto/Calib-PerspCentral-Foc-28000_Cam-PENTAX_K5.xml')
calibPath = dirOri + 'Calib-PerspCentral-Foc-28000_Cam-PENTAX_K5.xml'
calib = MMVII.PerspCamIntrCalib.fromFile(calibPath)

dist = calib.dir_Dist()
pp2i = calib.mapPProj2Im()
i2pp = pp2i.mapInverse()
inv_dist = DataInvertOfMapping2D(calib.dir_DistInvertible())
inv_dist = MMVII.DataInvertOfMapping2D(calib.dir_DistInvertible())

for name, pt in pts2d[imName1].items():
for mes in pts2d[imName1].measures():
pt = mes.pt
print('Pt im: ', pt)
print(' -> central with disto: ', i2pp.value(pt))
print(' -> central no disto: ', inv_dist.value(i2pp.value(pt)))
print(' -> lig/col no disto: ', pp2i.value(inv_dist.value(i2pp.value(pt))))


def myPP2I(calib: PerspCamIntrCalib, pt_central: tuple) -> tuple:
def myPP2I(calib: MMVII.PerspCamIntrCalib, pt_central: tuple) -> tuple:
# TODO, see doc 3.2
pass

def myDist(calib: PerspCamIntrCalib, pt_central: tuple) -> tuple:
def myDist(calib: MMVII.PerspCamIntrCalib, pt_central: tuple) -> tuple:
# TODO, see doc ...
pass

print('Test myPP2I:')
for name, pt in pts2d[imName1].items():
for mes in pts2d[imName1].measures():
pt = mes.pt
print('Pt im: ', pt)
pt_central = i2pp.value(pt)
print(' -> central with disto: ', pt_central)
print(' -> my lig/col with disto: ', myPP2I(dist, pt_central))


print('Test myDist:')
for name, pt in pts2d[imName1].items():
for mes in pts2d[imName1].measures():
pt = mes.pt
print('Pt im: ', pt)
pt_central_nodist = inv_dist.value(i2pp.value(pt))
print(' -> central no disto: ', pt_central_nodist)
print(' -> my central with disto: ', myDist(dist, pt_central_nodist))



########################################################
# 2 - Read Ori, projection
########################################################

cam1= SensorCamPC.fromFile(dirData + 'MMVII-PhgrProj/Ori/toto/Ori-PerspCentral-'+imName1+'.xml')
cam1= MMVII.SensorCamPC.fromFile(dirOri + 'Ori-PerspCentral-'+imName1+'.xml')

# project ground points on image
allX, allY,_ = zip(*map(cam1.ground2ImageAndDepth,pts3d.values()))
allX, allY,_ = zip(*map(cam1.ground2ImageAndDepth,[mes.pt for mes in pts3d.measures()]))

plt.title('Projected 3d points')
img = mpimg.imread(dirData + imName1)
plt.imshow(img)
plt.scatter(allX, allY, 30)
plt.show()

def myGround2ImageAndDepth(camPose: Isometry3D, calib: PerspCamIntrCalib, pt3d: tuple) -> tuple:
def myGround2ImageAndDepth(camPose: MMVII.Isometry3D,
calib: MMVII.PerspCamIntrCalib,
pt3d: tuple) -> tuple:
# TODO, see doc 3.2
pass

print('Test myGround2ImageAndDepth')
for name, pt in pts3d.items():
for pt in [mes.pt for mes in pts3d.measures()]:
print('Proj MM: ', cam1.ground2ImageAndDepth(pt))
print('My proj: ', myGround2ImageAndDepth(cam1.pose, cam1.internalCalib, pt))

Expand All @@ -124,40 +106,44 @@ def myGround2ImageAndDepth(camPose: Isometry3D, calib: PerspCamIntrCalib, pt3d:
########################################################

# Second image
imName2 = 'IMGP4169.JPG'
cam2= SensorCamPC.fromFile(dirData + 'MMVII-PhgrProj/Ori/toto/Ori-PerspCentral-'+imName2+'.xml')
imName2 = imNames[1]
cam2= MMVII.SensorCamPC.fromFile(dirOri + 'Ori-PerspCentral-'+imName2+'.xml')

# Compute 2d points without disortion
pts2d_nodist = {}
for imName in pts2d.keys():
pts2d_nodist[imName] = {}
for name, pt in pts2d[imName].items():
for mes in pts2d[imName].measures():
name = mes.namePt
pt = mes.pt
pts2d_nodist[imName][name] = list(inv_dist.value(i2pp.value(pt)))


# TODO: compute 3d coordinates of common points
def myPseudoIntersect(cam1: SensorCamPC, pt2d1: tuple, cam2: SensorCamPC, pt2d2: tuple) -> tuple:
def myPseudoIntersect(cam1: MMVII.SensorCamPC, pt2d1: tuple,
cam2: MMVII.SensorCamPC, pt2d2: tuple) -> tuple:
# TODO, see doc 3.2
pass

print('Test myPseudoIntersect')
# apply to common points between im1 and im2
for ptName, pt1 in pts2d[imName1].items():
if not ptName in pts3d :
for mes in pts2d[imName1].measures():
ptName = mes.namePt
pt1 = mes.pt
if not ptName in [mes.namePt for mes in pts3d.measures()] :
continue
if not ptName in pts2d[imName2] :
if not pts2d[imName2].nameHasMeasure(ptName):
continue
pt2 = pts2d[imName2][ptName]
print(ptName, ': GND =', pts3d[ptName])
pt2 = pts2d[imName2].measuresOfName(ptName)
# for now, there is no pts3d.measuresOfName()
ground_coords = {mes.namePt:mes.pt for mes in pts3d.measures()}[ptName]
print(ptName, ': GND =', ground_coords)
print('Pseudo-intersection: ', myPseudoIntersect(cam1, pt1, cam2, pt2))


########################################################
# 4 - Orientation from known points
########################################################

# recompute orientation from 4 2d/3d points
# TODO



0 comments on commit fa11a42

Please sign in to comment.