Forum

Author Topic: Pixel Size  (Read 1548 times)

ctabain

  • Newbie
  • *
  • Posts: 6
    • View Profile
Pixel Size
« on: August 21, 2022, 01:26:09 AM »
Hello,

I am wanting to build a texture with a texture size that is dynamic and customisable to each chunk, to ensure each is made at maximum resolution. I can estimate this from the model area and pixel size however, I cannot find a method to recall pixel size. The pixel size / resolution / GSD is available in the GUI when creating an orthomosaic (see image) so natrually I assume it should be a callable variable?


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14847
    • View Profile
Re: Pixel Size
« Reply #1 on: August 26, 2022, 02:15:49 PM »
Hello ctabain,

You can estimate average GSD for the project using the following code:
Code: [Select]
import Metashape, statistics, math

def percentile(data, perc: int):
size = len(data)
return sorted(data)[int(math.ceil((size * perc) / 100)) - 1]

def estimate_gsd_height(chunk):

cameras = [camera for camera in chunk.cameras if camera.transform and camera.type == Metashape.Camera.Type.Regular]
if not len(cameras):
print("No aligned cameras, script aborted")
return 0

point_cloud = chunk.point_cloud
points = point_cloud.points
npoints = len(points)
projections = point_cloud.projections
T = chunk.transform.matrix

cameras = [camera for camera in list(cameras) if (camera in projections)]

point_ids = [-1] * len(point_cloud.tracks)
for point_id in range(0, npoints):
point_ids[points[point_id].track_id] = point_id

dist = list()
flight_height = list()

for camera in cameras:
sensor = camera.sensor
f = sensor.calibration.f
dist_camera = list()
altitude = list()

for proj in projections[camera]:
track_id = proj.track_id
point_id = point_ids[track_id]
if point_id < 0:
continue
point = points[point_id]
if not point.valid:
continue

coord = point.coord
coord.size = 3
d = (T.mulp(coord) - T.mulp(camera.center)).norm()
dist_camera.append(d / f)
altitude.append(d)

flight_height.append(percentile(altitude, 20))
#print(camera.label, flight_height[-1])
#flight_height.append(statistics.median(altitude))
dist.append(statistics.median(dist_camera))

X = statistics.median(flight_height)
#X = sum(flight_height) / len(flight_height)
gsd = sum(dist)/len(dist)
print("GSD = " + str(gsd))
print("Flight height = " + str(X))
return gsd, X

chunk = Metashape.app.document.chunk
estimate_gsd_height(chunk)

Even if it doesn't give you the same result as the default value in Build Orthomosaic dialog, the value will be quite close.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ctabain

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Pixel Size
« Reply #2 on: August 27, 2022, 05:44:26 AM »
Little bit longer than I was hoping but thankyou very much.