Forum

Author Topic: Filter photos by point using Python API  (Read 1716 times)

ihabram

  • Newbie
  • *
  • Posts: 5
    • View Profile
Filter photos by point using Python API
« on: November 30, 2022, 02:12:39 PM »
Hello!

I am trying to perform two tasks using the Python API. (I have a project consisting 1155 RGB images. I generated a point cloud, using the GUI.)

1) I want to iterate through all points in the dense cloud. For every point I want to filter those photos where the point is visible and store their IDs in a list.
Filtering photos by point can be easily performed using the GUI (Filter photos by point), but I need to perform further steps in Python and I did not find a way to perform it.

2) Once I know which point is visible on which photo, I want to determine every point's pixel coordinate on those photos.

I am using Metashape Professional 1.8.4

Thank you for your help in advance!
« Last Edit: November 30, 2022, 03:06:17 PM by ihabram »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14855
    • View Profile
Re: Filter photos by point using Python API
« Reply #1 on: November 30, 2022, 03:05:13 PM »
Hello ihabram,

Please check, if the following code solves the first part of the task. It should create a dictionary, where the selected tie points ids are used as keys and list of the related cameras is assigned to each point id as a dictionary value:
Code: [Select]
import Metashape
def filter_cameras_by_tiepoints(chunk):
    #point_cloud = chunk.tie_points ####for version 2.0
    point_cloud = chunk.point_cloud ####for version 1.8
   
    projections = point_cloud.projections
    points = point_cloud.points
    tracks = point_cloud.tracks
    npoints = len(points)
    point_ids = [-1] * len(point_cloud.tracks)
    for point_id in range(0, npoints):
        point_ids[points[point_id].track_id] = point_id
       
    selected = dict()
       
    for camera in chunk.cameras:
        for proj in projections[camera]:
            track_id = proj.track_id
            point_id = point_ids[track_id]

            if point_id < 0:
                continue
            #if not points[point_id].valid: #skipping invalid points
            #    continue
            if points[point_id].selected: #skipping invalid points
                if point_id not in selected.keys():
                    selected[point_id] = [camera]
                else:
                    selected[point_id].append(camera)
    print("Script finished.")
    return selected
   
chunk = Metashape.app.document.chunk   
filter_cameras_by_tiepoints(chunk)
Best regards,
Alexey Pasumansky,
Agisoft LLC

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14855
    • View Profile
Re: Filter photos by point using Python API
« Reply #2 on: November 30, 2022, 03:17:09 PM »
For the second part you can access the proj.x and proj.y in the script above (should be accessed inside  if points[point_id].selected condition part). You can record this coordinates together with the cameras to the dictionary, as a tuple, for example: (camera, proj.x, proj.y) or use another list or dictionary, depending on the further usage.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ihabram

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Filter photos by point using Python API
« Reply #3 on: November 30, 2022, 03:19:00 PM »
Thank you for your answer!
I checked, unfortunately it returns with an empty dictionary.
How could I fix this issue?

Thanks in advance!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14855
    • View Profile
Re: Filter photos by point using Python API
« Reply #4 on: November 30, 2022, 03:23:05 PM »
Hello ihabram,

The script checks for the selected tie points.

If you would like to iterate through all the tie points, you need to replace the following line:
Code: [Select]
if points[point_id].selected:with this one:
Code: [Select]
if points[point_id].valid:
Best regards,
Alexey Pasumansky,
Agisoft LLC

ihabram

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Filter photos by point using Python API
« Reply #5 on: November 30, 2022, 05:11:24 PM »
Hello Alexey,

Thank you very much for your quick and helpful replies!
I did the modification in the code. In this case, after running the code Metashape does not respond and I have to force quit, even though I am using a powerful PC.
I also modified my original post and added that I want to iterate through the points of the dense cloud.

What could be the issue in my case, and how could I solve it?

Thank you in advance!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14855
    • View Profile
Re: Filter photos by point using Python API
« Reply #6 on: November 30, 2022, 05:41:47 PM »
Hello ihabram,

I suggest to check the code on the small project first, with the small number of tie points, like less than 1000 at first.

If you are planning to apply it to millions of points, you may need to modify the code further in order to store the extracted data in file instead of creating very long lists and dictionaries in memory. But in general Python code for millions of operations would work quite slow.

As for the dense cloud points, currently the access to those points is not available via Python, so you would need to export the point cloud to external text file, then read XYZ coordinates and apply camera.project method for each point (should be also transformed to the internal chunk's coordinate system) and check for every aligned camera, if the result of such projection fits the image dimensions. But actually, I think that this approach would take very long time for dense point clouds that have millions of points.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Paulo

  • Hero Member
  • *****
  • Posts: 1324
    • View Profile
Re: Filter photos by point using Python API
« Reply #7 on: November 30, 2022, 06:56:51 PM »
Many thanks to Alexey for his insightful code snippets! They are very helpful in our learning experience.
Best Regards,
Paul Pelletier,
Surveyor

ihabram

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Filter photos by point using Python API
« Reply #8 on: November 30, 2022, 08:49:57 PM »
.
« Last Edit: December 01, 2022, 02:12:17 AM by ihabram »

ihabram

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Filter photos by point using Python API
« Reply #9 on: December 01, 2022, 03:59:15 PM »
.
« Last Edit: January 03, 2023, 01:08:53 AM by ihabram »