Forum

Author Topic: Pixel to 3D code very slow  (Read 3620 times)

fabvin

  • Newbie
  • *
  • Posts: 22
    • View Profile
Pixel to 3D code very slow
« on: November 20, 2020, 03:20:25 PM »
Dear all
I proposed a code (below) to extract world 3D coordinates from image pixels of each image of an already aligned chunk.

The code works well, but is extremly long, even if I sample the pixels of the images (about 2 hours/image). Have you an idea on how to speed it up?
Best regards
FabVin


Code: [Select]
import Metashape, math, os, csv

# Pixel coordinates to 3D coordinates
def pixel_to_point3D(imX,imY):
    point2D = PhotoScan.Vector([imX, imY])
    sensor = camera.sensor
    v = chunk.model.pickPoint(camera.center, camera.transform.mulp(sensor.calibration.unproject(point2D)))
    if(v==None):return None
    v_t = chunk.transform.matrix.mulp(v)
    v_t.size = 3
    v_out_world = chunk.crs.project(v_t)
    return v_out_world

# Create a sequence of numbers
def seq(start,stop,increment):
    n=list()
    for num in range(start,stop):
        if num % increment == 0:
            n.append(num)
            continue
        pass
    return n

doc=PhotoScan.app.document

shift=PhotoScan.Vector([727290,6265210,70])

for chunk in doc.chunks:
    for camera in chunk.cameras:
        with open(path+'OUT/COORDS/'+chunk.label+'/'+camera.label.split(".")[0]+'.csv', 'wt') as csvfile:
            spamwriter=csv.writer(csvfile,delimiter=',',quotechar='|',quoting=csv.QUOTE_NONE,lineterminator='\n')
            for y in seq(0,6016,50):
                for x in seq(0,4000,50):
                    result=pixel_to_point3D(x,y)
                    if(result != None):
                        result=result-shift
                        spamwriter.writerow([x]+[y]+[round(result[0],3)]+[round(result[1],3)]+[round(result[2],3)])


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15145
    • View Profile
Re: Pixel to 3D code very slow
« Reply #1 on: November 20, 2020, 06:14:05 PM »
Hello FabVin,

Does it work faster, if you replace camera.transform.mulp(sensor.calibration.unproject(point2D)) in your function with camera.unproject(point2D)?

The long time may be also caused by the big number of polygons in the mesh model that you are using for pickPoint operation, therefore you may need to decimate it or consider using a different surface, like dense cloud or tiled model, which should work faster for pickPoint due to the hierarchical structure.

I would also suggest to check, whether the writing operation takes long time (you can record time with and without last three lines, where the output is written).
Best regards,
Alexey Pasumansky,
Agisoft LLC

fabvin

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Pixel to 3D code very slow
« Reply #2 on: November 20, 2020, 07:03:41 PM »
Thank you Alexey for the usefull advices.
Unfortunately, replacing camera.transform.mulp(sensor.calibration.unproject(point2D)) in your function with camera.unproject(point2D) and commenting the writing block of the code did not improve its speed.

But trying to test dense_cloud.pickpoint instead of model.pickpoint was the solution! 10 s instead of 1 hour, thank you Alexey!

Best regards
« Last Edit: November 20, 2020, 07:13:53 PM by fabvin »