-
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
Refactor imresize using Interpolations #7
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,10 @@ function restrict{T,N}(A::AbstractArray{T,N}, dim::Integer) | |
return A | ||
end | ||
newsz = ntuple(i->i==dim?restrict_size(size(A,dim)):size(A,i), Val{N}) | ||
out = Array{typeof(A[1]/4+A[2]/2),N}(newsz) | ||
# FIXME: The following line fails for interpolations because | ||
# interpolations can be accessed linearily A[i]. | ||
# out = Array{typeof(A[1]/4+A[2]/2),N}(newsz) | ||
out = Array{typeof(first(A)/4+first(A)/2),N}(newsz) | ||
restrict!(out, A, dim) | ||
out | ||
end | ||
|
@@ -78,7 +81,7 @@ for N = 1:5 | |
oneeighth = convert(eltype(T), 0.125) | ||
indx = 0 | ||
if dim == 1 | ||
z = convert(T, zero(A[1])) | ||
z = convert(T, zero(first(A))) | ||
@inbounds @nloops $N i d->(d==1 ? (1:1) : (1:size(A,d))) d->(j_d = i_d) begin | ||
c = d = z | ||
for k = 1:size(out,1)-1 | ||
|
@@ -125,32 +128,30 @@ end | |
|
||
restrict_size(len::Integer) = isodd(len) ? (len+1)>>1 : (len>>1)+1 | ||
|
||
## imresize | ||
function imresize!(resized, original::AbstractMatrix) | ||
scale1 = (size(original,1)-1)/(size(resized,1)-0.999f0) | ||
scale2 = (size(original,2)-1)/(size(resized,2)-0.999f0) | ||
for jr = 0:size(resized,2)-1 | ||
jo = scale2*jr | ||
ijo = trunc(Int, jo) | ||
fjo = jo - oftype(jo, ijo) | ||
@inbounds for ir = 0:size(resized,1)-1 | ||
io = scale1*ir | ||
iio = trunc(Int, io) | ||
fio = io - oftype(io, iio) | ||
tmp = (1-fio)*((1-fjo)*original[iio+1,ijo+1] + | ||
fjo*original[iio+1,ijo+2]) + | ||
+ fio*((1-fjo)*original[iio+2,ijo+1] + | ||
fjo*original[iio+2,ijo+2]) | ||
resized[ir+1,jr+1] = convertsafely(eltype(resized), tmp) | ||
end | ||
end | ||
resized | ||
# imresize | ||
imresize(original::AbstractArray, dim1, dimN...) = imresize(original, (dim1,dimN...)) | ||
|
||
function imresize{T,N}(original::AbstractArray{T,N}, short_size::NTuple) | ||
len_short = length(short_size) | ||
new_size = ntuple(i -> (i > len_short ? size(original,i) : short_size[i]), N) | ||
imresize(original, new_size) | ||
end | ||
|
||
imresize(original, new_size) = size(original) == new_size ? copy!(similar(original), original) : imresize!(similar(original, new_size), original) | ||
function imresize{T,N}(original::AbstractArray{T,N}, new_size::NTuple{N}) | ||
if size(original) == new_size | ||
copy!(similar(original), original) | ||
else | ||
imresize!(similar(original, new_size), original) | ||
end | ||
end | ||
|
||
convertsafely{T<:AbstractFloat}(::Type{T}, val) = convert(T, val) | ||
convertsafely{T<:Integer}(::Type{T}, val::Integer) = convert(T, val) | ||
convertsafely{T<:Integer}(::Type{T}, val::AbstractFloat) = round(T, val) | ||
convertsafely{T}(::Type{T}, val) = convert(T, val) | ||
function imresize!{T,S,N}(resized::AbstractArray{T,N}, original::AbstractArray{S,N}) | ||
itp = interpolate(original, BSpline(Linear()), OnGrid()) | ||
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. Might want to dispatch to a version of 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. Good idea |
||
sf = map(/, size(original), size(resized)) | ||
for I in CartesianRange(size(resized)) | ||
I_o = map(*, I.I, sf) | ||
resized[I] = itp[I_o...] | ||
end | ||
resized | ||
end | ||
|
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.
Interesting. I haven't coded with Interpolations recently, but if it supports what we need then
should work. Though perhaps the better strategy would to add support for
first
to Interpolations.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.
first(A)
works for interpolations.A[1]
does not. For extrapolations neither work