Forum

Author Topic: Script for exporting orthophotos using markers  (Read 46785 times)

tonio

  • Guest
Re: Script for exporting orthophotos using markers
« Reply #45 on: November 21, 2016, 05:52:15 PM »
Hi, i just updated to 1.2.5 and I got the same problem i had with the script.

I try to use it but nothing happens...:(

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15122
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #46 on: November 21, 2016, 06:40:14 PM »
Hello tonio,

Running the script should add a custom menu item - the actual process should be started from there.

If any errors appear in the Console pane, please provide the additional information.
Best regards,
Alexey Pasumansky,
Agisoft LLC

_mARCel_

  • Newbie
  • *
  • Posts: 22
  • I'm an archaeologist from Germany
    • View Profile
    • ArchaeoBW GmbH Home
Re: Script for exporting orthophotos using markers
« Reply #47 on: November 22, 2016, 01:08:01 PM »
Hi tonio,
the script needs markers to start corretly. So, please open a psx-project with already set markers before using the script. If you don't have any markers in your project, nothing happens. Sorry for my English...

Best regards,
Marcel

Dukytony

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #48 on: November 23, 2016, 12:40:02 PM »
So i got this psx (1.2.5) project with 3 markers already set.

i enter my export resolution (0.000636325)

I choose my markers

I go to export and select the folder of my project and then nothing happens and I go back to the opex box

the console shows only this

2016-11-23 10:35:36 Skript startet...
2016-11-23 10:35:36 Skript rum.Viele Grube MP
2016-11-23 10:35:36
>>>


Thanks a lot for your help!

_mARCel_

  • Newbie
  • *
  • Posts: 22
  • I'm an archaeologist from Germany
    • View Profile
    • ArchaeoBW GmbH Home
Re: Script for exporting orthophotos using markers
« Reply #49 on: November 23, 2016, 02:34:35 PM »
Hi,
i think the problem is, that "my" script needs 6 Markers...so try this one...
Code: [Select]
# Compatibility - PhotoScan Professional 1.2.6
# exports orthophoto in planar projection defined by vector (from three markers) and Z axes.
# script version 0.9
# written by Alexey Pasumansky, AgiSoft LLC
# expanded by Marcel C. Hagner for ArchaeoBW 2016
import os
import PhotoScan
from PySide import QtCore, QtGui

def getMarker(chunk, label):

for marker in chunk.markers:
if marker.label.upper() == label.upper():
return marker
return 0

def vect(a, b):

result = PhotoScan.Vector([a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y *b.x])
return result.normalized()

class ExportDlg(QtGui.QDialog):

def __init__ (self, parent):
QtGui.QDialog.__init__(self, parent)

chunk = doc.chunk
self.supported_types = ["jpg", "tif", "png"]
self.blending = PhotoScan.BlendingMode.MosaicBlending


self.setWindowTitle("OPEX")

self.btnQuit = QtGui.QPushButton("&Exit")
self.btnQuit.setFixedSize(150,50)

self.btnP1 = QtGui.QPushButton("&Export")
self.btnP1.setFixedSize(150,50)

self.resTxt = QtGui.QLabel()
self.resTxt.setText("Export resolution (m):")
self.resTxt.setFixedSize(150, 25)

self.vectTxt = QtGui.QLabel()
self.vectTxt.setText("Horizontal vector:")
self.vectTxt.setFixedSize(150, 25)

self.Btn_cc = QtGui.QCheckBox("Color Correction")
self.Btn_cc.setChecked(True)

self.vect2Txt = QtGui.QLabel()
self.vect2Txt.setText("Vertical vector:")
self.vect2Txt.setFixedSize(150, 25)

self.resEdt = QtGui.QLineEdit()
self.resEdt.setText("0.00025")
self.resEdt.setFixedSize(150, 25)

self.radioBtn_v = QtGui.QRadioButton("Vertical")
self.radioBtn_h = QtGui.QRadioButton("Horizontal")
self.radioBtn_v.setChecked(True)
self.radioBtn_h.setChecked(False)

self.llistV = [None, None]

self.llistV[0] = QtGui.QComboBox()
self.llistV[0].resize(150, 30)
self.llistV[1] = QtGui.QComboBox()
self.llistV[1].resize(150, 30)

self.llistH = [None, None]

self.llistH[0] = QtGui.QComboBox()
self.llistH[0].resize(150, 30)
self.llistH[1] = QtGui.QComboBox()
self.llistH[1].resize(150, 30)


self.radio = QtGui.QVBoxLayout()
self.radio.addWidget(self.radioBtn_v)
self.radio.addWidget(self.radioBtn_h)

for marker in chunk.markers:
for ilist in self.llistV:
ilist.addItem(marker.label)
for ilist in self.llistH:
ilist.addItem(marker.label)


layout = QtGui.QGridLayout()   #creating layout
layout.setSpacing(10)
layout.addWidget(self.resTxt, 0, 0)
layout.addWidget(self.resEdt, 0, 1)

layout.addWidget(self.vectTxt, 1, 0)
layout.addWidget(self.vect2Txt, 2, 0)
layout.addWidget(self.llistH[0], 1, 1)
layout.addWidget(self.llistH[1], 1, 2)
layout.addWidget(self.llistV[0], 2, 1)
layout.addWidget(self.llistV[1], 2, 2)
layout.addWidget(self.Btn_cc, 0, 2)
layout.addLayout(self.radio, 3, 0)

layout.addWidget(self.btnP1, 3, 1)
layout.addWidget(self.btnQuit, 3, 2)
self.setLayout(layout) 

proc_export = lambda : self.procExport()

QtCore.QObject.connect(self.btnP1, QtCore.SIGNAL("clicked()"), proc_export)
QtCore.QObject.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))

self.exec()

def procExport(self):

chunk = doc.chunk

path = PhotoScan.app.getExistingDirectory("Bitte wählen Sie den Ordner mit den gesammelten Geo-Cut-Modell aus")
filelist = os.listdir(path)
doclist = list()
for file in filelist:
if file[-3:].upper() == "PSX":
doclist.append(path + "\\" + file)


try:
d_x = d_y = float(self.resEdt.text())
except(ValueError):
PhotoScan.app.messageBox("Incorrect export resolution! Please use point delimiter.\n")
print("Script aborted.")
return 0

if self.llistV[0].currentIndex() == self.llistV[1].currentIndex():
PhotoScan.app.messageBox("Can't use the same marker for vector start and end.\n")
print("Script aborted.")
return 0

if self.llistH[0].currentIndex() == self.llistH[1].currentIndex():
PhotoScan.app.messageBox("Can't use the same marker for vector start and end.\n")
print("Script aborted.")
return 0

if len(chunk.markers) < 2:
PhotoScan.app.messageBox("No markers.\n")
print("Script aborted.")
return 0

print("Skript startet...")

type = "tif"

for doc_path in doclist:
doc.open(doc_path)
chunk = doc.chunk
T = chunk.transform.matrix

path = doc_path[:-3] + type


self.btnP1.setDisabled(True)
self.btnQuit.setDisabled(True)

m1 = getMarker(chunk, self.llistH[0].currentText())
m2 = getMarker(chunk, self.llistH[1].currentText())

horizontal = m2.position - m1.position
horizontal = T.mulv(horizontal)

m1 = getMarker(chunk, self.llistV[0].currentText())
m2 = getMarker(chunk, self.llistV[1].currentText())

vertical = m2.position - m1.position
vertical = T.mulv(vertical)

normal = vect(vertical, horizontal)

if self.radioBtn_h.isChecked():
vertical = vect(horizontal, normal)
horizontal = horizontal.normalized()

elif self.radioBtn_v.isChecked():
vertical = vertical.normalized()
horizontal = -vect(vertical, normal)
#horizontal, vertical = -vertical, -horizontal

else:
PhotoScan.app.messageBox("Fehler!Kann das OF nicht exportieren" + path)
print("Skript wird abgebrochen.")
continue

R = PhotoScan.Matrix ([horizontal, vertical, -normal])
origin = T.mulp(chunk.markers[0].position)
X = (-1) * R * origin

horizontal.size = 4
horizontal.w = X.x
vertical.size = 4
vertical.w = X.y
normal.size = 4
normal.w = -X.z

proj = PhotoScan.Matrix ([horizontal, vertical, -normal, PhotoScan.Vector([0,0,0,1])])

if self.Btn_cc.isChecked():
color_correction = color_correction=(True)

elif not self.Btn_cc.isChecked():
color_correction = color_correction=(False)

if doc.path[-3:].lower() != "psx":  #saving in PSX required to build orthomosaic
doc.save(doc.path[:-3] + "psx")
chunk = doc.chunk

chunk.buildOrthomosaic(surface = PhotoScan.DataSource.ModelData, blending = self.blending, color_correction = color_correction, projection = proj, dx = d_x, dy = d_y)
doc.save()
chunk.exportOrthomosaic(path, dx = d_x, dy = d_y)

self.btnP1.setDisabled(False)
self.btnQuit.setDisabled(False)
PhotoScan.app.update()
print("Orthofoto wird exportiert nach to:\n" + path)

print("Skript rum.Viele Grüße MP")
print()
return 1



def main():

global doc
doc = PhotoScan.app.document

app = QtGui.QApplication.instance()
parent = app.activeWindow()

dlg = ExportDlg(parent)

PhotoScan.app.addMenuItem("OPEX(Mosaic)/OPEX for 1.2.6", main)

Your model is georeferenced?
Did you create the orthomosaic for the psx?

Best regards,
Marcel

Alterco

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #50 on: May 18, 2017, 12:43:58 PM »
Hi,

I try to use the 1.3 version of the script with the 1.3.1 version of photoscan but it seems that it need to be updated.

Quote
Traceback (most recent call last):
File "E:/MarkerOrtho-1.3.py", line 7, in <module>
from PySide import QtCore, QtGui
File "C:\Program Files\Agisoft\PhotoScan Pro\python\lib\site-packages\qtconsole\qt_loaders.py", line 56, in load_module
    """ % (fullname, loaded_api()))
ImportError:
    Importing PySide disabled by QtConsole, which has
    already imported an incompatible Qt Binding: pyside2


Could anyone help me ?

Thank you in advance

AA

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15122
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #51 on: May 18, 2017, 01:02:33 PM »
Hello Alterco,

1.3 -is the script version. In the script that you have provided the compatibility is for PhotoScan Pro version 1.1.6.

Can you try this one:

Code: [Select]
# Compatibility - PhotoScan Professional 1.3.2
# exports orthophoto in planar projection defined by vector (from three markers) and Z axes.
# script version 0.9
# written by Alexey Pasumansky, AgiSoft LLC
# expanded by Marcel C. Hagner for ArchaeoBW 2016


import os
import PhotoScan
from PySide2 import QtCore, QtGui, QtWidgets

def getMarker(chunk, label):

for marker in chunk.markers:
if marker.label.upper() == label.upper():
return marker
return 0

def vect(a, b):

result = PhotoScan.Vector([a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y *b.x])
return result.normalized()

class ExportDlg(QtWidgets.QDialog):

def __init__ (self, parent):
QtWidgets.QDialog.__init__(self, parent)

chunk = doc.chunk
self.supported_types = ["jpg", "tif", "png"]
self.blending = PhotoScan.BlendingMode.MosaicBlending


self.setWindowTitle("OPEX")

self.btnQuit = QtWidgets.QPushButton("&Exit")
self.btnQuit.setFixedSize(150,50)

self.btnP1 = QtWidgets.QPushButton("&Export")
self.btnP1.setFixedSize(150,50)

self.resTxt = QtWidgets.QLabel()
self.resTxt.setText("Export resolution (m):")
self.resTxt.setFixedSize(150, 25)

self.vectTxt = QtWidgets.QLabel()
self.vectTxt.setText("Horizontal vector:")
self.vectTxt.setFixedSize(150, 25)

self.Btn_cc = QtWidgets.QCheckBox("Color Correction")
self.Btn_cc.setChecked(True)

self.vect2Txt = QtWidgets.QLabel()
self.vect2Txt.setText("Vertical vector:")
self.vect2Txt.setFixedSize(150, 25)

self.resEdt = QtWidgets.QLineEdit()
self.resEdt.setText("0.00025")
self.resEdt.setFixedSize(150, 25)

self.radioBtn_v = QtWidgets.QRadioButton("Vertical")
self.radioBtn_h = QtWidgets.QRadioButton("Horizontal")
self.radioBtn_v.setChecked(True)
self.radioBtn_h.setChecked(False)

self.llistV = [None, None]

self.llistV[0] = QtWidgets.QComboBox()
self.llistV[0].resize(150, 30)
self.llistV[1] = QtWidgets.QComboBox()
self.llistV[1].resize(150, 30)

self.llistH = [None, None]

self.llistH[0] = QtWidgets.QComboBox()
self.llistH[0].resize(150, 30)
self.llistH[1] = QtWidgets.QComboBox()
self.llistH[1].resize(150, 30)


self.radio = QtWidgets.QVBoxLayout()
self.radio.addWidget(self.radioBtn_v)
self.radio.addWidget(self.radioBtn_h)

for marker in chunk.markers:
for ilist in self.llistV:
ilist.addItem(marker.label)
for ilist in self.llistH:
ilist.addItem(marker.label)


layout = QtWidgets.QGridLayout()   #creating layout
layout.setSpacing(10)
layout.addWidget(self.resTxt, 0, 0)
layout.addWidget(self.resEdt, 0, 1)

layout.addWidget(self.vectTxt, 1, 0)
layout.addWidget(self.vect2Txt, 2, 0)
layout.addWidget(self.llistH[0], 1, 1)
layout.addWidget(self.llistH[1], 1, 2)
layout.addWidget(self.llistV[0], 2, 1)
layout.addWidget(self.llistV[1], 2, 2)
layout.addWidget(self.Btn_cc, 0, 2)
layout.addLayout(self.radio, 3, 0)

layout.addWidget(self.btnP1, 3, 1)
layout.addWidget(self.btnQuit, 3, 2)
self.setLayout(layout) 

proc_export = lambda : self.procExport()

QtCore.QObject.connect(self.btnP1, QtCore.SIGNAL("clicked()"), proc_export)
QtCore.QObject.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))

self.exec()

def procExport(self):

chunk = doc.chunk

path = PhotoScan.app.getExistingDirectory("Bitte wählen Sie den Ordner mit den gesammelten Geo-Cut-Modell aus")
filelist = os.listdir(path)
doclist = list()
for file in filelist:
if file[-3:].upper() == "PSX":
doclist.append(path + "\\" + file)


try:
d_x = d_y = float(self.resEdt.text())
except(ValueError):
PhotoScan.app.messageBox("Incorrect export resolution! Please use point delimiter.\n")
print("Script aborted.")
return 0

if self.llistV[0].currentIndex() == self.llistV[1].currentIndex():
PhotoScan.app.messageBox("Can't use the same marker for vector start and end.\n")
print("Script aborted.")
return 0

if self.llistH[0].currentIndex() == self.llistH[1].currentIndex():
PhotoScan.app.messageBox("Can't use the same marker for vector start and end.\n")
print("Script aborted.")
return 0

if len(chunk.markers) < 2:
PhotoScan.app.messageBox("No markers.\n")
print("Script aborted.")
return 0

print("Skript startet...")

type = "tif"

for doc_path in doclist:
doc.open(doc_path)
chunk = doc.chunk
T = chunk.transform.matrix

path = doc_path[:-3] + type


self.btnP1.setDisabled(True)
self.btnQuit.setDisabled(True)

m1 = getMarker(chunk, self.llistH[0].currentText())
m2 = getMarker(chunk, self.llistH[1].currentText())

horizontal = m2.position - m1.position
horizontal = T.mulv(horizontal)

m1 = getMarker(chunk, self.llistV[0].currentText())
m2 = getMarker(chunk, self.llistV[1].currentText())

vertical = m2.position - m1.position
vertical = T.mulv(vertical)

normal = vect(vertical, horizontal)

if self.radioBtn_h.isChecked():
vertical = vect(horizontal, normal)
horizontal = horizontal.normalized()

elif self.radioBtn_v.isChecked():
vertical = vertical.normalized()
horizontal = -vect(vertical, normal)
#horizontal, vertical = -vertical, -horizontal

else:
PhotoScan.app.messageBox("Fehler!Kann das OF nicht exportieren" + path)
print("Skript wird abgebrochen.")
continue

R = PhotoScan.Matrix ([horizontal, vertical, -normal])
origin = T.mulp(chunk.markers[0].position)
X = (-1) * R * origin

horizontal.size = 4
horizontal.w = X.x
vertical.size = 4
vertical.w = X.y
normal.size = 4
normal.w = -X.z

proj = PhotoScan.Matrix ([horizontal, vertical, -normal, PhotoScan.Vector([0,0,0,1])])

if self.Btn_cc.isChecked():
color_correction = color_correction=(True)

elif not self.Btn_cc.isChecked():
color_correction = color_correction=(False)

if doc.path[-3:].lower() != "psx":  #saving in PSX required to build orthomosaic
doc.save(doc.path[:-3] + "psx")
chunk = doc.chunk

chunk.buildOrthomosaic(surface = PhotoScan.DataSource.ModelData, blending = self.blending, color_correction = color_correction, projection = proj, dx = d_x, dy = d_y)
doc.save()
chunk.exportOrthomosaic(path, dx = d_x, dy = d_y)

self.btnP1.setDisabled(False)
self.btnQuit.setDisabled(False)
PhotoScan.app.update()
print("Orthofoto wird exportiert nach to:\n" + path)

print("Skript rum.Viele Grüße MP")
print()
return 1



def main():

global doc
doc = PhotoScan.app.document

app = QtWidgets.QApplication.instance()
parent = app.activeWindow()

dlg = ExportDlg(parent)

PhotoScan.app.addMenuItem("OPEX(Mosaic)/OPEX for 1.3.2", main)
« Last Edit: May 18, 2017, 01:10:32 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

Alterco

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #52 on: May 18, 2017, 01:07:31 PM »

Thank you for your answer.

Is there a way to adapt it to the 1.3.1 Photoscan version ?

AA

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15122
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #53 on: May 18, 2017, 01:21:16 PM »
I've posted the modified version above, let me know if there are any error messages.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Alterco

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #54 on: May 18, 2017, 01:32:35 PM »
There is still a problem but different :
Quote
File "E:/V0.9.py", line 121 SyntaxError: Non-UTF-8 code starting with '\xe4' in file E:/V0.9.py on line 121, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details


AA

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15122
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #55 on: May 18, 2017, 01:43:26 PM »
Hello Alterco,

I can suggest either to remove all "special" characters from the message strings, like "ß", for example, or be sure that you are saving the file in UTF-8 encoding.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Alterco

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #56 on: May 18, 2017, 01:50:28 PM »
Thank you very much.

I wasen't indeed  in UTF-8 encoding and I also removed the german special caracters.

It seems working now.

Thanks aggain.


AA

Alterco

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #57 on: May 18, 2017, 03:55:16 PM »
Ok, everything seems working.

But I thought that I could batch the export for several chunks running the script in the bacth tool.

Considering that the names of the projection markers will be the same (name, not coodinates), It could be possible to run automatically the script for each chunk with an exported file, named after the chunk name.

For now, the loading configuration window prevent the use of the script with the batch tool.

Anybody have some ideas to ease the process ?

Thank you in advance.

AA

Dukytony

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #58 on: April 19, 2018, 08:00:09 PM »
HI,

I got the same problem again on 1.3.3.

Console shows this :

2018-04-19 11:56:26 Skript startet...
2018-04-19 11:56:26 Skript rum.Viele Grüße MP
2018-04-19 11:56:26

and nothing happens...

Thanks a lot for your help!

Dukytony

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Script for exporting orthophotos using markers
« Reply #59 on: July 15, 2018, 03:30:49 PM »
Hello!

could you please update this script? I'm running 1.4.1 and it does not work anymore.

Thanks a lot!