JuliaImages / ImageTransformations.jl

Geometric transformations on images for Julia
Other
46 stars 27 forks source link

Avoid allocations in in-place image resizing #150

Closed pxl-th closed 3 years ago

pxl-th commented 3 years ago

This PR avoids allocations for the BSpline types of interpolations when doing in-place resizing. If I'm not mistaking anything, BSpline interpolations do not modify the original array?

The benefits are considerable. Also maybe we should expose the API?

Code to benchmark:

x = rand(Float64, 2048, 2048);
o = rand(Float64, 256, 256);
@btime ImageTransformations.imresize!(o, x; method=Linear());
johnnychen94 commented 3 years ago

Also maybe we should expose the API?

I'm okay to this. Previously we were concerned about the naming issues because the name imresize happens to be identical to MATLAB. But since the Google vs Oracle java API lawsuit ended I have good reason to believe this won't be an issue at all.

codecov[bot] commented 3 years ago

Codecov Report

Merging #150 (c3eb09e) into master (284bb1e) will increase coverage by 0.13%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #150      +/-   ##
==========================================
+ Coverage   90.14%   90.27%   +0.13%     
==========================================
  Files           8        8              
  Lines         213      216       +3     
==========================================
+ Hits          192      195       +3     
  Misses         21       21              
Impacted Files Coverage Δ
src/ImageTransformations.jl 83.33% <ø> (ø)
src/resizing.jl 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 284bb1e...c3eb09e. Read the comment docs.

pxl-th commented 3 years ago

Great! Can you add some test to ensure that it's working equivalently to imresize?

Added

johnnychen94 commented 3 years ago

Just sent you an invitation in case you want to commit more changes

pxl-th commented 3 years ago

Just sent you an invitation in case you want to commit more changes

Thanks!

timholy commented 3 years ago

If I'm not mistaking anything, BSpline interpolations do not modify the original array?

Not for Constant and Linear, but they do for Quadratic and Cubic. I don't think you can use interpolate! with Cubic, but you can for quadratic, search https://juliamath.github.io/Interpolations.jl/latest/control/ for "destroys".

timholy commented 3 years ago

We might need a fix for the quadratic case pronto.

pxl-th commented 3 years ago

I'll make a PR asap and add tests to ensure the original image is not changed (should've done this in this PR).