Forum

Author Topic: PointCloud.renderPreview() geolocation?  (Read 6017 times)

assafge

  • Newbie
  • *
  • Posts: 6
    • View Profile
PointCloud.renderPreview() geolocation?
« on: January 11, 2024, 09:50:12 AM »
Hello,
I'm using the python API to create a preview image of the point cloud, and I can't figure out how to pin a point in the result image to geolocation. I thought that the result area of the image is the chunk.region, but the bottom left point does not seems to collide with the region's origin. how can I calculate the bounding box of the result image in my project crs (espg)?

Code: [Select]
center = chunk.center
shape = chunk.region.size
region_rect = [center[:2] - shape[:2]/2, center[:2] + shape[:2]/2,
                         center[:2] + np.array(shape[0], -shape[1])/2, center[:2] + np.array(-shape[0], shape[1])/2]
region_proj = np.array([chunk.crs.project(
    chunk.transform.matrix.mulp(Metashape.Vector(pt.tolist() + [z_mean])) for pt in region_rect])
bl = region_proj.min(axis=0)[:2]
chunk.buildDepthMaps(downscale=1)
chunk.buildPointCloud()
preview_im = chunk.point_cloud.renderPreview(width=int(shape[0]/res), height=int(shape[1]/res))
preview_im.save(str(out_pc_im.with_name('preview_point_cloud_metashape.png')))
« Last Edit: January 11, 2024, 10:21:28 AM by assafge »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15433
    • View Profile
Re: PointCloud.renderPreview() geolocation?
« Reply #1 on: January 11, 2024, 01:35:46 PM »
Hello assafge,

Coordinate of what point you need to get for this script?
Best regards,
Alexey Pasumansky,
Agisoft LLC

assafge

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: PointCloud.renderPreview() geolocation?
« Reply #2 on: January 11, 2024, 02:00:29 PM »
the bottom left and the actual size that the preview image is representing, width for example.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15433
    • View Profile
Re: PointCloud.renderPreview() geolocation?
« Reply #3 on: January 11, 2024, 02:40:35 PM »
Hello assafge,

You may need then to consider renderImage instead of renderPreview, as it allows to specify the parameters of virtual camera.

Thus if you need to get the coordinates of the model point that appears in the bottom-left corner of the generated preview image, then you should intersect the ray that goes from the center of the virtual camera via the bottom-left corner of the virtual image (similar to the way the image footprints are generated in the following script: https://github.com/agisoft-llc/metashape-scripts/blob/master/src/footprints_to_shapes.py), then transform the coordinates of the obtained point from the internal coordinate system to geographic/projected.

If you do that for the points related to two corners, then you can also get the 3D distance or 2D distance (in the camera projection plane, for example) measured as a distance between them.

Getting some scale information from renderPreview result maybe quite complicated, as without transform argument specified for the command you would likely get different results, depending on the spread of the point cloud points in space (for example, for original and cropped point cloud).
Best regards,
Alexey Pasumansky,
Agisoft LLC

assafge

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: PointCloud.renderPreview() geolocation?
« Reply #4 on: January 11, 2024, 03:11:29 PM »
Hello Alexey,
I'm using renderPreview because I want a sort of ortho-photo, without the mosaicing of bits of images, I prefer an image with "holes". since it's not a virtual camera perspective, I thought that exported image is within the chunk.region. I've also exported the point cloud to pc3 file, and the points bounding box is not related to the rendered image.

In [1]: import numpy as np
   ...: import open3d as o3d

In [2]: pcd = o3d.io.read_point_cloud('/tmp/pc.pcd')

In [3]: out_arr = np.asarray(pcd.points)

In [4]: out_arr.min(axis=0)
Out[4]: array([2.49449300e+06, 6.96994850e+06, 3.73682281e+02])

In [5]: out_arr.min(axis=0)[0]
Out[5]: 2494493.0

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15433
    • View Profile
Re: PointCloud.renderPreview() geolocation?
« Reply #5 on: January 11, 2024, 05:34:47 PM »
Hello assafge,

Preview rendering feature does not consider the region, it tries to fit the source model (point cloud in your case) into the rendered image.

Using point_cloud.renderImage() would allow to define fixed position of the virtual camera center, for example, above the region center with fixed altitude offset, looking vertically down.

For example:
Code: [Select]
import Metashape
WIDTH = 5000

chunk = Metashape.app.document.chunk
region = chunk.region
location = region.center + region.rot * Metashape.Vector([0, 0, 2 * region.size.z])
R = region.rot.t() * Metashape.Matrix().Diag([1,-1,-1])

cameraT = Metashape.Matrix().Translation(location) * Metashape.Matrix().Rotation(R)
calibration = Metashape.Calibration()
calibration.width = WIDTH
calibration.height = int(WIDTH / region.size.x * region.size.y)
calibration.f = WIDTH // 2

image = chunk.point_cloud.renderImage(cameraT, calibration, point_size = 5)
image.save("D:/render.jpg")
print("done")
Best regards,
Alexey Pasumansky,
Agisoft LLC

assafge

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: PointCloud.renderPreview() geolocation?
« Reply #6 on: January 14, 2024, 11:08:37 AM »
Alexey, thanks for respond and the code, but it is a camera view, I would like to render some sort of ortho photo, but using point cloud instead of mosaicing pieces of images.