# Subsampling an image by a factor

**URL:** <https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518>\
**Category:** General Usage\
**Created:** [July 20, 2018, 2:46am UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518 "2018-07-20T02:46:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![microlifecc](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@microlifecc](https://discourse.julialang.org/u/microlifecc)\
**Post date:** [July 20, 2018, 2:46am UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/1 "2018-07-20T02:46:48Z")

</div>

Hi!

I am trying to subsample an image only in the vertical direction by a factor t (value is a geometric progression with common ratio sqrt(2)).

Is there any built-in function to subsample images? I was thinking of restrict() but since t is variable and irrational I am not sure if that is a good idea.

Thanks!

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [July 20, 2018, 6:07am UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/2 "2018-07-20T06:07:14Z")

</div>

[Julia Images](https://github.com/JuliaImages/Images.jl)?

---

<div class="post-metadata">

**Author:** ![microlifecc](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@microlifecc](https://discourse.julialang.org/u/microlifecc)\
**Post date:** [July 20, 2018, 6:11am UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/3 "2018-07-20T06:11:03Z")

</div>

Hi PetrKryslUCSD!

Yes, I am using JuliaImages but can’t find a relevant function. So am thinking of converting to a matrix and then the subsampling.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 20, 2018, 8:03am UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/4 "2018-07-20T08:03:56Z")

</div>

Can’t you do it just by indexing into the images directly? Like `img[t, :]`. It’s not clear exactly what `t` _is_, though. Is it a vector, a function or what?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [July 20, 2018, 9:04am UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/5 "2018-07-20T09:04:12Z")

</div>

Then perhaps Interpolations.jl?

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 20, 2018, 9:52am UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/6 "2018-07-20T09:52:59Z")

</div>

[imresize](http://juliaimages.github.io/latest/function_reference.html#ImageTransformations.imresize)?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 20, 2018, 1:24pm UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/7 "2018-07-20T13:24:19Z")

</div>

Can you clarify what you mean by subsampling? To me, that means selecting a subset of the currently available samples. But maybe you are talking about _re_-sampling, using e.g. interpolation or smoothing?

---

<div class="post-metadata">

**Author:** ![microlifecc](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@microlifecc](https://discourse.julialang.org/u/microlifecc)\
**Post date:** [July 20, 2018, 4:03pm UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/8 "2018-07-20T16:03:26Z")

</div>

Hi, t is a variable inside a loop. Initially t=1 and in further iterations of the loop it gets multiplied by sqrt(2).

I can try indexing into the image. The issue is my code is already at O(n^3) complexity. I was wondering if there is already an inbuilt function which won’t affect the complexity too much.

I mean subsampling=downsampling, i.e. taking the **t** th rows of pixels and ignoring the intermediate ones in case of vertical downsampling.

Thanks!

---

<div class="post-metadata">

**Author:** ![microlifecc](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@microlifecc](https://discourse.julialang.org/u/microlifecc)\
**Post date:** [July 20, 2018, 4:04pm UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/9 "2018-07-20T16:04:32Z")

</div>

Thanks PetrKryslUCSD!

I am checking that.

---

<div class="post-metadata">

**Author:** ![microlifecc](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@microlifecc](https://discourse.julialang.org/u/microlifecc)\
**Post date:** [July 20, 2018, 4:10pm UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/10 "2018-07-20T16:10:21Z")

</div>

Hi yakir12!

I was actually thinking the same. But have a confusion. For eg., in Matlab the image resize function uses weights on the surrounding pixels while resizing the image. But according to the paper I am trying to implement, I just need to skip the intermediate rows of pixels and consider only those rows which are multiples of t, and there are no weight involved. So I am not sure if Julia uses weights while discaring pixels to resize the image.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [July 20, 2018, 10:11pm UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/11 "2018-07-20T22:11:54Z")

</div>

`imresize` interpolates between pixels when downscaling. You might just want to index into the image?

`sqrt_restrict(img, i) = img[1:round(Int,sqrt(2)^i):end, :]`

---

<div class="post-metadata">

**Author:** ![microlifecc](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@microlifecc](https://discourse.julialang.org/u/microlifecc)\
**Post date:** [July 20, 2018, 10:13pm UTC](https://discourse.julialang.org/t/subsampling-an-image-by-a-factor/12518/12 "2018-07-20T22:13:59Z")

</div>

@stillyslalom

Thanks a lot!
