Forum

Author Topic: Create New Camera Object And Assign Transform Matrix  (Read 7108 times)

StormingPython

  • Guest
Create New Camera Object And Assign Transform Matrix
« on: November 05, 2021, 01:35:32 AM »
I would like to start off by saying that I am an extreme beginner who may have no clue as to what I am actually doing.

I am currently creating a script that takes in an image, creates a camera based on the Exif data of the image, then draws a bounding box onto the chunk based on the image. The code below is how I create the camera.

Code: [Select]
addedCam = chunk.addCamera()
addedCam.sensor = chunk.addSensor()
addedCam.label = "Test Camera"
addedCam.reference.location = [pictureLon, pictureLat, pictureAlt]
addedCam.reference.rotation = [pictureYaw, picturePit, pictureRoll]
addedCam.sensor.width = img.imageDimensions["Width"]
addedCam.sensor.height = img.imageDimensions["Height"]
addedCam.sensor.focal_length = img.focalLength
addedCam.transform = ??????

With all that information, I have a couple of questions:

1- Is there an easier (possibly a one-step) approach to creating a new camera where the only input that I would need is the image? If so, how do I go about doing so?

2- Is there any way to assign the camera a transformation matrix through Agisoft Metashape, or will I have to manually calculate the transform matrix? If I will need to manually calculate it, how do I go about doing so in terms of latitude, longitude, altitude, and yaw, pitch and roll?

3- I believe that I will need the transformation matrix in order to use the camera's unproject function but is there anything else that I will need besides the transformation matrix?

Thank you in advance to whomever helps out!

Paulo

  • Hero Member
  • *****
  • Posts: 1606
    • View Profile
Re: Create New Camera Object And Assign Transform Matrix
« Reply #1 on: November 05, 2021, 02:23:35 AM »
Hi Storming,

why do you just not use the addPhotos method that will automaticalle create the cameras according to exif present in each imge loaded and  also a corresponding sensor...

like if you have an image to load just do:
Code: [Select]
image_list = [pathtoimage]
chunk.addPhotos(image_list) #add image
and your image will be loaded in  Metashape with reference info according to Exif as well as the camera.sensor

as for camera.transform, this will be only created after photo alignment....

For question 2, have a look at https://github.com/agisoft-llc/metashape-scripts/blob/master/src/quick_layout.py to assign the reference info to estimated (ie transformation matrix) for each camera in project...

for question 3, I guess once you have camera.transform, you also need sensor.calibration in order to use camera.unproject method, ie. both camera extrinsics or external orientation (camera.transform) and camera intrinsics or internal orientation (camera.sensor.calibration).
For example given a point at a given image top left corner (0,0):
Code: [Select]
imgpt = Metashape.Vector((0,0)) # image top left corner
camera = chunk.cameras[0]
calib = camera.sensor.calibration
camera.transform.mulp(calib.unproject(imgpt))
Out[7]: 2021-11-04 20:12:41 Vector([-0.14196328895905663, -1.0067476730152016, 0.42209728482927544])
camera.unproject(imgpt)
Out[8]: 2021-11-04 20:12:51 Vector([-0.14196328895905663, -1.0067476730152016, 0.42209728482927544])
as you see, camera.unproject() is equivalent to camera.transform.mulp(calib.unproject())
Hope this can get you started...
« Last Edit: November 05, 2021, 05:16:45 AM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor

StormingPython

  • Guest
Re: Create New Camera Object And Assign Transform Matrix
« Reply #2 on: November 05, 2021, 06:16:39 AM »
Thank you, this has all been extremely useful.

For camera.transform, you said that it gets assigned to a matrix once photo alignment is performed. I used the method that you suggested for creating a camera using chunk.addPhotos(), and created a new camera out of a single input image. Its transform attribute was still None, so I attempted to align it and got the same result. The chunk that I'm working with currently has around 300 cameras that have undoubtedly been aligned. If I want to align my new camera in order to get a transformation matrix assigned to it, do I have to realign all of the other cameras as well by resetting their alignment then aligning them with the new camera? Is it possible that I could be missing something?

Paulo

  • Hero Member
  • *****
  • Posts: 1606
    • View Profile
Re: Create New Camera Object And Assign Transform Matrix
« Reply #3 on: November 05, 2021, 09:26:07 PM »
Hi,

if you import a single image with addPhotos(), it will have its reference info set from exif as weell as corresponding sensor. However, to get the transform, you must align but for that you need at least 2 overlapping images or cameras added to project...

To set transform matrix from reference data (X,Y,Z,yaw,pitch,roll) you can always use the quick layout script I referenced  and you will get a transform (image from dot to blue aligned rectangle)...

Hope this makes some sense, :-\
Best Regards,
Paul Pelletier,
Surveyor