Resizing image in R

前端 未结 5 2550
天命终不由人
天命终不由人 2020-12-25 14:56

I am trying to work with some image data in R, and cannot figure out how to resize the images I have to ensure they are all the same size.

In Python, I approached th

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-25 15:04

    I use the following code to resample matrices. If you have a jpeg object, you might do that for each color channel individual.

    The strategy is as follows:

    Given a matrix m with the dimensions a and b and the new dimensions a.new and b.new

    1. Define your new grid
    x.new <- seq(1,a,length.out=a.new)
    y.new <- seq(1,a,length.out=b.new)
    
    1. resample the original matrix twice in x and in y direction
    V <- apply(V,2,FUN=function(y,x,xout) return(spline(x,y,xout=xout)$y),x,x.new)
    V <- t(apply(V,1,FUN=function(y,x,xout) return(spline(x,y,xout=xout)$y),d,y.new))
    

    Here I choose the spline-interpolation but you can also use a linear one with apporx(). You will gain additionally an x- and y-axis for plotting with the image(x = x.new, y = y.new, z = V) function.

    Best.

提交回复
热议问题