1
Python and Java API / Disabling some photos that are close to object on a turntable
« on: September 07, 2024, 04:14:37 PM »
Good day everyone,
I am trying to disable some photos, for the texturing part, when modelling an object on a turntable. The object is scaled.
It seems that photos that are too close to the object are only focused on part of the object, due to the DOF. These closer images are blurring the texture, hence me trying to disable them. There are many other photos further away, in a larger orbit that I can use to texture.
So I am trying to disable the photos closer to the object by calculating their distances from the centroid of the object. Then I could disable those that are closer to the object.
I have a piece of code for that, but it seems to be giving me wrong measurements when I look at the relative distances of the cameras to the centroid.
I understand the centroid method is not exact, but it might be good enough.
Any ideas why, or any other method I could use?
Thanks
Roshan
def calculate_distance_of_camera_from_object_centroid():
reference_point = Metashape.Vector([0, 0, 0])
obj_path = "C:\\02854\\MS\\02854.psx"
if os.path.exists(obj_path):
doc = Metashape.Document()
doc.open(obj_path)
ch_index = 0
chunk = doc.chunks[ch_index]
#centre model
centroid = Metashape.Vector([0, 0, 0])
for vertex in chunk.model.vertices:
centroid += vertex.coord
centroid /= len(chunk.model.vertices)
print("Centroid:",centroid)
# Get the translation vector to move the centroid to the origin (0, 0, 0)
translation_vector = -centroid
# print('trans vector:',translation_vector)
# Create a transformation matrix for translation
translation_matrix = Metashape.Matrix().Translation(translation_vector)
# Apply the transformation to the chunk
chunk.transform.matrix = translation_matrix * chunk.transform.matrix
# Optionally, update bounding box and region
chunk.resetRegion()
reference_point = centroid
for camera in chunk.cameras:
if camera.transform is not None: # Ensure the camera has a known position
# Calculate the camera's position in world coordinates
camera_position = chunk.transform.matrix.mulp(camera.center)
print(camera_position)
# Calculate the distance from the camera to the reference point
distance = (camera_position - reference_point).norm()
# Print the camera label and its distance to the object
print(f"Camera {camera.label}: Distance to object = {distance:.2f} meters")
# Optionally, disable cameras that are too close (e.g., less than 0.5 meters)
distance_threshold = 0.5 # Set the desired threshold
if distance < distance_threshold:
camera.enabled = False
print(f"Camera {camera.label} disabled due to being too close: {distance:.2f} meters")
else:
print(f"Camera {camera.label} has no position data.")
doc.save()
print("project saved")
I am trying to disable some photos, for the texturing part, when modelling an object on a turntable. The object is scaled.
It seems that photos that are too close to the object are only focused on part of the object, due to the DOF. These closer images are blurring the texture, hence me trying to disable them. There are many other photos further away, in a larger orbit that I can use to texture.
So I am trying to disable the photos closer to the object by calculating their distances from the centroid of the object. Then I could disable those that are closer to the object.
I have a piece of code for that, but it seems to be giving me wrong measurements when I look at the relative distances of the cameras to the centroid.
I understand the centroid method is not exact, but it might be good enough.
Any ideas why, or any other method I could use?
Thanks
Roshan
def calculate_distance_of_camera_from_object_centroid():
reference_point = Metashape.Vector([0, 0, 0])
obj_path = "C:\\02854\\MS\\02854.psx"
if os.path.exists(obj_path):
doc = Metashape.Document()
doc.open(obj_path)
ch_index = 0
chunk = doc.chunks[ch_index]
#centre model
centroid = Metashape.Vector([0, 0, 0])
for vertex in chunk.model.vertices:
centroid += vertex.coord
centroid /= len(chunk.model.vertices)
print("Centroid:",centroid)
# Get the translation vector to move the centroid to the origin (0, 0, 0)
translation_vector = -centroid
# print('trans vector:',translation_vector)
# Create a transformation matrix for translation
translation_matrix = Metashape.Matrix().Translation(translation_vector)
# Apply the transformation to the chunk
chunk.transform.matrix = translation_matrix * chunk.transform.matrix
# Optionally, update bounding box and region
chunk.resetRegion()
reference_point = centroid
for camera in chunk.cameras:
if camera.transform is not None: # Ensure the camera has a known position
# Calculate the camera's position in world coordinates
camera_position = chunk.transform.matrix.mulp(camera.center)
print(camera_position)
# Calculate the distance from the camera to the reference point
distance = (camera_position - reference_point).norm()
# Print the camera label and its distance to the object
print(f"Camera {camera.label}: Distance to object = {distance:.2f} meters")
# Optionally, disable cameras that are too close (e.g., less than 0.5 meters)
distance_threshold = 0.5 # Set the desired threshold
if distance < distance_threshold:
camera.enabled = False
print(f"Camera {camera.label} disabled due to being too close: {distance:.2f} meters")
else:
print(f"Camera {camera.label} has no position data.")
doc.save()
print("project saved")