Forum

Author Topic: Setting mapping region  (Read 35967 times)

gatsri

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: Setting mapping region
« Reply #15 on: July 08, 2014, 11:48:06 AM »
thank you very much!

and is there a option to run a script automatically on every photoscan start?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14796
    • View Profile
Re: Setting mapping region
« Reply #16 on: July 08, 2014, 11:51:41 AM »
Hello ristag,

You can put it to the following folder:
C:/users/<user name>/AppData/Local/AgiSoft/PhotoScan Pro/scripts

But I recommend to check every new script you are moving to the autorun folder to avoid any unwanted issues.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Romarb

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Setting mapping region
« Reply #17 on: July 22, 2014, 04:30:44 PM »
Hello!
I tried to use this script:

Code: [Select]
# Define: Map region
#MapRegionVec = [CenterX, CenterY, SizeX0, SizeY0, ZRotationDeg]
MapRegionVec = [304331.4, 5740607.3, 173.8, 143.6, -25.0]

# ALIGN PHOTOS
chunk.alignPhotos()

# SET MAPPING REGION (BOUNDING BOX)
# create new region object
newregion = PhotoScan.Region()
# Set region center:
centerUTM = PhotoScan.Vector([MapRegionVec[0], MapRegionVec[1], 0])
centerGEO = chunk.crs.unproject(centerUTM)
centerGEO.size = 4
centerGEO.w = 1
centerLocal = chunk.transform.inv() * centerGEO
newregion.center = PhotoScan.Vector([centerLocal[0], centerLocal[1], chunk.region.center[2]])
# Set region size
newregion.size = PhotoScan.Vector([MapRegionVec[2], MapRegionVec[3], chunk.region.size[2]])
# Set region rotation
# build rotation matrix in our coordinate system
RotZDeg = -MapRegionVec[4]
SinRotZ= math.sin(math.radians(RotZDeg))
CosRotZ= math.cos(math.radians(RotZDeg))
RotMat = PhotoScan.Matrix([[CosRotZ,-SinRotZ,0,0],[SinRotZ,CosRotZ,0,0],[0,0,1,0],[0,0,0,1]])
#  rotate region bounding box
T = chunk.transform
v = PhotoScan.Vector( [0,0,0,1] )
v_t = T * v
v_t.size = 3
m = chunk.crs.localframe(v_t)
m = m * T
m = RotMat*m
s = math.sqrt(m[0,0]**2 + m[0,1]**2 + m[0,2]**2) #scale factor
R = PhotoScan.Matrix( [[m[0,0],m[0,1],m[0,2]], [m[1,0],m[1,1],m[1,2]], [m[2,0],m[2,1],m[2,2]]])
R = R * (1. / s)
newregion.rot = R.t()
# put newregion to chunk
chunk.region = newregion

But it didn't work. I've changed: MapRegionVec = [5592663.063, 5840723.167, 500, 500, 0] and disable "Allign Photos" option because my photos are already alligned.
I set correct coordinate system in my project and assumed that CRS [in scritpt] is loaded from project.
What could be wrong?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14796
    • View Profile
Re: Setting mapping region
« Reply #18 on: July 22, 2014, 04:46:05 PM »
Hello eurosystem,

And what is the console pane output?

Probably, chunk variable used in the script is not defined? You need to assign it as chunk = PhotoScan.app.document.activeChunk.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Romarb

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Setting mapping region
« Reply #19 on: July 23, 2014, 09:18:21 AM »
Thank you Alexey, your advice was helpful and now it works fine:)

EDIT
unfortunately not everything is right
I think that my units are wrong. I set bounding box size as 1x1 and when I checked reg.size it shows values correctly. But when I analyzed result I  saw that region is much more bigger (~40). Further, reg.center is placed in wrong place- I set it as a point 105.

« Last Edit: July 23, 2014, 11:46:10 AM by eurosystem »

Romarb

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Setting mapping region
« Reply #20 on: July 23, 2014, 04:58:19 PM »
I haven't solved this problem yet:/
Maybe it is caused somehow by transformation?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14796
    • View Profile
Re: Setting mapping region
« Reply #21 on: July 23, 2014, 05:07:06 PM »
Hello eurosystem,

Could you please send the script you are using (with all the modifications) and the project file (just with sparse point cloud and markers, no need of dense cloud and mesh) to support@agisoft.ru?
Best regards,
Alexey Pasumansky,
Agisoft LLC

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14796
    • View Profile
Re: Setting mapping region
« Reply #22 on: July 24, 2014, 02:20:02 PM »
Hello eurosystem,

Seems like the problem is in the input coordinates for the starting point - it has zero altitude in the following line:
Code: [Select]
centerEPSG = PhotoScan.Vector([MapRegionVec[0], MapRegionVec[1], 0])if you change the third coordinate to corresponding marker altitude you'll get the region centered by source marker location.

As for the size, you need to put size section after rotation calculation:
Code: [Select]
real_size = PhotoScan.Vector([MapRegionVec[2], MapRegionVec[3], 200])  #here z-size is 200m
newregion.size = real_size / s
« Last Edit: July 24, 2014, 03:55:10 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

ppkong

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Setting mapping region
« Reply #23 on: September 29, 2014, 03:57:10 AM »
Hello,

Somewhere on forum I have already posted the code that rotates the bounding box in accordance of the chunk coordinate system, so you need to use it and for additional rotations multiply the matrix (m) by rotation 3x3 matrices:

Code: [Select]
T = chunk.transform
v = PhotoScan.Vector( [0,0,0,1] )
v_t = T * v
v_t.size = 3

m = chunk.crs.localframe(v_t)
m = m * T
s = math.sqrt(m[0,0]**2 + m[0,1]**2 + m[0,2]**2) #scale factor
R = PhotoScan.Matrix( [[m[0,0],m[0,1],m[0,2]], [m[1,0],m[1,1],m[1,2]], [m[2,0],m[2,1],m[2,2]]])

R = R * (1. / s)
reg = chunk.region
reg.rot = R.t()
chunk.region = reg

If chunk.transform and chunk.crs are None, you should use identity matrix instead them.
When I run python script in PS1.0 everything is ok,but some code can't run in PS 1.1 pre. It always prompt me some code error, for example "v_t = T * v","chunk.transform.inv()" etc

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14796
    • View Profile
Re: Setting mapping region
« Reply #24 on: September 29, 2014, 02:16:52 PM »
Hello ppkong,

There were some major changes in Python API for version 1.1.

Transformation matrix for chunk is now accessible via chunk.transform.matrix.
Best regards,
Alexey Pasumansky,
Agisoft LLC

jeremyeastwood

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Setting mapping region
« Reply #25 on: October 04, 2014, 02:45:08 AM »
Hi Alexey,

I've used following the code (taken from the code earlier in this thread - big thanks to you and the other contributors!) to change my bbox x and y dimensions (and center location), however I'm also seeing an unwanted shift in the bbox z center location when the transformation is applied.  This is particularly strange because the chunk region size and center z values (checked via the console) are unchanged after the transformation.  I've attached screenshots of the bbox before and after transformation, showing the console output for the region size and center.

My code:

Code: [Select]
import PhotoScan, math

doc = PhotoScan.app.document
chunk = doc.activeChunk
reg = chunk.region
trans = chunk.transform

newregion = PhotoScan.Region()
map_center = [-122.4004003, 37.7772147]
map_size = [50., 50.]
RotZDeg = 0.0

# SET MAPPING REGION (BOUNDING BOX)

# Set region center:
center_geo = PhotoScan.Vector([map_center[0], map_center[1], 0.])  # uses existing region height

v_temp = chunk.crs.unproject(center_geo)
v_temp.size = 4
v_temp.w = 1
centerLocal = chunk.transform.inv() * v_temp
centerLocal.size = 3

newregion.center = PhotoScan.Vector([centerLocal[0], centerLocal[1], reg.center[2]])  # uses existing region height

# Set region size

#<---- Rotation ---->
rot_untransformed = PhotoScan.Matrix().diag([1,1,1,1])
rot_temp = trans * rot_untransformed 

s = math.sqrt(rot_temp[0, 0]**2 + rot_temp[0, 1]**2 + rot_temp[0, 2]**2)
R = PhotoScan.Matrix( [[rot_temp[0,0],rot_temp[0,1],rot_temp[0,2]], [rot_temp[1,0],rot_temp[1,1],rot_temp[1,2]], [rot_temp[2,0],rot_temp[2,1],rot_temp[2,2]]])
R = R * (1.0 / s)

#<---- Size ---->
inter_size = PhotoScan.Vector([0,0,0])
geo_size = PhotoScan.Vector(map_size)  # uses original chunk region z size
inter_size = geo_size / s 

newregion.size = PhotoScan.Vector([inter_size[0], inter_size[1], reg.size[2]])

# Set region rotation
SinRotZ = math.sin(math.radians(RotZDeg))
CosRotZ = math.cos(math.radians(RotZDeg))
RotMat = PhotoScan.Matrix([[CosRotZ, -SinRotZ, 0, 0], [SinRotZ, CosRotZ, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])  # just rotate about z-axis
#  rotate region bounding box
T = chunk.transform
v = PhotoScan.Vector([0, 0, 0, 1])
v_t = T * v
v_t.size = 3
m = chunk.crs.localframe(v_t)
m = m * T
m = RotMat*m
s = math.sqrt(m[0, 0]**2 + m[0, 1]**2 + m[0, 2]**2)  # scale factor
R = PhotoScan.Matrix([[m[0, 0], m[0, 1], m[0, 2]], [m[1, 0], m[1, 1], m[1, 2]], [m[2, 0], m[2, 1], m[2, 2]]])
R = R * (1. / s)
newregion.rot = R.t()

# put newregion to chunk
chunk.region = newregion

Any ideas on what I need to do to keep the z size and center fixed would be greatly appreciated.
« Last Edit: October 04, 2014, 02:49:55 AM by jeremyeastwood »

ppkong

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Setting mapping region
« Reply #26 on: October 17, 2014, 09:39:02 AM »
Hello ppkong,

There were some major changes in Python API for version 1.1.

Transformation matrix for chunk is now accessible via chunk.transform.matrix.
Thanks for your reply,there have many error when i run old script in 1.1 so can you tell me How to set map region in Python API 1.1?
« Last Edit: October 17, 2014, 10:06:43 AM by ppkong »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14796
    • View Profile
Re: Setting mapping region
« Reply #27 on: October 17, 2014, 04:15:24 PM »
Hello ppkong,

Try to replace doc.activeChunk by doc.chunk and chunk.transform by chunk.transform.matrix. I think that there should not be other changes for 1.1, but if it still doesn't work, please post an error message from Console pane output.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ppkong

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Setting mapping region
« Reply #28 on: October 20, 2014, 09:13:59 AM »
Thanks Alexey!
 I have solved this problem according to your method.But i have a question about buildDenseCloud,My script:
Code: [Select]
chunk.buildDenseCloud(quality=PhotoScan.MediumQuality,filter=PhotoScan.AgressiveFiltering,keep_depth=True,reuse_depth=TrueWhen I set reuse_depth as True,the Python Script can not run,she prompt me "selected 0 cameras from xxx in 0.00 sec" And can't Loading Photos...
I think “reuse_depth” function should be preserve the last available depth, and generated depth images without depth,But it seems not so,The reuse function is only use the last depth map.I think he will greatly save the processing time.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14796
    • View Profile
Re: Setting mapping region
« Reply #29 on: October 20, 2014, 09:17:04 AM »
Hello ppkong,

Reuse depth maps option is only available if depth maps have been estimated earlier and were kept in the project (corresponding option in Advanced tab of PhotoScan Preferences window should be selected).

But this option could not be used properly if the camera positions have changed relatively to the moment when the depth maps have been calculated.
Best regards,
Alexey Pasumansky,
Agisoft LLC