# Forum

### Author Topic: Detect pixel in image based on coordinates  (Read 3677 times)

#### martinmokros

• Newbie
• Posts: 7
##### Detect pixel in image based on coordinates
« on: July 16, 2021, 09:34:36 PM »
Hi All,
I would like to search for pixels in images based on global coordinates after I have aligned images and georeferenced point clouds. So the images are aligned oriented and in a global coordinate system. Is it possible now to allocate specific points within these images? Search those images that are looking in that direction and then which pixel is the one that is facing that point.
Thank you

#### Alexey Pasumansky

• Agisoft Technical Support
• Hero Member
• Posts: 14771
##### Re: Detect pixel in image based on coordinates
« Reply #1 on: July 17, 2021, 01:21:03 AM »
Hello martinmokros,

Do you mean to get the 2D coordinates on the image (or set of images) of the 3D point based on it geographic coordinates?
Best regards,
Alexey Pasumansky,
Agisoft LLC

#### martinmokros

• Newbie
• Posts: 7
##### Re: Detect pixel in image based on coordinates
« Reply #2 on: July 17, 2021, 08:36:07 AM »
Hi Alexey,
I mean 2D coordinates on the image. So to get the position of the image cell (row, column), based on the global coordinates.
Thank you

#### Paulo

• Hero Member
• Posts: 1276
##### Re: Detect pixel in image based on coordinates
« Reply #3 on: July 17, 2021, 03:54:51 PM »
Hi martin,

for a point with given (X, Y, Z) global coordinates, the following code will select and print the pixel coordinates (u, v) for each image where it appears:
Code: [Select]
`chunk = Metashape.app.document.chunkT = chunk.transform.matrixX, Y, Z  = (463949.694,  5271949.401,  402.461) # point with X, Y, Z global coordinates in chunk.crs p = T.inv().mulp(chunk.crs.unproject(Metashape.Vector((X,Y,Z)))) # point in internal CSprint("Image pixel coordinates of point with following global coordinates ("+chunk.crs.name+"): ("+str(X)+","+str(Y)+","+str(Z)+")")for camera in chunk.cameras:    if not camera.project(p):    continue    u = camera.project(p).x   # u pixel coordinates in camera    v = camera.project(p).y   # v pixel coordinates in camera     if (u < 0 or u > camera.sensor.width or v < 0 or v > camera.sensor.height):    continue    print(" Camera: {} u: {:.3f}\tv: {:.3f}".format(camera.label,u,v))    camera.selected = True`
Hope this can be useful
« Last Edit: July 17, 2021, 07:38:09 PM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor

#### martinmokros

• Newbie
• Posts: 7
##### Re: Detect pixel in image based on coordinates
« Reply #4 on: July 19, 2021, 04:15:52 PM »
Hi Paulo,
Thank you very much. I have tried your code, and it works. So it has selected images that are facing the point.
Do you think it could be possible to identify which pixel in each image is that particular point? For example, to print row and column numbers for each image. Also, I am thinking now that it would be good to preselect images based on the distance from that point.
Thank you very much for your help
Martin

#### Paulo

• Hero Member
• Posts: 1276
##### Re: Detect pixel in image based on coordinates
« Reply #5 on: July 19, 2021, 06:26:24 PM »
Martin,

the code prints the u,v pixel coordinates for each image where ithe point  is seen. u coodinate is the column number from image upper left and v coordinate is row number from upper left....
Best Regards,
Paul Pelletier,
Surveyor

#### Jordan Pierce

• Newbie
• Posts: 23
##### Re: Detect pixel in image based on coordinates
« Reply #6 on: November 10, 2022, 11:47:49 PM »
Hi Paulo, thanks for providing this code snippet (and many others), it's been very helpful to learn from your comments across the forum.

In the above example that you show most of the cameras are looking downwards towards nadir, and the rays between the point (P) in internal coordinates going towards the source images are unobstructed. But how would you determine if a point (P) in internal coordinates is obstructed from the view of a camera that is facing in the direction of the point (P)? I'm in the process of projecting points (markers) from an orthomosaic (made from DEM) to all the contributing source images, and marking the corresponding pixels (u,v). Though, from my scene there some cameras taking images at highly oblique angles; when looking at all the marked pixels (u, v) within all the source images I can see markers that are located on the other side of a solid object. Is there a way to determine programmatically if that point (P) is in fact obstructed from the current source camera's view, and therefore not mark the "corresponding" u, v coordinate within the image?

I have access to all data (dense, mesh, dem, ortho, depth maps, etc.,)

Thanks.

#### Paulo

• Hero Member
• Posts: 1276
##### Re: Detect pixel in image based on coordinates
« Reply #7 on: November 11, 2022, 05:40:41 PM »
Hi Paulo, thanks for providing this code snippet (and many others), it's been very helpful to learn from your comments across the forum.

In the above example that you show most of the cameras are looking downwards towards nadir, and the rays between the point (P) in internal coordinates going towards the source images are unobstructed. But how would you determine if a point (P) in internal coordinates is obstructed from the view of a camera that is facing in the direction of the point (P)? I'm in the process of projecting points (markers) from an orthomosaic (made from DEM) to all the contributing source images, and marking the corresponding pixels (u,v). Though, from my scene there some cameras taking images at highly oblique angles; when looking at all the marked pixels (u, v) within all the source images I can see markers that are located on the other side of a solid object. Is there a way to determine programmatically if that point (P) is in fact obstructed from the current source camera's view, and therefore not mark the "corresponding" u, v coordinate within the image?

I have access to all data (dense, mesh, dem, ortho, depth maps, etc.,)

Thanks.

Hi Jordan,

your question is very interesting and I guess the solution would be that for any point P on the model, you would have to determine if the vector P to CameraCenter intersects any other triangle in the mesh. To do this efficiently and fast would be a good challenge....
Best Regards,
Paul Pelletier,
Surveyor

#### Jordan Pierce

• Newbie
• Posts: 23
##### Re: Detect pixel in image based on coordinates
« Reply #8 on: November 12, 2022, 01:12:21 AM »
Indeed

I'm currently looking into the method you mentioned in the post below, as I could use either the dense point cloud, mesh, or DEM as a surface:

https://www.agisoft.com/forum/index.php?topic=13965.msg61842#msg61842

Thoughts?

#### Paulo

• Hero Member
• Posts: 1276
##### Re: Detect pixel in image based on coordinates
« Reply #9 on: November 12, 2022, 07:31:12 AM »
Indeed

I'm currently looking into the method you mentioned in the post below, as I could use either the dense point cloud, mesh, or DEM as a surface:

https://www.agisoft.com/forum/index.php?topic=13965.msg61842#msg61842

Thoughts?

Jordan,

you refreshed my memory on some old topic. This script can be dona. Please PM me for more details,
« Last Edit: November 12, 2022, 04:38:37 PM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor