Forum

Author Topic: Assign the proper camera location and orientation to the single image  (Read 1517 times)

alobo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Alexey,
Regarding the old thread
https://www.agisoft.com/forum/index.php?topic=9886.msg45182

Do you have a script for this purpose?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14890
    • View Profile
Re: Assign the proper camera location and orientation to the single image
« Reply #1 on: November 23, 2022, 04:00:44 PM »
Hello alobo,

Do you also (like in the linked thread) have only the projections of the markers in the pixel coordinates and XYZ coordinates of the related real-world points?
Best regards,
Alexey Pasumansky,
Agisoft LLC

alobo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Assign the proper camera location and orientation to the single image
« Reply #2 on: November 23, 2022, 06:24:40 PM »
I actually have the whole flight and generated the mosaic with Metashape,
but I wish an easy way to inspect the original photos for particular areas in the mosaic.
It would be enough to just have the original photos with approximate georeferencing from position and
orientation.
Regards,

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14890
    • View Profile
Re: Assign the proper camera location and orientation to the single image
« Reply #3 on: November 23, 2022, 06:54:32 PM »
Hello alobo,

If you have exterior orientation loaded to the Reference pane in a form of XYZ coordinates and Euler rotation angles, you can use quick layout script (https://github.com/agisoft-llc/metashape-scripts/blob/master/src/quick_layout.py) that will be applied to all Not Aligned images in the chunk.
Best regards,
Alexey Pasumansky,
Agisoft LLC

alobo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Assign the proper camera location and orientation to the single image
« Reply #4 on: November 23, 2022, 07:51:48 PM »
Thanks, we will try.
Would it be possible to save these rotation matrices as world files?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14890
    • View Profile
Re: Assign the proper camera location and orientation to the single image
« Reply #5 on: November 23, 2022, 09:25:53 PM »
Hello alobo,

I have the script sample for the version 1.6 for exporting the World files for all the aligned cameras, let me know, if it works on your side or returns any error:
Code: [Select]

import os
import Metashape
from PySide2 import QtWidgets

global app
app = QtWidgets.QApplication.instance()
doc = Metashape.app.document

chunk = doc.chunk
crs = chunk.crs
T = chunk.transform.matrix
print('Processing started...')
nprocessed = 0

if chunk.model:
surface = chunk.model
elif chunk.dense_cloud:
surface = chunk.dense_cloud
else:
surface = chunk.point_cloud

for photo in chunk.cameras:
if not photo.type == Metashape.Camera.Type.Regular: #skip camera track, if any
continue
if not photo.transform: #skip not aligned cameras
continue

A = D = B = E = C = F = 0.0
x0 = Metashape.Vector((0.0,0.0,0.0))
x1 = Metashape.Vector((0.0,0.0,0.0))
x2 = Metashape.Vector((0.0,0.0,0.0))

if (photo.photo.path.find("/") == -1):
name = photo.photo.path.rsplit("\\", 1)[1]
else:
name = photo.photo.path.rsplit("/", 1)[1]
path = photo.photo.path.rsplit(".", 1)[0]
photo_ext = photo.photo.path.rsplit(".",1)[1]

file_ext = ".jgw"
if (photo_ext.upper() in ["TIF", "TIFF"]):
file_ext = ".tfw"
elif (photo_ext.upper() in ["JPG", "JPEG"]):
file_ext = ".jgw"
file = open(path + file_ext, "wt")

# vectors corresponding to photo corners

sensor = photo.sensor
width = photo.sensor.width
height = photo.sensor.height
calib = photo.sensor.calibration
corners = list()
for i in [[0, 0], [sensor.width - 1, 0], [sensor.width - 1, sensor.height - 1], [0, sensor.height - 1]]:
corners.append(surface.pickPoint(photo.center, photo.transform.mulp(calib.unproject(Metashape.Vector(i)))))
if not corners[-1]:
corners[-1] = chunk.point_cloud.pickPoint(photo.center, photo.transform.mulp(calib.unproject(Metashape.Vector(i))))
if not corners[-1]:
break
corners[-1] = crs.project(T.mulp(corners[-1]))

if not all(corners):
print("Skipping camera " + photo.label + "...")
continue

x0 = corners[0]
x1 = corners[1]
x2 = corners[3]

# solution
C = x0[0]
F = x0[1]
A = (x1[0] - x0[0]) / width
D = (x1[1] - x0[1]) / width
B = (x2[0] - x0[0]) / height
E = (x2[1] - x0[1]) / height

n = "\n"
output_str = ("{0:.10f}".format(A) + n + "{0:.10f}".format(D) + n + "{0:.10f}".format(B) + n + "{0:.10f}".format(E) + n + "{0:.10f}".format(C) + n + "{0:.10f}".format(F) + n)
file.write(output_str)
file.close()
print(photo.label + " written...")
app.processEvents()
nprocessed += 1

print('Processing finished, generated ' + str(nprocessed) + ' world file(s)')
Best regards,
Alexey Pasumansky,
Agisoft LLC

alobo

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Assign the proper camera location and orientation to the single image
« Reply #6 on: November 29, 2022, 08:42:42 PM »
Thanks, excellent, it works very well. We've been able to create a shape file with this information to locate the original photograms on the mosaic.
I suggest Metashape produce this information as a regular output, it is very useful.
A similar script saving the x,y,z coordinates of the 4 corners and centers would also be highly appreciated.
« Last Edit: January 20, 2023, 03:59:36 PM by alobo »