Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: rbnkc on April 01, 2020, 08:12:44 PM

Title: Gradual Selection Python Script
Post by: rbnkc on April 01, 2020, 08:12:44 PM

Hi All,
I really need a python script to go through the gradual selection process in between camera alignment and dense cloud. There’s nothing in the list of programs on the wiki or github and although I’ve seen some discussions in the forum it’s fragmented and not a complete procedure. While many years ago I use to program in Pascal and Basic, I do not know python well enough to do this on my own. Thanks for any help I can get.
Title: Re: Gradual Selection Python Script
Post by: Alexey Pasumansky on April 02, 2020, 12:24:00 AM
Hello rbnkc,

Please check the following examples:
https://github.com/agisoft-llc/metashape-scripts/blob/master/src/contrib/gradual_selection_tie_points.py
https://agisoft.freshdesk.com/support/solutions/articles/31000154629-how-to-select-fixed-percent-of-the-points-gradual-selection-using-python

If you can describe the procedure that you want to script I'll try to assist you.
Title: Re: Gradual Selection Python Script
Post by: rbnkc on April 03, 2020, 12:47:14 AM
Thank you Alexey,

The gradual selection procedure I’ve found, that has been successful for my work is:
1.   Reconstruction Uncertainty – 50% of points or as near as possible to 10
2.   Optimize Cameras
3.   Projection Accuracy – 50% of points or as near as possible to 2.5
4.   Optimize Cameras
5.   Reprojection Error – 10% of points or as near as possible to .3
6.   Optimize Cameras with all lens parameters checked
7.   Repeat Reprojection Error/Optimize Camera until all above .3 pixels on reference error removed

The examples you provided I think contain all the elements needed for this. The target values and percentages can be constants. Some error checking on the iterative procedures would be probably be necessary. I may be able to put something together with this. It looks pretty straight forward. Any help would be greatly appreciated.
Title: Re: Gradual Selection Python Script
Post by: rbnkc on April 10, 2020, 10:09:12 PM
Alexey,

I've run the two scripts you referenced. The gradual selection script runs smoothly and I'm looking to modify it to my needs. The selection by percentage script, cut and pasted from the post, produces the error below. What am I missing ?

2020-04-10 13:49:14 Analyzing point cloud...
2020-04-10 13:49:15 Finished processing in 0.25 sec (exit code 1)
2020-04-10 13:49:15 Traceback (most recent call last):
2020-04-10 13:49:15   File "F:/Documents/Photogammetry/Python Scripts/selectfixedpercentageofpoints.py", line 21, in <module>
2020-04-10 13:49:15     for i in range(list_values):
2020-04-10 13:49:15 TypeError: 'list' object cannot be interpreted as an integer
2020-04-10 13:49:15 Error: 'list' object cannot be interpreted as an integer

Also,
What is the correct syntax for including the camera optimization after, or in, the "for chunk in Metashape" statements. I've tried several different placements and only generate errors. Sucks being a noob

Thanks for your help
>>>
Title: Re: Gradual Selection Python Script
Post by: UAV_project on April 14, 2020, 09:38:36 AM
The problem is that you need to change

Code: [Select]
for i in range(list_values):
to

Code: [Select]
for i in range(len(list_values)):
in line 21 of your script
Title: Re: Gradual Selection Python Script
Post by: rbnkc on April 19, 2020, 11:02:17 PM
That runs perfectly. Thank you, much appreciated UAV_project
Title: Re: Gradual Selection Python Script
Post by: JJR on January 12, 2021, 11:01:33 AM
Dear all,

May I ask you, rbnkc, or anyone else how you to include 'optimize cameras' after each of the gradual selection steps?

Thank you in advance for any help!
Title: Re: Gradual Selection Python Script
Post by: Uygar on January 12, 2021, 04:56:01 PM
Dear all,

May I ask you, rbnkc, or anyone else how you to include 'optimize cameras' after each of the gradual selection steps?

Thank you in advance for any help!

I wonder too. How ?
Title: Re: Gradual Selection Python Script
Post by: Alexey Pasumansky on January 13, 2021, 03:51:08 PM
Have you tried to add chunk.optimizeCameras() for every iteration?
Title: Re: Gradual Selection Python Script
Post by: JJR on January 13, 2021, 05:26:02 PM
Dear Alexey,

Thank you for the hint! That works fine if a single iteration is set as follows:

for chunk in Metashape.app.document.chunks:
    f = Metashape.PointCloud.Filter()
    f.init(chunk,Metashape.PointCloud.Filter.ReprojectionError)
    f.removePoints(reperr)
chunk.optimizeCameras(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,fit_p3=True,fit_p4=True,adaptive_fitting=False,tiepoint_covariance=False)

Like this, the script will run on every chunk that is in the project. How do I have to change that it will only run on the active chunk? I tried to change

"for chunk in Metashape.app.document.chunks"
into
"for chunk in Metashape.app.document.chunk"

but that resulted in an error. Please excuse my beginner's questions...

Best regards
Title: Re: Gradual Selection Python Script
Post by: Alexey Pasumansky on January 13, 2021, 07:59:26 PM
Hello JJR,

If you need to perform the operation for the single chunk only (for active chunk) you should remove the loop and just use chunk = Metashape.app.document.chunk for assignment.
Title: Re: Gradual Selection Python Script
Post by: JJR on January 14, 2021, 02:18:04 PM
Hello Alexey,

Thank you for the fast support! As soon as I understood what's part of the loop, it worked.

For silent readers, now it looks like this:

Code: [Select]
chunk = Metashape.app.document.chunk
f = Metashape.PointCloud.Filter()
f.init(chunk,Metashape.PointCloud.Filter.ProjectionAccuracy)
f.removePoints(projacc)
chunk.optimizeCameras(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,fit_p3=True,fit_p4=True,adaptive_fitting=False,tiepoint_covariance=False)

Original code from https://github.com/agisoft-llc/metashape-scripts/blob/master/src/contrib/gradual_selection_tie_points.py

Thank you again and kind regards.
Title: Re: Gradual Selection Python Script
Post by: Davier on March 02, 2021, 12:38:36 AM
Can you please post a link to the full script. This is exactly what I am looking for.

Many thanks,
David