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. |
foc_dist |
float
|
Current focus distance [mm], set by |
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], conventionally negative (object in front of the lens). Must be less than the focal length. |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in deeplens-src/deeplens/defocuslens.py
psf
Compute the defocus PSF as a circular disk of diameter CoC.
The PSF is a 2D blur disk whose diameter is the circle of confusion at
each object depth, masked to a circle and normalized to sum to 1. With
psf_type="gaussian" the disk is filled with a Gaussian falloff; with
psf_type="pillbox" it is a flat-top (uniform) disk. The CoC model is
wavelength-independent, so wvln is accepted for API uniformity with
the other lens types but ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Object point positions in [mm], shape [N, 3] or [3]; the depth is taken from the z (third) coordinate. |
required |
wvln
|
float or None
|
Wavelength in [µm]. Ignored (the CoC model is achromatic). Defaults to None. |
None
|
ks
|
int
|
PSF kernel size in pixels. Defaults to PSF_KS. |
PSF_KS
|
**kwargs
|
Model-specific options. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf |
Tensor
|
Normalized PSF kernel(s), shape [ks, ks] for a single point or [N, ks, ks] for N points. |
Source code in deeplens-src/deeplens/defocuslens.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
coc
Compute the circle-of-confusion (CoC) diameter from the paraxial defocus relation.
Depth is clamped to [self.d_far, self.d_close] and taken as an absolute
distance before evaluating the CoC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
Tensor
|
Object depth in [mm], shape [B] (or scalar). Conventionally negative (object in front of the lens). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
coc |
Tensor
|
Circle-of-confusion diameter in [mm], same shape
as |
Reference
[1] https://en.wikipedia.org/wiki/Circle_of_confusion
Source code in deeplens-src/deeplens/defocuslens.py
dof
Compute the depth of field (DoF) at a given object depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
Tensor
|
Object depth in [mm], shape [B] (or scalar). Conventionally negative (object in front of the lens). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dof |
Tensor
|
Depth of field in [mm], same shape as |
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
|
Object point positions in [mm], shape [N, 3]. |
required |
ks
|
int
|
PSF kernel size in pixels. Defaults to PSF_KS. |
PSF_KS
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_rgb |
Tensor
|
RGB PSFs, shape [N, 3, ks, ks]. |
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 (rows, cols). Defaults to (5, 5). |
(5, 5)
|
ks
|
int
|
PSF kernel size in pixels. Defaults to PSF_KS. |
PSF_KS
|
depth
|
float or None
|
Object depth in [mm]. When None
(default), falls back to |
None
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_map |
Tensor
|
PSF map, shape [rows, cols, 1, ks, ks]. |
Source code in deeplens-src/deeplens/defocuslens.py
psf_dp
Generate left/right dual-pixel PSFs by masking the base PSF.
Takes the base defocus PSF and splits the aperture vertically into left and right halves to mimic a dual-pixel sensor. The half assigned to each sub-aperture is swapped depending on whether the object is nearer or farther than the focus distance, reproducing the depth-dependent left/right disparity that enables dual-pixel depth estimation and autofocus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Object point positions in [mm], shape [N, 3] with columns [x, y, z]; depth is taken from z. |
required |
ks
|
int
|
PSF kernel size in pixels. Defaults to PSF_KS. |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_l |
Tensor
|
Left sub-aperture PSFs, shape [N, ks, ks]. |
psf_r |
Tensor
|
Right sub-aperture PSFs, 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
|
Object point positions in [mm], shape [N, 3]. |
required |
ks
|
int
|
PSF kernel size in pixels. Defaults to PSF_KS. |
PSF_KS
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_l |
Tensor
|
Left sub-aperture RGB PSFs, shape [N, 3, ks, ks]. |
psf_r |
Tensor
|
Right sub-aperture RGB PSFs, shape [N, 3, ks, ks]. |
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 (rows, cols). Defaults to (5, 5). |
(5, 5)
|
ks
|
int
|
PSF kernel size in pixels. Defaults to PSF_KS. |
PSF_KS
|
depth
|
float or None
|
Object depth in [mm]. When None
(default), falls back to |
None
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
psf_map_l |
Tensor
|
Left sub-aperture PSF map, shape [rows, cols, 1, ks, ks]. |
psf_map_r |
Tensor
|
Right sub-aperture PSF map, shape [rows, cols, 1, ks, ks]. |
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 in [mm], shape [B, 1, H, W] (or [B, H, W]). Values must be positive. |
required |
psf_ks
|
int
|
PSF kernel size in pixels. 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 left/right dual-pixel images from an RGBD input.
Computes dual-pixel PSFs at uniformly sampled reference depths and convolves the image with depth interpolation to produce the two sub-aperture views. Positive depths are negated internally so the object lies in front of the lens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rgb_img
|
Tensor
|
RGB object image, shape [B, 3, H, W]. |
required |
depth
|
Tensor
|
Depth map in [mm], shape [B, 1, H, W]. |
required |
psf_ks
|
int
|
PSF kernel size in pixels. Defaults to PSF_KS. |
PSF_KS
|
num_layers
|
int
|
Number of depth layers. Defaults to 16. |
16
|
Returns:
| Name | Type | Description |
|---|---|---|
img_left |
Tensor
|
Left sub-aperture image, shape [B, 3, H, W]. |
img_right |
Tensor
|
Right sub-aperture image, shape [B, 3, H, W]. |