Forum

Author Topic: How to export depth map in python the same as GUI ?  (Read 1428 times)

lyhour

  • Newbie
  • *
  • Posts: 41
    • View Profile
How to export depth map in python the same as GUI ?
« on: December 21, 2021, 05:37:26 PM »
I export the depth from the GUI and I get the resolution of the image the same as RGB image and good result as show in the figure 1. However, I want to try the python to export the depth to obtain the result as the GUI. But, the result from the python is not the same as in in the GUI, as shown in the figure 2. Therefore, How can I solve this problem in python code. I will shows you the code in this post.
Code: [Select]
import Metashape
import numpy
from PySide2 import QtWidgets

def save_depth_maps():
chunk = Metashape.app.document.chunk #active chunk
#app = QtWidgets.QApplication.instance()
if not chunk.depth_maps:
message = "No depth maps in the active chunk. Script Aborted."
print(message)
Metashape.app.messageBox(message)
return 0

print("Script started...")
#app.processEvents() # this is the click even when we create the menu bar
if chunk.transform.scale:
scale = chunk.transform.scale
else:
scale = 1
count = 0
camera_list = list()

for camera in chunk.cameras:
camera_list.append(camera)

depth = chunk.depth_maps[camera_list[4]].image()
img = depth * 1
img.save('C:/Users/DepthMapGeneration/Depth/test7.jpg')
message = "Script finished "
print(message)
#print("Depth maps exported to:\n " + output_folder)
Metashape.app.messageBox(message)
return 1


#Metashape.app.addMenuItem("Custom menu/Save Depth Maps", save_depth_maps)

if __name__ == "__main__":
save_depth_maps()

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14840
    • View Profile
Re: How to export depth map in python the same as GUI ?
« Reply #1 on: December 22, 2021, 02:01:57 PM »
Hello lyhour,

Do you mean to get the result similar to Export Depth command? Export depth, actually, generates the depth data based on the distance from camera to the existing mesh model and saves it in 8-bit RGB image (grayscale). Is it what you are looking for? Or whether you need to save the original depth maps as grayscale (8-bir RGB) images?
Best regards,
Alexey Pasumansky,
Agisoft LLC

lyhour

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: How to export depth map in python the same as GUI ?
« Reply #2 on: December 23, 2021, 10:18:59 AM »
Thank you Alexey for your kindly response. First, I want to get the result similar as the Export Depth command. Second, I want to get the original depth. During these day, I found your suggestion in past few year ago (https://www.agisoft.com/forum/index.php?topic=6074.0). I will show you the coding. I run it and it is working well. However, I am not sure about the Metashape new version is update for make faster running or not. It quite slow due to two loop as you were mention. My main objective is I want to save the depth data as matlab file (.m or mat) file. I cannot do it. it would be better if you show me the way to save it.
Code: [Select]
import Metashape
import numpy
from scipy.io import savemat
from PySide2 import QtWidgets

def save_depth_maps():
chunk = Metashape.app.document.chunk #active chunk
#app = QtWidgets.QApplication.instance()
if not chunk.depth_maps:
message = "No depth maps in the active chunk. Script Aborted."
print(message)
Metashape.app.messageBox(message)
return 0

print("Script started...")
#app.processEvents() # this is the click even when we create the menu bar
if chunk.transform.scale:
scale = chunk.transform.scale
else:
scale = 1
count = 0
camera_list = list()

for camera in chunk.cameras:
camera_list.append(camera)

cam = chunk.cameras[4]
depth = chunk.model.renderDepth(cam.transform, cam.sensor.calibration)
depth_scaled = Metashape.Image(depth.width, depth.height, " ", "F32")
depth_grey = Metashape.Image(depth.width, depth.height, "RGB", "U8")
v_min = 10E10
v_max = -10E10

for y in range(depth.height):
for x in range(depth.width):
depth_scaled[x,y] = (depth[x,y][0] * scale, )
v_max = max(v_max, depth_scaled[x,y][0])
if depth_scaled[x,y][0]:
v_min = min(v_min, depth_scaled[x,y][0])

crange = v_max - v_min
for y in range(depth.height):
for x in range(depth.width):
color = int((v_max - depth_scaled[x,y][0]) / crange * 255)
depth_grey[x,y] = (color, color, color)

#print(type(depth_grey))
savemat("D:/DepthMapGeneration/Depth/test.mat", depth_grey.tostring())
depth_grey.save("D:/DepthMapGeneration/Depth/greyT.tif")

message = "Script finished "
#print(depth)
print(message)
Metashape.app.messageBox(message)
return 1

if __name__ == "__main__":
save_depth_maps()