Home

Awesome

[# Moving Least Squares (MLS) (Numpy & PyTorch)

Introduction

Moving least squares is a method of reconstructing continuous functions from a set of unorganized point samples via the calculation of a weighted least squares measure biased towards the region around the point at which the reconstructed value is requested.

In computer graphics, the moving least squares method is useful for reconstructing a surface from a set of points. Often it is used to create a 3D surface from a point cloud through either downsampling or upsampling.

Methods

Usage

1. Install Packages

pip install -r requirements.txt

The accelerated algorithms requires PyTorch. PyTorch Installation Guide

2. Try the demo

Please check the demo.py for usage. We provide four demos:

demo()          # Toy
demo2()         # Monalisa
demo3()         # Cells
demo_torch()    # Toy in PyTorch

NEW 2023-04-28: @spedr provides an interactive demo. (See interactive_demo.py)

You can run the demo with

python interactive_demo.py images/monalisa.jpg

Hotkeys:
q or ESC - Quit
d - Delete the selected control point
c - Clear all control points
a - Create an affine deformation and display it in a separate window
s - Create a similarity deformation and display it in a separate window
r - Create a rigid deformation and display it in a separate window
w - Write the last deformation to the images folder

Here's an usage example of performing a rigid deformation on Monalisa's smile.

https://user-images.githubusercontent.com/22013744/231604569-c747ce8b-e074-4765-88ea-942fc3c60e8b.mp4

Results

Deformation

Rigid deformation

Rigid Deformation

The original label is overlapped on the deformed labels for better comparison.

Rigid Deformation

Code list

Metrics

Optimize memory usage

Image SizeControl PointsAffineSimilarityRigid
500 x 500160.57s / 0.15GB0.99s / 0.16GB0.89s / 0.13GB
500 x 500641.6s / 0.34GB3.7s / 0.3GB3.6s / 0.2GB
1000 x 1000647.7s / 1.1GB17s / 0.98GB15s / 0.82GB
2000 x 20006430s / 4.2GB65s / 3.6GB69s / 3.1GB

Accelerated by PyTorch

The algorithm is also implemented with PyTorch and has faster speed benefiting from the CUDA acceleration.

Image SizeControl PointsNumpyPyTorch with CUDA
100 x 100160.025s0.128s
500 x 500160.753s0.187s
500 x 500321.934s0.205s
500 x 500643.384s0.483s
1000 x 10006413.089s0.663s
2000 x 20006461.874s1.784s

(* Tested on pytorch=1.6.0 with cudatoolkit=10.1)

Update

Reference

[1] Schaefer S, Mcphail T, Warren J. Image deformation using moving least squares[C]// ACM SIGGRAPH. ACM, 2006:533-540.

[2] interp implementation in interp_torch.py. Github: aliutkus/torchinterp1d ](https://github.com/spedr)