Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: yangjie on July 27, 2018, 12:29:49 AM

Title: How to map a pixel in a orthophoto to the original images
Post by: yangjie on July 27, 2018, 12:29:49 AM
Hi,

I am new to Agisoft and didn't finish all the Python tutorial yet, but would like to know if it is possible to map a pixel in a orthophoto to all the original images. For example, if I detect an object in the orthophoto using an object detection model, can I easily extract the same object from all the original images? Which input file shall I use? Is there any Python script example I can study?

Thanks a lot!
Title: Re: How to map a pixel in a orthophoto to the original images
Post by: Alexey Pasumansky on September 19, 2018, 09:49:19 PM
Hello yangjie,
If you have drawn the point shape on the orthomosaic and want to get it's coordinates on the source images you should at first use "update altitude" command from teh shape context menu in the Ortho view mode to ensure that shape vertex has all three coordinates and then execute the following code (providing that you have only one shape in the active chunk):

Code: [Select]
import PhotoScan
chunk = PhotoScan.app.document.chunk
shape = chunk.shapes[0]
T = chunk.transform.matrix
crs = chunk.crs
point = shape.vertices[0]
point_local = T.inv().mulp(chunk.crs.unproject(point))
for camera in chunk.cameras:
if not camera.transform:
continue
x, y  = camera.project(point_local)
if (0 <= x < camera.sensor.width) and (0 <= y < camera.sensor.height):
print(camera.label, str(x), str(y))

This code will output the XY coordinates of your original point shape on each image where this point is visible on.