Forum

Author Topic: How does API determine region size?  (Read 451 times)

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
How does API determine region size?
« on: March 29, 2021, 09:24:01 PM »
I have a python script that looks like this. I am running 1.6.5

1. Create Metashape.Document()
2. Load photos, import reference:

Code: [Select]
curChunk = doc.addChunk()
curChunk.addPhotos(newCameras,strip_extensions=False)
curChunk.crs = Metashape.CoordinateSystem("EPSG::4326")
curChunk.importReference(locRefFile,Metashape.ReferenceFormatCSV,columns=myColumns,delimiter=', ',skip_rows=1)
curChunk.camera_location_accuracy = myAccuracyVector
curCunk.camera_rotation_accuracy=myHeadingAccuracyVector


3. Align photos

Code: [Select]
curChunk.matchPhotos(downscale=1,generic_preselection=False)
curChunk.alignCameras(adaptive_fittin=True)

4. Build mesh

Code: [Select]
curChunk.buildModel(surface_type=Metashape.HeightField,
 interpolation = Metashape.EnabledInterpolation,
 face_count=Metashape.MediumFaceCount,
 source_date=Metashape.DataSource.PointCloudDate,
 vertex_colors=False)

4. Build Ortho

Code: [Select]
curChunk.buildOrthomosaic(surface_data=Metashape.ModelData,
 blending_mode=Metashape.MosaicBlending,
 region=Metashape.BBox(Metashape.Vector([b0,b1]),Metashape.Vector([b2,b3]),
 projection=myUTMProjection)



I've left some out for clarity, but this summarized what I'm doing. Up until now this has always worked well for me. Recently I noticed though, that the region and corresponding mesh it's generating is smaller than my area. Sometimes it cuts off just one or two photos at the edge, sometimes almost half of my data. I can go in and manually resize the region, but I don't understand where in the code it is deciding to use such a small region.

My code iterates through several chunks like this. Other than when building my orthomosaic, I never explicitly set any region bounds.

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Re: How does API determine region size?
« Reply #1 on: April 30, 2021, 09:19:11 PM »
On a different post someone explained that the region after aligning points sets itself up automatically pick what it decides is the area of interest, which may exclude outliers if you don't have too many points in some sections.

So before building the mesh, I have to reset the region to encompass my full area of interest so I don't lose any possible information.

How can I do this given a bounding box of my area that I would like as the region?

I have an exact region BBox that I use when creating my orthomosaic, I want to set my region for the chunk to those same bounds, but the Region class uses center/rotation/size. How can I translate BBox to Region?

Paulo

  • Hero Member
  • *****
  • Posts: 614
    • View Profile
Re: How does API determine region size?
« Reply #2 on: May 03, 2021, 01:20:52 AM »
Hi ashalota,

since the ortho BB is a 2d oblect while the region is a 3d parallelepiped then the following code would keep the original region.size.z as well as region.center.z while the x,y extents and center would be set according to BB extents with region aligned to crs X, Y...

Code: [Select]
import Metashape as ps
chunk = ps.app.document.chunk
chunk.resetRegion()
ortho = chunk.orthomosaic
crs = ortho.crs
T = chunk.transform.matrix
s = chunk.transform.matrix.scale()
zg = crs.project(T.mulp(chunk.region.center)).z # keep original region center z
side3 = chunk.region.size.z                     # keep original region height
new_region = ps.Region()

BB = [ps.Vector((ortho.left,ortho.bottom)),ps.Vector((ortho.right,ortho.top))]  # BB in orthomosaic crs can replace with [ps.Vector((b0,b1)),ps.Vector((b2,b3))] from your build ortho script

# define region size.x and size.y according to BB extents
new_size = ps.Vector([(BB[1].x-BB[0].x)/s, (BB[1].y-BB[0].y)/s, side3])

# define region center at center of BB extents and zg
xcoord, ycoord, z = T.inv().mulp(crs.unproject(ps.Vector([(BB[0].x+BB[1].x)/2., (BB[0].y+BB[1].y)/2., zg])))
new_center = Metashape.Vector([xcoord, ycoord, z])

# define region rotation aligned  with ortho X,Y
v_t = T.mulp(Metashape.Vector( [0,0,0] ))
m = crs.localframe(v_t)
m = m * T
R = m.rotation()
new_region.rot = R.t()
new_region.center = new_center
new_region.size = new_size
chunk.region = new_region

Hopefully this can get you started...
« Last Edit: May 03, 2021, 10:18:22 AM by Paulo »
Best Regards,

Paul Pelletier

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Re: How does API determine region size?
« Reply #3 on: May 04, 2021, 12:35:52 AM »
Not quite right, seemed to place my region far off z-wise... I'll keep looking into it. Really what I want is to automatically set a region to contain all of my points and extend along my x/y bounds..I'm sure the math works out somehow, just don't have the details worked out.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 12742
    • View Profile
Re: How does API determine region size?
« Reply #4 on: May 04, 2021, 03:52:53 PM »
Hello ashalota,

trying to fit the complete tie point cloud by the bounding box (chunk.region) is many cases is not a good idea, as there could be some outliers, which are quite distant from the actual area of interest, so some additional checks should be applied to identify the outliers and remove them from the set of points to be used for the bounding box positioning.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ashalota

  • Jr. Member
  • **
  • Posts: 93
  • Forest orthomosaics, long transects (300m agl)
    • View Profile
    • NASA: G-LiHT (Public orthomosaics)
Re: How does API determine region size?
« Reply #5 on: May 04, 2021, 03:57:18 PM »
O. What would be the best way to include a given bounding box in the region? I already know it will encompass the area I'm interested in. My concern with the above solution (well, on top of the math not working out) was that I am not sure of the z coordinates, I'd like to capture all of it.

The images I'm working with are partially over water, so I'm having trouble with the default region discarding anything outside of the biggest island that it sees (since water is no points found), but I want to create the orthomosaic over the whole area. It's usually not that big an area, but the automatic region detection does not include things even within the same photo, so I end up losing data even from a photo that was aligned properly.