DefocusLens
Thin-lens / circle-of-confusion model for fast depth-of-field and bokeh simulation, without full ray tracing. Useful when you need an inexpensive, differentiable defocus approximation.
deeplens.DefocusLens
DefocusLens(foclen, fnum, sensor_size=(8.0, 8.0), sensor_res=(2000, 2000), device=None, dtype=torch.float32)
Bases: Lens
Defocus lens that pre-computes the circle-of-confusion (CoC) PSF.
Rather than ray transfer (ABCD) matrices or thin-lens ray tracing, this model derives the circle of confusion from the focal length, F-number and focus distance, builds the corresponding PSF, and applies it directly. It simulates defocus blur (depth of field) but not higher-order optical aberrations. Useful as a fast baseline renderer, as commonly used in Blender and similar tools.
Attributes:
| Name | Type | Description |
|---|---|---|
foclen |
float
|
Focal length [mm]. |
fnum |
float
|
F-number. |
sensor_size |
tuple
|
Physical sensor size (W, H) [mm]. |
sensor_res |
tuple
|
Pixel resolution (W, H). |
pixel_size |
float
|
Pixel pitch [mm]. |
Initialize a defocus lens.
A defocus lens models geometric defocus via the circle of confusion, which is wavelength-independent, so it takes no wavelength or default object-depth arguments (unlike the other lens classes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
foclen
|
float
|
Focal length in [mm]. |
required |
fnum
|
float
|
F-number. |
required |
sensor_size
|
tuple
|
Physical sensor size as (W, H) in [mm]. Defaults to (8.0, 8.0). |
(8.0, 8.0)
|
sensor_res
|
tuple
|
Sensor resolution as (W, H) in pixels. Defaults to (2000, 2000). |
(2000, 2000)
|
device
|
str
|
Computation device. Defaults to None (auto-select GPU if available, else CPU). |
None
|
dtype
|
dtype
|
Data type for computations. Defaults to torch.float32. |
float32
|
Source code in deeplens-src/deeplens/defocuslens.py
refocus
Refocus the lens to a given object distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
foc_dist
|
float
|
Focus distance in [mm]. Must be less than the focal length (i.e. beyond the focal point). |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If foc_dist >= |
Source code in deeplens-src/deeplens/defocuslens.py
psf
PSF is modeled as a 2D uniform circular disk with diameter CoC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Points of the object. Shape [N, 3] or [3]. |
required |
ks
|
int
|
Kernel size. |
PSF_KS
|
psf_type
|
str
|
PSF type. "gaussian" or "pillbox". |
'gaussian'
|
**kwargs
|
Additional arguments for psf(). Currently not used. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf |
Tensor
|
PSF kernels. Shape [ks, ks] or [N, ks, ks]. |
Source code in deeplens-src/deeplens/defocuslens.py
coc
Calculate circle of confusion (CoC) diameter [mm].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
Tensor
|
Depth of the object. Shape [B]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
coc |
Tensor
|
Circle of confusion diameter [mm]. Shape [B]. |
Reference
[1] https://en.wikipedia.org/wiki/Circle_of_confusion
Source code in deeplens-src/deeplens/defocuslens.py
dof
Calculate depth of field [mm].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
Tensor
|
Depth of the object. Shape [B]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dof |
Tensor
|
Depth of field. Shape [B]. |
Reference
[1] https://en.wikipedia.org/wiki/Depth_of_field
Source code in deeplens-src/deeplens/defocuslens.py
psf_rgb
Compute RGB PSF by replicating the monochrome PSF across three channels.
The defocus model is achromatic, so all channels share the same PSF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Point source positions, shape |
required |
ks
|
int
|
Kernel size. Defaults to |
PSF_KS
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: RGB PSFs, shape |
Source code in deeplens-src/deeplens/defocuslens.py
psf_map
Compute a spatially-uniform monochrome PSF map.
Because the defocus model has no spatially-varying aberrations, every grid position receives the same on-axis PSF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple
|
Grid dimensions |
(5, 5)
|
ks
|
int
|
Kernel size. Defaults to |
PSF_KS
|
depth
|
float
|
Object depth [mm]. When |
None
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: PSF map, shape |
Source code in deeplens-src/deeplens/defocuslens.py
psf_dp
Generate dual-pixel PSF for left and right sub-apertures.
This function generates separate PSFs for left and right sub-apertures of a dual pixel sensor, which enables depth estimation and improved autofocus capabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Input tensor with shape [N, 3], where columns are [x, y, z] coordinates. |
required |
ks
|
int
|
Kernel size for PSF generation. |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(left_psf, right_psf) where each PSF tensor has shape [N, ks, ks]. |
Source code in deeplens-src/deeplens/defocuslens.py
psf_rgb_dp
Compute RGB dual-pixel PSFs for left and right sub-apertures.
Replicates the monochrome dual-pixel PSFs across three colour channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Point source positions, shape |
required |
ks
|
int
|
Kernel size. Defaults to |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
|
Source code in deeplens-src/deeplens/defocuslens.py
psf_map_dp
Compute spatially-uniform dual-pixel PSF maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple
|
Grid dimensions |
(5, 5)
|
ks
|
int
|
Kernel size. Defaults to |
PSF_KS
|
depth
|
float
|
Object depth [mm]. When |
None
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
|
Source code in deeplens-src/deeplens/defocuslens.py
render_rgbd
Occlusion-aware RGBD rendering for defocus lens.
Uses back-to-front layered compositing to prevent color bleeding at depth discontinuities. Since defocus lenses have no spatially varying aberrations, rendering uses a spatially invariant PSF sampled across depth layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_obj
|
tensor
|
Object image. Shape [B, C, H, W]. |
required |
depth_map
|
tensor
|
Depth map [mm]. Shape [B, 1, H, W]. Values should be positive. |
required |
psf_ks
|
int
|
PSF kernel size. Defaults to PSF_KS. |
PSF_KS
|
num_layers
|
int
|
Number of depth layers. Defaults to 16. |
16
|
Returns:
| Name | Type | Description |
|---|---|---|
img_render |
tensor
|
Rendered image. Shape [B, C, H, W]. |
Reference
[1] "Dr.Bokeh: DiffeRentiable Occlusion-aware Bokeh Rendering", CVPR 2024.
Source code in deeplens-src/deeplens/defocuslens.py
render_rgbd_dp
Render RGBD image with dual-pixel PSF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rgb_img
|
tensor
|
[B, 3, H, W] |
required |
depth
|
tensor
|
[B, 1, H, W] |
required |
psf_ks
|
int
|
PSF kernel size. Defaults to PSF_KS. |
PSF_KS
|
num_layers
|
int
|
Number of depth layers. Defaults to 16. |
16
|
Returns:
| Name | Type | Description |
|---|---|---|
img_left |
tensor
|
[B, 3, H, W] |
img_right |
tensor
|
[B, 3, H, W] |