Emanuele,
for a given image polygon with following vertices:
shape_ver = [(2057.559327869943, 1153.6486906124392),(3552.436575384403, 1461.328355764949),(2435.6456550103676, 2431.3815568427776),
(994.530923104548, 1688.063895881371),(1093.956581442808, 763.8357805011806),(2057.559327869943, 1153.6486906124392)] # test image polygon
the following code:
import numpy
def pnt_in_polygon(x,y,poly):
n = len(poly)
inside = False
p1x,p1y = poly[0]
for i in range(n+1):
p2x,p2y = poly[i % n]
if y > min(p1y,p2y):
if y <= max(p1y,p2y):
if x <= max(p1x,p2x):
if p1y != p2y:
xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
if p1x == p2x or x <= xints:
inside = not inside
p1x,p1y = p2x,p2y
return inside
chunk = Metashape.app.document.chunk
print("Starting script...")
start_time = time.time()
path = 'C:/Users/paul.pelletier/Downloads/FLIR_data/croppedImages/crop'
shape_ver = [(2057.559327869943, 1153.6486906124392),(3552.436575384403, 1461.328355764949),(2435.6456550103676, 2431.3815568427776),
(994.530923104548, 1688.063895881371),(1093.956581442808, 763.8357805011806),(2057.559327869943, 1153.6486906124392)] # test image polygon
xrange = Metashape.Vector([10E10, -10E10])
yrange = Metashape.Vector([10E10, -10E10])
for ver in shape_ver: # find range of image polygon
xrange[0] = min(ver[0], xrange[0])
xrange[1] = max(ver[0], xrange[1])
yrange[0] = min(ver[1], yrange[0])
yrange[1] = max(ver[1], yrange[1])
xmin = math.floor(xrange[0])
ymin = math.floor(yrange[0])
xcrop = math.ceil(xrange[1])-xmin
ycrop = math.ceil(yrange[1])-ymin
camera = chunk.cameras[21] # given camera
if camera.label.rfind('.') > 0:
label = camera.label[0:camera.label.rfind('.')]
else:
label = camera.label
img = camera.image()
if img.data_type == 'U8':
ext ='.jpg'
dtype = numpy.uint8
if img.data_type == 'U16':
dtype = numpy.uint16
ext ='.tif'
if img.data_type == 'F32':
ext ='.tif'
dtype = numpy.float32
start = img.cn*(xmin+ymin*img.width)
arr = numpy.frombuffer(img.tostring(), dtype=dtype)
narr = numpy.array(([0] * xcrop * ycrop * img.cn),dtype=dtype)
for j in range(0,ycrop):
for i in range(0,xcrop*img.cn):
I = int(i/img.cn)
point = Point(xmin+I,ymin+j)
if not pnt_in_polygon(xmin+I,ymin+j,shape_ver):
i = i+img.cn
continue
narr[i+j*xcrop*img.cn] = arr[start+i+j*img.width*img.cn]
cropimg = Metashape.Image()
cropimg = cropimg.fromstring(narr.tobytes(),xcrop,ycrop,channels=img.channels,datatype=img.data_type)
cropimg.save(path+'_'+label+ext)
print("Finished creating cropped image: Crop_" + label+ext +" Elapsed time: " + str(time.time()-start_time))
will export a cropped image to defined polygon of given camera in defined path as following example shows.
The code is not optimized so process can take a few minutes....but it should get you started ,,,,