Forum

Author Topic: Replace source values with estimated values in reference tab/pane  (Read 17559 times)

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Hi to everyone,
So here's the thing. I have one huge project with more than 1000 photos included for sure and one big trouble....well, I have multiple images with same names but they were shoot from different locations. One thing is clear, I can't go to that site anymore. These images was stored on different directories on my pc. Problem is that I want to enter, somehow automatically estimated values instead source values to minimize camera errors in report (cause source values are wrong, cause I did not use GPS RTK (or PPK) while capturing photos...)

My 'expected' workflow to replace source values with estimated was:
1.) Export reference values within reference pane tab. I choose 'cameras' when exporting and estimated values as well.
2.) Then I (re)import these estimated values as source values within import reference dialog...

Well, result is duplicate or even triple source values on same photo(s) name(s) (or even more than that). There's option to enter values manually but there's a tone of photos that needs to be edited...and that would took a while. 

I made a list of how many photos are duplicated or,  shows on more than that (in attachments you have screenshots of that too)

List of Photo names intervals and their repeating
DJI_0001 - DJI_0019 (IMG) 3X
DJI_0020 - DJI_0041 (IMG) 4X
DJI_0042 (IMG) 3X
DJI_0043 - DJI_0071 (IMG) 4X
DJI_0072 - DJI_0128 (IMG) 3X
DJI_0129 - DJI_0139 (IMG) 2X
DJI_0140 - DJI_0266 (IMG) 3X
DJI_0267 - DJI_0400 (IMG) 2X
DJI_0401 - DJI_0404 (IMG) 1X
DJI_0405 - DJI_0412 (IMG) 2X
DJI_0413 - DJI_0418 (IMG) 1X
DJI_0419 - DJI_0474 (IMG) 2X
DJI_0475 - DJI_0526 (IMG) 3X
DJI_0527 - DJI_0538 (IMG) 2X
DJI_0539 - DJI_0548 (IMG) 3X
DJI_0549 - DJI_0754 (IMG) 4X
DJI_0755 - DJI_0850 (IMG) 3X
DJI_0851 - DJI_0860 (IMG) 2X
DJI_0861 - DJI_0904 (IMG) 3X
DJI_0905 - DJI_0965 (IMG) 2X
DJI_0966 - DJI_0999 (IMG) 3X

Well, that's really big problem when you have older drone, in my case DJI Mavic Mini 1, and when Image number reaches 0999 then drone creates new folder with images starting from 0001, well that's the reason why I'm asking this. If someone know how to modify, on DJI Mavic Mini 1, that photos name use some conventional 'image name' format like year-month-day-hours-minutes-seconds (for example like phones have...for example: 20241216_115300.jpg) I will appreciate that also.

Please can you help me with this... I'll leave (in attachments) few images if you did not understand me what I want to accomplish, and what problem is...and of course link with the file exported from reference tab containing all stuff like (#label, X,Y,Z, Yaw,Pitch,Roll...etc)

Link to txt file with cameras positions informations: https://drive.google.com/file/d/1605LVauQniXbotASR8LMFArewXktCMk_/view?usp=drivesdk

Thank you very much for each suggestion or maybe solution to this issue.

Have a nice day!

J.K.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15691
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #1 on: December 16, 2024, 02:56:48 PM »
Hello Josip,

Maybe easier approach would be to use Python script in order to substitute source values in the Reference pane with estimated.

Something like the following:

Code: [Select]
chunk = Metashape.app.document.chunk
cameras = [c for c in chunk.cameras if c.type == Metashape.Camera.Type.Regular]
crs = chunk.crs
T = chunk.transform.matrix

for c in cameras:
if not c.transform:
c.reference.location = None
continue
coord = crs.project(T.mulp(c.center))
c.reference.location = coord
print("Script finished")

This code updates Source  values based on existing Estimated values. And for the not aligned cameras the reference information is cleared.
Best regards,
Alexey Pasumansky,
Agisoft LLC

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #2 on: December 16, 2024, 11:54:26 PM »
Hi Alexey,
Thank you so much!
I'm sorry cause I did not response to you earlier.
This script work so fast, I mean...Almost that I did not noticed what happened when I run script.
By the way, if you interested (or if you not) but. without script, I was doing on my own...with Excel approach...check this link..if you're interested: https://drive.google.com/drive/folders/1vC2atEArMEUOEAkka3nxXQn7LWrrwSSF?usp=sharing
But, I need intervals from every part of exported cameras from reference tab and also initial row when changing image intervals.
Later, I clear values from reference pane on selected images and then import values from Excel to these cameras.
Have a nice day :)

J.K.
   

 

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #3 on: December 17, 2024, 12:02:45 AM »
Hi, Alexey again,
I found that source values for Pitch, Roll and Yaw did not replaced with estimated ones cause there's huge errors?
Why's that?

Best regards!

J.K.

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #4 on: December 17, 2024, 01:59:28 PM »
Hi to everybody,
If someone is here, can someone tell me is this script good enough to replace source values in reference tab with estimated ones, for cameras, including rotations (yaw, pitch, roll)

Here's the code...
Code: [Select]
import Metashape
import math

def matrix_to_euler(matrix):
    """Pretvara 3x3 rotacijsku matricu u Eulerove kutove (yaw, pitch, roll)."""
    yaw = math.degrees(math.atan2(matrix[1, 0], matrix[0, 0]))
    pitch = math.degrees(math.atan2(-matrix[2, 0], math.sqrt(matrix[2, 1]**2 + matrix[2, 2]**2)))
    roll = math.degrees(math.atan2(matrix[2, 1], matrix[2, 2]))
    return yaw, pitch, roll

chunk = Metashape.app.document.chunk
cameras = [c for c in chunk.cameras if c.type == Metashape.Camera.Type.Regular]
crs = chunk.crs
T = chunk.transform.matrix

for c in cameras:
    if not c.transform:
        c.reference.location = None
        c.reference.rotation = None
        continue

    # Postavljanje referentne lokacije
    coord = crs.project(T.mulp(c.center))
    c.reference.location = coord

    # Postavljanje referentne rotacije
    R = c.transform.rotation()  # Rotacijski dio transformacijske matrice kamere
    yaw, pitch, roll = matrix_to_euler(R)
    c.reference.rotation = Metashape.Vector([yaw, pitch, roll])

print("Script finished")



Thank you very much.
For reason, I'll explain later.

Have a nice day!

J.K.

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #5 on: December 17, 2024, 09:47:18 PM »
Well,
Source code does not work for me.
Please help me.
Thanks:)
BYE;
J.K.

Paulo

  • Hero Member
  • *****
  • Posts: 1608
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #6 on: December 17, 2024, 10:12:57 PM »
Hello JK,

to correctly define the rotación matrix, you should use following code (taking into account rotation to geocentric CS and then to local):
Code: [Select]
    ecef_crs = crs.geoccs
    rotation_ecef = (T * c.transform).rotation() * Metashape.Matrix.Diag((1, -1, -1)) 
    if chunk.euler_angles == Metashape.EulerAnglesOPK or chunk.euler_angles == Metashape.EulerAnglesPOK:
        localframe = crs.localframe(location_ecef)
    else:
        localframe = ecef_crs.localframe(location_ecef)
    R = localframe.rotation() * rotation_ecef  # Rotacijski dio transformacijske matrice kamere
    c.reference.rotation = Metashape.utils.mat2euler(R, chunk.euler_angles)

Try it, it should work
Best Regards,
Paul Pelletier,
Surveyor

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #7 on: December 17, 2024, 10:16:13 PM »
Hi Paulo,
Thanks for participation,
Can you please tell on which place should i put your code to mine?
Have a nice day!
J.K.

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #8 on: December 17, 2024, 10:27:56 PM »
Hi Paulo,
Well, I'm getting strange error while placing your code into my script so I suppose that I put your code wrongly to initial script.
Here's how now look script, which in fact DOES NOT WORK but anyways.
If you know better please point on my errors.

Code: [Select]
import Metashape
import math

def matrix_to_euler(matrix):
    """Pretvara 3x3 rotacijsku matricu u Eulerove kutove (yaw, pitch, roll)."""
    yaw = math.degrees(math.atan2(matrix[1, 0], matrix[0, 0]))
    pitch = math.degrees(math.atan2(-matrix[2, 0], math.sqrt(matrix[2, 1]**2 + matrix[2, 2]**2)))
    roll = math.degrees(math.atan2(matrix[2, 1], matrix[2, 2]))
    return yaw, pitch, roll

chunk = Metashape.app.document.chunk
cameras = [c for c in chunk.cameras if c.type == Metashape.Camera.Type.Regular]
crs = chunk.crs
T = chunk.transform.matrix

for c in cameras:
    if not c.transform:
        c.reference.location = None
        c.reference.rotation = None
        continue

    # Postavljanje referentne lokacije
    coord = crs.project(T.mulp(c.center))
    c.reference.location = coord

    ecef_crs = crs.geoccs
    rotation_ecef = (T * c.transform).rotation() * Metashape.Matrix.Diag((1, -1, -1))
    if chunk.euler_angles == Metashape.EulerAnglesOPK or chunk.euler_angles == Metashape.EulerAnglesPOK:
        localframe = crs.localframe(location_ecef)
    else:
        localframe = ecef_crs.localframe(location_ecef)
    R = localframe.rotation() * rotation_ecef  # Rotacijski dio transformacijske matrice kamere
    c.reference.rotation = Metashape.utils.mat2euler(R, chunk.euler_angles)

print("Script finished")

Have a nice day!!
Bye :)
J.K.

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #9 on: December 17, 2024, 10:43:12 PM »
Hi Paulo,
If everything I entered well this is what I get from MY previous code,
check for the image.
I get this error message: "name 'location_ecef' is not defined"

Have a nice day!

J.K.


Paulo

  • Hero Member
  • *****
  • Posts: 1608
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #10 on: December 18, 2024, 01:11:30 AM »
Hello JK,

yes there was an error in code. Please use following:
Code: [Select]
import Metashape
import math

chunk = Metashape.app.document.chunk
cameras = [c for c in chunk.cameras if c.type == Metashape.Camera.Type.Regular]
crs = chunk.crs
T = chunk.transform.matrix

for c in cameras:
    if not c.transform:
        c.reference.location = None
        c.reference.rotation = None
        continue

    # Postavljanje referentne lokacije
    coord = crs.project(T.mulp(c.center))
    c.reference.location = coord

    # Postavljanje referentne rotacije
    ecef_crs = crs.geoccs
    rotation_ecef = (T * c.transform).rotation() * Metashape.Matrix.Diag((1, -1, -1))
    if chunk.euler_angles == Metashape.EulerAnglesOPK or chunk.euler_angles == Metashape.EulerAnglesPOK:
        localframe = crs.localframe(T.mulp(c.center))
    else:
        localframe = ecef_crs.localframe(T.mulp(c.center))
    R = localframe.rotation() * rotation_ecef  # Rotacijski dio transformacijske matrice kamere
    c.reference.rotation = Metashape.utils.mat2euler(R, chunk.euler_angles)

print("Script finished")
« Last Edit: December 18, 2024, 01:15:44 AM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor

jkova96

  • Full Member
  • ***
  • Posts: 184
    • View Profile
Re: Replace source values with estimated values in reference tab/pane
« Reply #11 on: December 22, 2024, 12:11:16 PM »
Hi Paulo,

Thanks for your time and effort to make this script that works properly.
I'm sorry cause my feedback to you is bit late.
Best regards!

J.K.