Forum

Author Topic: Import GCP per pixel  (Read 10659 times)

giancan

  • Newbie
  • *
  • Posts: 19
    • View Profile
Import GCP per pixel
« on: April 04, 2014, 06:14:47 PM »
Dear all,
I guess it would be quite nice to have a change to import ground control point of specific pixel of images.
Say that I georeference one single image in GIS program and I am satisfied with this georeferencing. Ideally each pixel of this 2D image has proper coordinates.
My requested feature would be to allow user to import gcp in a format similar to the following

Code: [Select]
imagename xpixel ypixel xcoordinate ycoordinate zcoordinate
Thanks for your help,
G.

willcodeforfoo

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Import GCP per pixel
« Reply #1 on: December 29, 2015, 06:52:50 PM »
+1 for this. It's been awhile since it was posted, has it (or something like it, that is defining GCPs in relation to pixel coordinates of source images) become a feature?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15117
    • View Profile
Re: Import GCP per pixel
« Reply #2 on: December 30, 2015, 02:02:57 PM »
Hello willcodeforfoo,

Here's an example of the script that loads both projections of the markers on the corresponding photos and the 3D world coordinates:
Code: [Select]
#compatibility Agisoft PhotoScan Professional 1.2.1
#markers import script
#input file format:
#marker_label, camera_label, x-pixel, y_pixel,x-coord, y-coord, z-coord
#(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
file = open(path, "rt") #input file

photos_total = len(chunk.cameras) #number of photos in chunk
markers_total = len(chunk.markers) #number of markers in chunk

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

while not eof:
   
#print(line)

sp_line = line.split(",", 6)   #splitting read line by four parts

y = float(sp_line[3]) #x- coordinate of the current projection in pixels
x = float(sp_line[2]) #y- coordinate of the current projection in pixels
x_coord = float(sp_line[4]) #x- coordinate of the marker
y_coord = float(sp_line[5]) #y- coordinate of the marker
z_coord = float(sp_line[6]) #x- coordinate of the marker

camera_label = sp_line[1] #camera label
marker_name = sp_line[0] #marker label

flag = 0
for camera in chunk.cameras:

if camera.label.upper() == camera_label.upper(): #searching for the image (comparing with all the photo paths in chunk)

for marker in chunk.markers: #searching for the marker (comparing with all the marker labels in chunk)
if marker.label == marker_name:
marker.projections[camera] = (x, y) #setting up marker projection of the correct photo)
flag = 1
break

if not flag: #adding new marker if no correspondence found
chunk.addMarker()
chunk.markers[-1].label = marker_name
chunk.markers[-1].projections[camera] = (x, y)  #setting up marker projection of the correct photo)

if not marker.reference.location:
marker.reference.location = PhotoScan.Vector([x_coord, y_coord, z_coord])

break

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()

Please pay attention to the input format and delimiter option in this example.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ARF__

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Import GCP per pixel
« Reply #3 on: February 22, 2016, 11:04:52 PM »
What is the input format of the files requested in this script?
thanks

Dang Vu

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Import GCP per pixel
« Reply #4 on: June 27, 2019, 04:23:49 AM »
My input file is txt/rt with format as below:
Code: [Select]
marker_label camera_label x-pixel y_pixel,x-coord y-coord z-coordBut when I run this py file, Photoscan have error: "invalid item value". I changed tab separator to comma separator but import only Name of marker, still error "invalid item value".
I using 1.4.5 version. Pls help me about this solution.
« Last Edit: June 27, 2019, 05:49:11 AM by Dang Vu »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15117
    • View Profile
Re: Import GCP per pixel
« Reply #5 on: June 27, 2019, 09:07:44 PM »
Hello Dang Vu,

Please check if the new script revision works in the latest Metashape Pro version:
Code: [Select]
import PhotoScan

def get_marker(label, chunk):
for marker in chunk.markers:
if marker.label == label:
return marker

marker = chunk.addMarker()
marker.label = label
return marker


chunk = Metashape.app.document.chunk
T = chunk.transform.matrix
crs = chunk.crs
path = Metashape.app.getOpenFileName("Specify the import file for markers:")

file = open(path, "rt")
lines = file.readlines()
for line in lines:
if line[0] == "#":
continue #skipping commented lines
if len(line.strip()) < 2:
continue #skipping empty lines
label, x, y, z = line.strip().split(",", 3)
x = float(x)
y = float(y)
z = float(z)

marker = get_marker(label, chunk)
marker.reference.location = Metashape.Vector([x, y, z])

for camera in chunk.cameras:
if camera.project(T.inv().mulp(chunk.crs.unproject(marker.reference.location))):
x_img, y_img = camera.project(T.inv().mulp(chunk.crs.unproject(marker.reference.location)))
if (0 <= x_img < camera.sensor.width) and (0 <= y_img < camera.sensor.height):
marker.projections[camera] = Metashape.Marker.Projection(Metashape.Vector([x_img,y_img]), True)
file.close()
print("script finished")
Best regards,
Alexey Pasumansky,
Agisoft LLC

inceo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Import GCP per pixel
« Reply #6 on: July 08, 2024, 03:33:06 PM »
Hi, I am using Ver 2.0.4 and have tried the above code (both the 2015 and 2019 versions). The latest one reads only 4 parameters, so there are no pixel coordinate values used. The earlier code does read pixel coordinates but nothing happens when I run the script. I have several images in the chunk of which a few are used with known x-pixel, y-pixel, x-coord, y-coord, z-coord for the GPCPs.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15117
    • View Profile
Re: Import GCP per pixel
« Reply #7 on: July 08, 2024, 04:53:20 PM »
Hello inceo,

Can you please give a few example lines for the input data that you have? Please also specify, if there is only single line per GCP and it is required to project back the estimated point location to other images, or whether same GCP could be defined in different lines (i.e projections could be specified for different images)?
Best regards,
Alexey Pasumansky,
Agisoft LLC

inceo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Import GCP per pixel
« Reply #8 on: July 09, 2024, 09:24:27 AM »
Hi Alexey

The lines here are for one GCP which are covered by several images, so with different X and Y pixel positions. So I want to do that for several GCPs, i.e. each GCP appears in a few images.
Format: GCP_nr Image_nr X_pix Y_pix Long Lat Alt

1   image_01   284   130   28.136518   -25.428954   1137
2   image_02   265   259   28.136518   -25.428954   1146
3   image_03   257   406   28.136518   -25.428954   1134
4   image_04   77   62   28.136518   -25.428954   1122
5   image_05   84   188   28.136518   -25.428954   1132
6   image_06   92   323   28.136518   -25.428954   1136
7   image_07   95   468   28.136518   -25.428954   1136

thanks