-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
imresize
/imrotate
fixed point
#129
Open
ginkulv
wants to merge
7
commits into
JuliaImages:master
Choose a base branch
from
ginkulv:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
515f11b
add imresize with fixed_point
ginkulv 342c9fd
add struct FixedPoint
ginkulv 8c60c63
add examples of imresize with fixed point
ginkulv 1d3fee9
rename FixedPoint to CenterPoint
ginkulv 81ffd49
refactor imresize with FixedPoint
ginkulv cb837d7
add some tests and fix the description for CenterPoint
ginkulv dd9d5c7
add some tests
ginkulv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
CenterPoint(dims) -> cp | ||
|
||
Create a fixed point which can be used in `imresize` and `imrotate` | ||
functions in order to keep the value in this point the same, i.e., | ||
|
||
```jldoctest | ||
img[cp] == imgr[cp] | ||
``` | ||
""" | ||
struct CenterPoint{N} | ||
p::CartesianIndex{N} | ||
end | ||
|
||
CenterPoint(dims::Dims{N}) where N = CenterPoint{N}(CartesianIndex(dims)) | ||
CenterPoint(dims::Int64...) = CenterPoint(Tuple(dims)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ upsample/downsample the image `img` to a given size `sz` or axes `inds` using in | |
The output size is `ceil(Int, size(img).*ratio)`. If `ratio` is larger than `1`, it is | ||
an upsample operation. Otherwise it is a downsample operation. `ratio` can also be a tuple, | ||
in which case `ratio[i]` specifies the resize ratio at dimension `i`. | ||
- `method::InterpolationType`: | ||
- `method::InterpolationType`: | ||
specify the interpolation method used for reconstruction. conveniently, `methold` can | ||
also be a `Degree` type, in which case a `BSpline` object will be created. | ||
For example, `method = Linear()` is equivalent to `method = BSpline(Linear())`. | ||
|
@@ -83,6 +83,10 @@ imresize(img, (1:256, )) # 256*768 | |
imresize(img, ratio = 0.5) #256*384 | ||
imresize(img, ratio = (2, 1)) # 1024*768 | ||
|
||
# pass `FixedPoint` | ||
imresize(img, (256, 384), FixedPoint(10, 15)) # 256*384, img[10,15] == imgr[10,15] | ||
imresize(img, FixedPoint(10, 15), ratio = 0.5) # 256*384, img[10,15] == imgr[10,15] | ||
|
||
# use different interpolation method | ||
imresize(img, (256, 384), method=Linear()) # 256*384 bilinear interpolation | ||
imresize(img, (256, 384), method=Lanczos4OpenCV()) # 256*384 OpenCV-compatible Lanczos 4 interpolation | ||
|
@@ -120,6 +124,23 @@ function imresize(original::AbstractArray{T,N}, new_inds::Indices{N}; kwargs...) | |
end | ||
end | ||
|
||
function imresize(original::AbstractArray{T,N}, new_size::Dims{N}, center_point::CenterPoint{N}; kwargs...) where {T,N} | ||
Tnew = imresize_type(first(original)) | ||
cp = center_point.p | ||
checkbounds(Bool, original, cp) || error("given point $cp is out of range") | ||
topleft = firstindex.(Ref(original), Tuple(1:N)) | ||
offset = cp.I .- new_size .* (cp.I .- topleft) .÷ size(original) .- 1 | ||
resized = OffsetArray(similar(original, Tnew, new_size), offset) | ||
imresize!(resized, original; kwargs...) | ||
resized[cp] = original[cp] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that Looks like there's no fixed point at all 😢 |
||
resized | ||
end | ||
|
||
function imresize(original::AbstractArray{T,N}, center_point::CenterPoint{N}; ratio, kwargs...) where {T,N} | ||
all(ratio .> 0) || throw(ArgumentError("ratio $ratio should be positive")) | ||
new_size = ceil.(Int, size(original) .* ratio) # use ceil to avoid 0 | ||
imresize(original, new_size, center_point; kwargs...) | ||
end | ||
# To choose the output type, rather than forcing everything to | ||
# Float64 by multiplying by 1.0, we exploit the fact that the scale | ||
# changes correspond to integer ratios. We mimic ratio arithmetic | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this
jldoctest
is definitely broken. Usingwould be fine.
The more concerning issue here is that maybe there isn't such a fixed point concept at all. All we're introducing is a warp center point. Looks like it is this property that holds true:
I'm in a bad state here and don't have enough bandwidth to think this carefully. Can you double-check it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I could make such a bad mistake, sorry. I will test it carefully.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't notice 'at all' and got the meaning of your comment wrong.
Honestly, that's what I was thinking about. Not to disturb your state, but can you give any tips of how this idea looks in your head? Othewise I'm afraid I don't understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the back-and-forth and the inaccuracy.
In https://evizero.github.io/Augmentor.jl/dev/operations/#Affine-Transformations, the scale/resize is no different from
imresize
. I was thinking of a generic solution to theZoom
operator there where the center point is invariant. The term "fixed point" showed itself in my head when I wrote down #121.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delayed response, got stuck with exams. Unfortunately, I still can't fully understand your concerns. I made a poor visualization of what it does right now when I create a new image with
imresize(img, FixedPoint(100, 100); ratio=0.5)
.To me it looks like it works as expected and implements the
Zoom
operator, except it doesn't crop the image. Hopefully, my point is clear and makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I'll recheck next weekend (also busy with school stuff). In the meantime, if you can add up some tests when you're available it would be great.