Forum

Author Topic: remove Tie points outside of the bounding box  (Read 2511 times)

Felzigi

  • Newbie
  • *
  • Posts: 7
    • View Profile
remove Tie points outside of the bounding box
« on: August 22, 2023, 02:54:15 PM »
Hi,

can anyone give me the Python Code for the newest Metashape version for removing all Tie Points outside of the Bounding Box??? I would really appreciate it.

Greetz,
Felix

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14753
    • View Profile
Re: remove Tie points outside of the bounding box
« Reply #1 on: August 22, 2023, 03:19:13 PM »
Hello Felix,

Please check, if this code works as intended:

Code: [Select]

import Metashape
from PySide2 import QtCore, QtGui, QtWidgets
from multiprocessing.dummy import Pool as ThreadPool ##

def checkPointTask(point):
global points
checkPoint(point, points)

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

QtWidgets.QDialog.__init__(self, parent)

self.setWindowTitle("Crop sparse cloud by region")
self.btnQuit = QtWidgets.QPushButton("Close")
self.btnQuit.setFixedSize(100,50)
self.btnP1 = QtWidgets.QPushButton("Start")
self.btnP1.setFixedSize(100,50)
self.pBar = QtWidgets.QProgressBar()
self.pBar.setTextVisible(False)
self.pBar.setFixedSize(100, 50)

layout = QtWidgets.QGridLayout()   
layout.addWidget(self.pBar, 0, 0)
layout.addWidget(self.btnP1, 0, 1)
layout.addWidget(self.btnQuit, 0, 2)
self.setLayout(layout)
self.move(500, 400)

proc_markers = lambda : self.process()
QtCore.QObject.connect(self.btnP1, QtCore.SIGNAL("clicked()"), proc_markers)
QtCore.QObject.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))
self.exec()

def isReady(self):
for task in self.tasks:
if (not task.done):
return False
return True

def onReady(self, result):
print("on ready")

self.pBar.setRange(0,1)
self.btnP1.setDisabled(False)
self.btnQuit.setDisabled(False)
app.processEvents()

Metashape.app.update()
print("Script finished.")
#self.close()

def process(self):

global doc, app
global region, points
print("\nScript started...")
chunk = doc.chunk
region = chunk.region
points = chunk.tie_points.points

self.btnP1.setDisabled(True)
self.btnQuit.setDisabled(True)
self.pBar.setRange(0,0)
app.processEvents()

for i in range(len(chunk.frames)):

chunk.frame = i
frame = chunk.frames[i]

result = ThreadPool().map_async(checkPointTask, points, callback = self.onReady)
app.processEvents()
chunk.tie_points.removeSelectedPoints()
app.processEvents()

def checkPoint(point, points):

global region
R = region.rot #Bounding box rotation matrix
C = region.center #Bounding box center vector
size = region.size

v = point.coord
v.size = 3
v_c = v - C
v_r = R.t() * v_c

if abs(v_r.x) > abs(size.x / 2.):
point.selected = True
elif abs(v_r.y) > abs(size.y / 2.):
point.selected = True
elif abs(v_r.z) > abs(size.z / 2.):
point.selected = True
else:
pass

def main():

global doc
doc = Metashape.app.document
global app
app = QtWidgets.QApplication.instance()
parent = app.activeWindow()
dlg = progressDialog(parent)

Metashape.app.addMenuItem("Custom menu/Crop point cloud by bounding box", main)
Best regards,
Alexey Pasumansky,
Agisoft LLC

Felzigi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: remove Tie points outside of the bounding box
« Reply #2 on: August 22, 2023, 05:40:57 PM »
Hi Alexey,

thanks for the quick response. I have tried it and it doesn't do anything. All the TIe Points outside the Bounding Box are still there. At least it doesn't give an Error :-D
Maybe something is missing?

Greetz

Felzigi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: remove Tie points outside of the bounding box
« Reply #3 on: August 23, 2023, 03:32:44 PM »
Alexey,

I have installed PySide2 in hope that the script is working. Multiprocessing comes with Python already, isn't it? So I dont need to install that package.
In Visual Studio Code  in the line "from Pyside2 import QtCore, QtGui, QtWidgets" the Word QtGui is pale and has not the same green color as QtCore and QtWidgets.
Is there the mistake why the script is not working?

Greetz
Felix

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14753
    • View Profile
Re: remove Tie points outside of the bounding box
« Reply #4 on: August 23, 2023, 07:02:41 PM »
Hello Felix,

PySide2 should be available by default in Metashape Pro package.

The script adds custom menu item that you need to click on in order to apply tie points removal.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Felzigi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: remove Tie points outside of the bounding box
« Reply #5 on: August 29, 2023, 05:46:54 PM »
Hi Alexey,

I`ve found the Button :-D it works very well. Thank you very much for that piece of code!


Felzigi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: remove Tie points outside of the bounding box
« Reply #6 on: August 29, 2023, 06:34:56 PM »
One last question:

Is it possible to call this "Custom menu"-Button and the function "Crop Point Clound by bounding box" in a Python Script after it is created by your  code? so I can integrate this into my workflow and keep it automated and dont need to stop there to manually push the button

Greetz,
Felix

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14753
    • View Profile
Re: remove Tie points outside of the bounding box
« Reply #7 on: August 30, 2023, 02:03:34 PM »
Hello Felix,

In order to use it without custom dialog you can remove unnecessary parts from the script:
Code: [Select]
import Metashape
from PySide2 import QtWidgets
from multiprocessing.dummy import Pool as ThreadPool

def checkPoint(point, points):

global region
R = region.rot #Bounding box rotation matrix
C = region.center #Bounding box center vector
size = region.size

v = point.coord
v.size = 3
v_c = v - C
v_r = R.t() * v_c

if abs(v_r.x) > abs(size.x / 2.):
point.selected = True
elif abs(v_r.y) > abs(size.y / 2.):
point.selected = True
elif abs(v_r.z) > abs(size.z / 2.):
point.selected = True
else:
pass

def checkPointTask(point):

global points
checkPoint(point, points)

def process(chunk):

global region, points
print("\nScript started...")
region = chunk.region
points = chunk.tie_points.points

for i in range(len(chunk.frames)):
chunk.frame = i
frame = chunk.frames[i]
result = ThreadPool().map_async(checkPointTask, points)
app.processEvents()
chunk.tie_points.removeSelectedPoints()
print("Points removed.\n")


global app
app = QtWidgets.QApplication.instance()
doc = Metashape.app.document
chunk = doc.chunk
process(chunk)

Best regards,
Alexey Pasumansky,
Agisoft LLC