# How to use WarpedView to interpolate a 3D image

**URL:** <https://discourse.julialang.org/t/how-to-use-warpedview-to-interpolate-a-3d-image/123903>\
**Category:** Specific Domains\
**Tags:** question, package, images, interpolations\
**Created:** [December 16, 2024, 11:30pm UTC](https://discourse.julialang.org/t/how-to-use-warpedview-to-interpolate-a-3d-image/123903 "2024-12-16T23:30:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![buffon](https://avatars.discourse-cdn.com/v4/letter/b/e47774/32.png) [@buffon](https://discourse.julialang.org/u/buffon)\
**Post date:** [December 16, 2024, 11:30pm UTC](https://discourse.julialang.org/t/how-to-use-warpedview-to-interpolate-a-3d-image/123903/1 "2024-12-16T23:30:16Z")

</div>

I’m referring to the WarpedView function from ImageTransformations.jl: [ImageTransformations.jl · ImageTransformations](https://juliaimages.org/ImageTransformations.jl/stable/). I can use imresize to interpolate a 3D image naively like: `imresize(image, ratio=(1,1,z_res/xy_res))`, where `z_res/xy_res` is the ratio of the z to the xy resolution. I think WarpedView is the appropriate function to do this operation lazily but I cannot tell from the documentation how to actually do it. Anyone know how to do so? Thanks.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [December 17, 2024, 4:31pm UTC](https://discourse.julialang.org/t/how-to-use-warpedview-to-interpolate-a-3d-image/123903/2 "2024-12-17T16:31:38Z")

</div>

`imresize` defined by the given ratio can be defined as an image warping by the linear transformation of matrix:

```julia
[ 1 0 0 ; 
  0 1 0; 
  0 0 z_res/xy_res]

```

But the image warping is implemented via backward transformation:

```julia
using Images, ImageTransformations, CoordinateTransformations, Interpolations
function invtform(xy_res, z_res) #inverse transformation
    M=[1 0 0 ; 0 1 0; 0 0 xy_res/z_res]
    LinearMap(M)
end 
img = load("image-3d.tif");
xy_res = 120
a, b, c= size(img)
imgw = warpedview(img, invtform(xy_res, c), 
          (1:a, 1:b, 1:round(Int, c^2/xy_res)); method=Lanczos(4)) 

```

---

<div class="post-metadata">

**Author:** ![buffon](https://avatars.discourse-cdn.com/v4/letter/b/e47774/32.png) [@buffon](https://discourse.julialang.org/u/buffon)\
**Post date:** [December 17, 2024, 5:37pm UTC](https://discourse.julialang.org/t/how-to-use-warpedview-to-interpolate-a-3d-image/123903/3 "2024-12-17T17:37:22Z")

</div>

Hi, thanks for the response. I should have clarified: what I meant by xy\_res and z\_res was the physical resolution per pixel in each direction. So, for example, 1 micron as the xy\_res and 4 microns as the z\_res. In your usage, these refer to the height, width, and number of slices of the image.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [December 17, 2024, 5:53pm UTC](https://discourse.julialang.org/t/how-to-use-warpedview-to-interpolate-a-3d-image/123903/4 "2024-12-17T17:53:30Z")

</div>

I interpreted `xy_res` as DPI, and `z_res` as number of slices.You shoud derive the image transformation and its inverse in the case of the right interpretation of the two parameters.

---

<div class="post-metadata">

**Author:** ![buffon](https://avatars.discourse-cdn.com/v4/letter/b/e47774/32.png) [@buffon](https://discourse.julialang.org/u/buffon)\
**Post date:** [December 17, 2024, 6:05pm UTC](https://discourse.julialang.org/t/how-to-use-warpedview-to-interpolate-a-3d-image/123903/5 "2024-12-17T18:05:28Z")

</div>

Okay, got it. Just wondering though – where did `c^2/xy_res` come from as the third index?

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [December 17, 2024, 6:38pm UTC](https://discourse.julialang.org/t/how-to-use-warpedview-to-interpolate-a-3d-image/123903/6 "2024-12-17T18:38:39Z")

</div>

c is the last element in 1:c. This value multiplied by z\_res/xy\_res, and rounded, gives the last value of the third index in the warped image. I stress again, the image transformation/warping is performed by the initial linear map. Its inverse is used only in the implementation of `warpedview`.
