Forum

Author Topic: Automatic Gradual Selection to obtain a certain number of point  (Read 1345 times)

Blegoff

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hi everyone !

to follow on my last post, (https://www.agisoft.com/forum/index.php?topic=16405.0), I need to thin the tie point to a certain number of point n.

To do that, I had two main ideas :

1. use the Tools => Thin Tie point => n tie point or in python API chunk.thinTiePoints(n)

for this option, I don't obtain the n best Tie Point but a number of Tie point that doesn't seem to be in a clear proportionate factor of the n given (for example : n = 100 n_final = 53, n = 200 nfinal = 111, n = 300 nfinal = 180, n = 400 nfinal = 222 ...)

So first question, how does it chooses the good points ?
and  how can i obtain a define number of points ?

2. Use the gradual selections criterias

To do that, I used two functions from https://medium.com/@danfcarlson/how-to-use-python-to-streamline-your-agisoft-metashape-workflow-part-ii-e7e3fba8612d. And one that i coded myself. I removed in both scripts the chunk.Optmize camera, because i don't want to move camera positions once the alignment is performed.

code below :
Code: [Select]

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


def grad_selection_RE(RE_thrsh, num_tries=10, pct_del=10, thrsh_incr=0.1):
    n=0
    target_thrsh=0.3
    init_thrsh=RE_thrsh
    points = chunk.tie_points.points
    points_start_num = len(points)
    npoints = len(points)
    f = Metashape.TiePoints.Filter()
    f.init(chunk, criterion = Metashape.TiePoints.Filter.ReprojectionError)
    f.selectPoints(init_thrsh)
    # count number of points selected
    nselected = len([True for point in points if point.valid is True and point.selected is True])
    # check to see if any points were selected
    pct_selected = (nselected / points_start_num)*100
    if init_thrsh <= target_thrsh and pct_selected <= pct_del: # job done
        print(f"Now removing {nselected} points at a threshold of {init_thrsh}")
        f.removePoints(init_thrsh)
        #chunk.optimizeCameras(adaptive_fitting=True,fit_f = True, fit_cx = True, fit_cy = True, fit_b1 = True, fit_b2 = True, fit_k1 = True, fit_k2 = True, fit_k3 = True, fit_k4 = True, fit_p1 = True, fit_p2 = True)
    else:
        while True:
            n+=1
            if n>num_tries or init_thrsh<=target_thrsh or (100*((points_start_num-npoints)/points_start_num))>=pct_del:
                break
            else:
                points = chunk.tie_points.points
                npoints = len(points)
                f.selectPoints(init_thrsh)
                nselected = len([True for point in points if point.valid is True and point.selected is True])
                pct_selected = (nselected / npoints)*100
                while True:
                    if pct_selected <= pct_del:
                        init_thrsh -= thrsh_incr
                        f.selectPoints(init_thrsh)
                        nselected = len([True for point in points if point.valid is True and point.selected is True])
                        pct_selected = (nselected/npoints)*100
                    else:
                        break
                f.selectPoints(init_thrsh)
                nselected = len([True for point in points if point.valid is True and point.selected is True])
                if nselected > 0:
                    print(f"Removing {nselected} points at a threshold of {init_thrsh}")
                    f.removePoints(init_thrsh)
                    init_thrsh -= thrsh_incr
                    #chunk.optimizeCameras(adaptive_fitting=True,fit_f = True, fit_cx = True, fit_cy = True, fit_b1 = True, fit_b2 = True, fit_k1 = True, fit_k2 = True, fit_k3 = True, fit_k4 = True, fit_p1 = True, fit_p2 = True)
                    points = chunk.tie_points.points
                    npoints = len(points)
                   
def grad_selection_PA(PA_thrsh, num_tries=4, pct_del=10, thrsh_incr=1):
    n=0
    target_thrsh = 3
    init_thrsh=PA_thrsh
    points = chunk.tie_points.points
    points_start_num = len(points)
    npoints = len(points)
    f = Metashape.TiePoints.Filter()
    f.init(chunk, criterion = Metashape.TiePoints.Filter.ProjectionAccuracy)
    f.selectPoints(init_thrsh)
    # count number of points selected
    nselected = len([True for point in points if point.valid is True and point.selected is True])
    # check to see if any points were selected
    pct_selected = (nselected / points_start_num)*100
    if init_thrsh<=target_thrsh and pct_selected <= 50: # job done
        print(f"Now removing {nselected} points at a threshold of {init_thrsh}")
        f.removePoints(init_thrsh)
        #chunk.optimizeCameras(adaptive_fitting=True,fit_f = True, fit_cx = True, fit_cy = True, fit_b1 = True, fit_b2 = True, fit_k1 = True, fit_k2 = True, fit_k3 = True, fit_k4 = True, fit_p1 = True, fit_p2 = True)
    else:
        while True:
            n+=1
            if n>num_tries or init_thrsh<=target_thrsh or (100*((points_start_num-npoints)/points_start_num))>=50:
                break
            else:
                points = chunk.tie_points.points
                npoints = len(points)
                f.selectPoints(init_thrsh)
                nselected = len([True for point in points if point.valid is True and point.selected is True])
                pct_selected = (nselected / npoints)*100
                while True:
                    if pct_selected <= pct_del:
                        init_thrsh -= thrsh_incr/5
                        f.selectPoints(init_thrsh)
                        nselected = len([True for point in points if point.valid is True and point.selected is True])
                        pct_selected = (nselected/npoints)*100
                    else:
                        break
                f.selectPoints(init_thrsh)
                nselected = len([True for point in points if point.valid is True and point.selected is True])
                if nselected > 0:
                    print(f"Removing {nselected} points at a threshold of {init_thrsh}")
                    f.removePoints(init_thrsh)
                    init_thrsh -= thrsh_incr
                    #chunk.optimizeCameras(adaptive_fitting=True,fit_f = True, fit_cx = True, fit_cy = True, fit_b1 = True, fit_b2 = True, fit_k1 = True, fit_k2 = True, fit_k3 = True, fit_k4 = True, fit_p1 = True, fit_p2 = True)
                    points = chunk.tie_points.points
                    npoints = len(points)


def thin_tie_points(nb_points_min,Th_RE,Th_PA,chunk):
    points = chunk.tie_points.points
    npoints = len(points)
   
    while npoints > nb_points_min:
       
        grad_selection_RE(Th_RE,1)
        grad_selection_PA(Th_PA,1)
        Th_RE -= 0.2
        Th_PA -= 0.1
        points = chunk.tie_points.points
        npoints = len(points)
        if npoints < nb_points_min:
            break

thin_tie_points(100,3,3,chunk)

But when i run my function thin_tie_points(100,3,3,chunk), it work until 2000 points and then it freezes  ???.
Do you happen to know why ?

Thank you all for your replies,
Cheers
Bastien