Forum

Author Topic: Is it possible to crop area by region?  (Read 1842 times)

photoscan_user

  • Jr. Member
  • **
  • Posts: 98
    • View Profile
Is it possible to crop area by region?
« on: July 13, 2018, 05:47:12 PM »
Is it possible to crop chunk point_cloud by region in GUI or python script?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14846
    • View Profile
Re: Is it possible to crop area by region?
« Reply #1 on: July 13, 2018, 06:37:59 PM »
Hello photoscan_user,

You can use the following script that crops the sparse point cloud by the region in the active chunk:

Code: [Select]
#compatibility PhotoScan Pro 1.4.3

import PhotoScan
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()

PhotoScan.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.point_cloud.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.point_cloud.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 =PhotoScan.app.document

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

dlg = progressDialog(parent)


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