Forum

Author Topic: Accessing Invalid matches in Python  (Read 1921 times)

ckoeppl

  • Newbie
  • *
  • Posts: 1
    • View Profile
Accessing Invalid matches in Python
« on: January 14, 2020, 05:08:54 PM »
Hi, I am trying to access the invalid matches with the Python API, but I could not find a solution. Can anyone help me to find out?

In the GUI, I can right click on an image --> View Matches and then I see all valid and invalid matches between two images. I would like to be able to get that information also through the python API. I figured out how to do it for the valid matches, using the points in chunk.pointcloud.points, but this only gives valid points, no invalid points.

Some research told me, that chunk.pointcloud.tracks also holds the invalid matches. Unfortunately, it seems to me, that the object track only has the attribute color, and no id or anything else, which would help me to reconstruct in which image and where in that image the track is located.

Does anyone know a solution for that?

calberto.rueda

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Accessing Invalid matches in Python
« Reply #1 on: May 06, 2022, 02:53:42 PM »
Hi!

hope I am not a little too late to the party. Here is a code I worked to get the info of the total, valid and invalid matches from the API as it appears in the GUI. Hope it helps.

Best,
Carlos

Code: [Select]
import Metashape

doc = Metashape.app.document
chunk = doc.chunk

#Define values
point_cloud = chunk.point_cloud #current cloud
point_proj = point_cloud.projections #collection of projections
points = point_cloud.points #collection of points
npoints = len(points)

#Define Data Collections
projection_matches = list() #here projections for total matches will be stored
photo_matches = dict() #here points for valid matches will be stored
total_matches = list() #here the result will be stored

#add projections to collection
for photo in chunk.cameras:
photo_proj = point_proj[photo] #get individual projection
total_projections = set()
for proj in photo_proj:
total_projections.add(proj.track_id)
projection_matches.append(total_projections)


#add points to collection
for photo in chunk.cameras:

total_points = set() #only valid
point_index = 0
proj = point_proj[photo]

for cur_point in proj:

track_id = cur_point.track_id
while point_index < npoints and points[point_index].track_id < track_id:
point_index += 1
if point_index < npoints and points[point_index].track_id == track_id:

if point_cloud.points[point_index].valid:
total_points.add(point_index)
photo_matches[photo] = total_points

#do calculation magic
for i in range(0, len(chunk.cameras) - 1):

for j in range(i + 1, len(chunk.cameras)):
projection_match = projection_matches[i] & projection_matches[j] # Get projections intersections for cameras

MatchesKeys = [] # needed to call into dictionary
for keys in photo_matches.keys(): #convert dictionary keys into list
MatchesKeys.append(keys)

point_match = photo_matches[MatchesKeys[i]] & photo_matches[MatchesKeys[j]] # Get points intersections for cameras

total = len(projection_match)
valid = len(point_match)
invalid = total - valid

total_matches.append((chunk.cameras[i].label, chunk.cameras[j].label, total, valid, invalid)) #the result - photo1, photo2, total, valid, invalid

print (total_matches)