Forum

Author Topic: Export markers per image  (Read 9495 times)

b3059651

  • Newbie
  • *
  • Posts: 25
    • View Profile
Export markers per image
« on: December 10, 2015, 05:11:16 PM »
Hi all,

I am using educational version 1.1.6 and I am interested in exporting markers (many) per camera. Actually what I do is import markers through the reference panel and then I can see the grey flag on the images. Is it possible to export only the visible markers  of each image with their pixel position? So I can get in the end a txt file that contains:

camera_label, marker_label, x_pixels, y_pixels

I am attaching the photo for an example to understand what I want.
Until now I am trying to play with a python script where I read a txt with the 3D position X,Y,Z for all the markers and manually import the camera label and I get in the console the pixel coordinates. It works fine  but it also gives the pix coordinates of points that are not included in the specified camera. And I am wondering if there is any way to solve this automatically so I can get only the included markers as you can see in the attached picture.


Thanks a lot for any feedback!

The script I am  using is:

Code: [Select]
#markers import script
#input file format:
#camera_label marker_label x y z
#(TAB separator)

import PhotoScan

doc = PhotoScan.app.document
chunk = doc.chunk

path = PhotoScan.app.getOpenFileName("Specify input file with marker coordinates:")

print("Import started...")  #informational message

sth={}
file = open(path, "rt") #input file

eof = False
line = file.readline()
if len(line) == 0:
eof = True

while not eof:
   
#print(line)

sp_line = line.rsplit(",", 4)   #splitting read line by 5 parts

oid = sp_line[0]
z = float(sp_line[3])
y = float(sp_line[2]) #x- coordinate of the current projection in pixels
x = float(sp_line[1]) #y- coordinate of the current projection in pixels
camera_label = 82          #camera label1

photo_0 = chunk.cameras[int(camera_label)]
point3D = PhotoScan.Vector([x,y,z])
point_geocentric = chunk.crs.unproject(point3D)
point_internal = chunk.transform.matrix.inv().mulp(point_geocentric)

imgx, imgy = photo_0.project(point_internal)
print(str(oid),str(imgx),str(imgy))    

       


line = file.readline() #reading the line in input file
if not len(line):
eof = True
break # End of File


print("Script finished")  #information message
file.close()
« Last Edit: December 10, 2015, 05:12:48 PM by b3059651 »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Export markers per image
« Reply #1 on: December 11, 2015, 01:09:47 PM »
Hello b3059651,

It is not quite clear if you wish to import or export the projections of the markers?

Note that grey flags actually represent the estimated positions for the current marker based on the projections (blue and green flags) on other images. So it is not actually the projection itself, but the predicted position shown only for the user convenience. Grey flags are not used in the processing.
Best regards,
Alexey Pasumansky,
Agisoft LLC

b3059651

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Export markers per image
« Reply #2 on: December 11, 2015, 02:41:09 PM »
Hello!

Thank you for the reply!

To make it clearer I import the markers from the reference panel and then I want to export their estimated projections in pixel coordinates for every image automatically. I use the above script to do that but it doesn't do it automatically because some markers are not visible in every camera so I get pixel coordinates negative and larger than the width of the camera. To solve this issue for now I take the printed results from the console and process them externally in python to get only the markers with the pixel coordinates that are within the width and height of the camera (3648x2736, specifically) Is this possible to do it in python built-in python scripts so to save some time?
I know that the grey flags do not take part into the process, I am just using them for other purposes.
 

Thanks a lot.
:)

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Export markers per image
« Reply #3 on: December 11, 2015, 02:52:22 PM »
Hello b3059651,

Not sure why do you have the negative coordinates of the marker projections. The following code should export only those marker projections that are present on the current photo:

Code: [Select]
for camera in chunk.cameras:

for marker in chunk.markers:
if not marker.projections[camera]:
continue
else:
x0, y0 = marker.projections[camera].coord
print(camera.label,marker.label,x0,y0)
Best regards,
Alexey Pasumansky,
Agisoft LLC

b3059651

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Export markers per image
« Reply #4 on: December 11, 2015, 03:26:43 PM »
Hello Alexey,

Thank you for your quick reply.
The code you send me exports the markers but only if they have blue flags. Is it possible to export also the estmated pixel coords of the markers with grey flags too?

Regards,



Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Export markers per image
« Reply #5 on: December 11, 2015, 03:56:56 PM »
Hello b3059651,

For markers that haven't got projections on the image (blue/green flags), but have suggested locations (grey flags), you can use the following (i've added it to the previous code):

Code: [Select]
for camera in chunk.cameras:

for marker in chunk.markers:
if not marker.projections[camera]:
                        key = 0
                        x0, y0 = camera.project(marker.position)
else:
                        key = 1
x0, y0 = marker.projections[camera].coord

        print(camera.label,marker.label,x0,y0, key)

So camera.project() should return only the values inside the image frame space. It it is not so in your case, please check that the code is working as expected in the version 1.2.
Best regards,
Alexey Pasumansky,
Agisoft LLC

b3059651

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Export markers per image
« Reply #6 on: December 11, 2015, 04:12:11 PM »
Hello Alexey,

It pratially worked! Thanks for that. I get the markers with grey flags, but nbot inside the image frame and also I get an error in the end.
See attached image.

Regards,

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Export markers per image
« Reply #7 on: December 11, 2015, 04:37:47 PM »
Hello b3059651,

For me this code worked as expected - only coordinates for visible blue/green/grey flags on the image frame space (but i'm using PhotoScan Pro 1.2):

Code: [Select]
import PhotoScan

doc = PhotoScan.app.document
chunk = doc.chunk

for camera in list(chunk.cameras):
for marker in list(chunk.markers):
if marker.projections[camera]:
key = 1
x0, y0 = marker.projections[camera].coord
else:
if camera.project(marker.position):
x0, y0 = camera.project(marker.position)
key = 0
else:
key = -1
if key + 1:
print(camera.label, marker.label, x0, y0, key)

print("That's all.")

Best regards,
Alexey Pasumansky,
Agisoft LLC

b3059651

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Export markers per image
« Reply #8 on: December 11, 2015, 04:59:21 PM »
Thank you Alexey,

It is working with no errors this time, but I still get the markers outside of the frame with negative coords. It might be the older version. It is ok for me because I can just do an extra python script outside Agisoft to remove the ouside frame coordinates.

Thanks for your quick response.
:)

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Export markers per image
« Reply #9 on: December 11, 2015, 05:15:46 PM »
Hello b3059651,

In the older version you can add some conditions to this script and check if 0 < x0 < camera.sensor.width -1 and 0 < y < camera.sensor.height - 1.
Best regards,
Alexey Pasumansky,
Agisoft LLC

b3059651

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Export markers per image
« Reply #10 on: December 11, 2015, 05:44:51 PM »
Yes, of course it worked with the condition!

Thanks :)

tutoni

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Export markers per image
« Reply #11 on: October 31, 2019, 10:07:17 AM »

Hi,
the script worked for me as well but I am wondering how I could make it work that I am only exporting the pixel locations only of markers with green flags (markers corrected by me), not even the blue ones?

Does anybody have a solution for that?

Thanks in advance!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14813
    • View Profile
Re: Export markers per image
« Reply #12 on: October 31, 2019, 01:07:25 PM »
Hello tutoni,

Please try this code to save only "pinned" marker projections (green flags):


Code: [Select]
import Metashape
doc = Metashape.app.document
chunk = doc.chunk
for marker in chunk.markers:
    for camera in marker.projections.keys():
        proj = marker.projections[camera]
        if proj.pinned:
            print(marker.label, camera.label, proj.coord.x, proj.coord.y)
Best regards,
Alexey Pasumansky,
Agisoft LLC