Forum

Author Topic: TiePoint Filtering  (Read 1850 times)

Etienne_U

  • Newbie
  • *
  • Posts: 5
    • View Profile
TiePoint Filtering
« on: May 09, 2023, 03:50:44 PM »
Dear Metashape Community,

I am writing an algorithm to keep only a percentage of the tiepoints after sort them by filtering.
For example, I want to filter tiepoints by ReprojectionError and keep the 80% best
then filter tiepoints by ReconstructionUncertainty and keep the 80% best
then filter tiepoints by ProjectionAccuracy and keep the 80% best

For example, if I first have 10 000 000 points and I want to keep each time 80%, then it should become :
10 000 000 -> 8 000 000
8 000 000 -> 6 400 000
6 400 000 -> 5 120 000

The problem is that it works for the first filter, and then it removes more than 80% ( for example I get 5 500 000 points instead of 6 400 000)
Does someone have an idea why ? You'll find my script attached to this message.

Thanks a lot,

Best regards,

Etienne

Code: [Select]
import Metashape
import glob
import os
import time
import math



def Tie_Points_Filtering():

    doc = Metashape.app.document
   
    # Demande à l'utilisateur du nom du chunk
    chunk_name = Metashape.app.getString("What is the chunk's name ?")

    # Itération au sein des chunks pour travailler dans celui sélectionner par l'utilisateur
    for chunk_it in doc.chunks:
        if chunk_name == chunk_it.label:
            chunk = chunk_it


    # Demande de combien de pourcent de tie points à garder
    TARGET_PERCENT = Metashape.app.getFloat("What percent of tie points would you like to keep after the alignement ?")


##########################          Filtre des tie points         #######################


   
    # Fonction pour supprimer les mauvais tie points
    def remove_tie_points(critere,TARGET_PERCENT):
       
        points = chunk.tie_points
        num_points = len(chunk.tie_points.points)
        target = num_points * TARGET_PERCENT / 100
       
        # Trie des Tiepoints selon leur erreur
        list_values = []
        f = Metashape.TiePoints.Filter()
        f.init(points, criterion = critere)
        list_values = f.values
        list_values.sort()

        # Threshold à appliquer
        threshold = list_values[int(target)]
        f.selectPoints(threshold)
        f.removePoints(threshold)

   

    # Suppresion des tie points pour selon les 3 différentes manières
    critere = Metashape.TiePoints.Filter.ReprojectionError
    remove_tie_points(critere,int(TARGET_PERCENT))
   
    critere = Metashape.TiePoints.Filter.ReconstructionUncertainty
    remove_tie_points(critere,int(TARGET_PERCENT))
   
    critere = Metashape.TiePoints.Filter.ProjectionAccuracy
    remove_tie_points(critere,int(TARGET_PERCENT))
   
    chunk.optimizeCameras(fit_f=True, fit_cx=True, fit_cy=True, fit_b1=False, fit_b2=False, fit_k1=True,
    fit_k2=True, fit_k3=True, fit_k4=True, fit_p1=True, fit_p2=True, fit_corrections=True,
    adaptive_fitting=True, tiepoint_covariance=True)


label = "Scripts/[02] Tie Points Filtering"
Metashape.app.addMenuItem(label, Tie_Points_Filtering)

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: TiePoint Filtering
« Reply #1 on: May 09, 2023, 06:01:09 PM »
Bonjour Etienne,

il semble que le nombre de points et les points du chunk.tie_points ne sont pas mis à jour à la suite de chaque sélection/élimination graduelle de points ...

J'ai modifié la fonction remove tie points pour qu'elle prenne en compte seulement les points valides avant élimination:
Code: [Select]
def remove_tie_points(critere,TARGET_PERCENT):

    points = chunk.tie_points
    num_points = len([i for i in chunk.tie_points.points if i.valid])
    target = num_points * TARGET_PERCENT / 100

    # Trie des Tiepoints selon leur erreur
    list_values = []
    f = Metashape.TiePoints.Filter()
    f.init(points, criterion = critere)
    list_values = [val for i,val in enumerate(f.values) if chunk.tie_points.points[i].valid]
    list_values.sort()

    # Threshold à appliquer
    threshold = list_values[int(target)]
    f.selectPoints(threshold)
    f.removePoints(threshold)

En espérant que cela peut marcher...
Best Regards,
Paul Pelletier,
Surveyor

Etienne_U

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: TiePoint Filtering
« Reply #2 on: May 10, 2023, 06:56:58 PM »
Bonjour Paul,

La modification a résolu le problème, merci 1000 fois !

Etienne

( for non french speakers : thanks to Paul it works ! )