Skip to content

specialize imrotate for 0, 90, 180, 270 degrees#79

Open
johnnychen94 wants to merge 4 commits intoJuliaImages:masterfrom
johnnychen94:jc/rotate
Open

specialize imrotate for 0, 90, 180, 270 degrees#79
johnnychen94 wants to merge 4 commits intoJuliaImages:masterfrom
johnnychen94:jc/rotate

Conversation

@johnnychen94
Copy link
Copy Markdown
Member

@johnnychen94 johnnychen94 commented Dec 14, 2019

img = testimage("cameraman") # (512, 512)

# Before: (514, 514)
# After: (512, 512)
size(imrotate(img, pi/2))

# Before: sizes are not equal
# After: true
imrotate(imrotate(imrotate(imrotate(img, pi/2), pi/2), pi/2), pi/2) == img

# Before: sizes are not equal
# After: true
imrotate(imrotate(img, pi), pi) == img

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 14, 2019

Codecov Report

Merging #79 into master will increase coverage by 0.56%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #79      +/-   ##
==========================================
+ Coverage   88.56%   89.12%   +0.56%     
==========================================
  Files           7        7              
  Lines         271      285      +14     
==========================================
+ Hits          240      254      +14     
  Misses         31       31
Impacted Files Coverage Δ
src/warp.jl 100% <100%> (ø) ⬆️

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 9c18de9...ca777ff. Read the comment docs.

@johnnychen94 johnnychen94 changed the title crop images for special rotation degrees specialize imrotate for 0, 90, 180, 270 degrees Dec 16, 2019
@johnnychen94
Copy link
Copy Markdown
Member Author

I believe this PR is ready for review and merge

@Evizero
Copy link
Copy Markdown
Member

Evizero commented Dec 16, 2019

is it type stable? was it before this PR?

Copy link
Copy Markdown
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good except for concerns about non-inferrability. I suppose an alternative would be to write specialized functions (rotate90, rotate180, rotate270) for specific rotations.

Comment thread test/warp.jl
@test_nowarn imrotate(img, π/4, axes(img))
@test_nowarn imrotate(img, π/4, axes(img), Constant())
@test isequal(channelview(imrotate(img,π/4)), channelview(imrotate(img, π/4, Linear()))) # TODO: if we remove channelview the test will break for Float
@test isequal(channelview(imrotate(img, π/4)), channelview(imrotate(img, π/4, Linear()))) # NaN != NaN
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meaning of this comment is unclear. Do you mean that you wanted to write == but instead have to use isequal?

@johnnychen94
Copy link
Copy Markdown
Member Author

Thanks for the comment, I'm already aware of the type stability issue, I'll come back and try to fix that later when I finished the upgrading of Augmentor.jl made by @Evizero (for my own research interest)

@kimikage
Copy link
Copy Markdown

BTW, 360 ° seems to be missing.

Comment thread src/warp.jl
Comment on lines +130 to +132
# 2. typemax(Int16) is 32767, we choose 32760 to make sure the
# discretization result of pi/2 is exactly pi/2 (or 90°)
max_num_angles = 32760
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the same goes for the current code, why don't you use 32768?

Copy link
Copy Markdown
Member Author

@johnnychen94 johnnychen94 Dec 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... It's like I randomly chose the first number I found that meets the requirement, and I feel this choice doesn't matter for image processing tasks.

Comment thread src/warp.jl
Comment on lines +133 to +134
θ = round(Int, 180*floor(mod(θ, 2pi)/pi*max_num_angles)/max_num_angles)
tform = recenter(RotMatrix{2}(θ/180*pi), center(img))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the θ! 😱

Comment thread src/warp.jl
Comment on lines +137 to +147
elseif θ == 90
idx = StepRange.(axes(img))
perm_img = PermutedDimsArray(img, (2, 1))
return view(perm_img, idx[2], reverse(idx[1]))
elseif θ == 180
idx = map(i->1:1:length(i), axes(img))
return view(img, reverse(idx[1]), reverse(idx[2]))
elseif θ == 270
idx = StepRange.(axes(img))
perm_img = PermutedDimsArray(img, (2, 1))
return view(perm_img, reverse(idx[2]), idx[1])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think @johnnychen94 want to reallocate the memory and relocate the elements.:smile:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, and there won't be type stability issues if we collect them and return an Array; but that would be slower than views.

I don't have a good fix in mind for this at the moment.

@kimikage
Copy link
Copy Markdown

BTW, I think it is better to round the rotation matrix instead of quantizing the angle.

@johnnychen94
Copy link
Copy Markdown
Member Author

That sounds promising to me!

@mkitti
Copy link
Copy Markdown
Member

mkitti commented Dec 21, 2020

It seems a bit dangerous to specialize on an approximation for an irrational number. Maybe it would be better to have a degree based version of imrotate and specialize on multiples of 90 like the title suggests? The discretized imrotate could then map to that.

Otherwise, I would really appreciate something like this. It's absence was duly noted when attempting something AoC2020 Day 20: https://adventofcode.com/2020/day/20

@mkitti
Copy link
Copy Markdown
Member

mkitti commented Dec 25, 2020

sinpi and cospi could also have some relevance here.
https://docs.julialang.org/en/v1/base/math/#Base.Math.sinpi

@timholy
Copy link
Copy Markdown
Member

timholy commented Oct 10, 2021

I'll let @johnnychen94 decide what to do here, but I put all the tests from this PR into #149 and they pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants