Resizing image in R

前端 未结 5 2443
天命终不由人
天命终不由人 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:03

    The package imager is a nice fit and hides all the details about splines, interpolations and simply stores the images in a 4 dimensional array (the fourth dimension being used in the case of videos)

    library(imager)
    
    im <- load.image(my_file)
    
    thmb <- resize(im,round(width(im)/10),round(height(im)/10))
    
    plot(im)
    plot(thmb,main="Thumbnail")
    

    More informations can be found here: on the official introduction.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-25 15:17

    Inspired by Seily, to resize grey level image.

    resize = function(img, new_width, new_height) {
      new_img = apply(img, 2, function(y){return (spline(y, n = new_height)$y)})
      new_img = t(apply(new_img, 1, function(y){return (spline(y, n = new_width)$y)}))
    
      new_img[new_img < 0] = 0
      new_img = round(new_img)
    
      return (new_img)
    }
    
    0 讨论(0)
  • 2020-12-25 15:21

    Do these options cover what you need:

    library(jpeg)
    
    img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
    
    # Set image size in pixels
    for (i in 3:6) {
      jpeg(paste0("Pixels",i,".jpeg"), width=200*i, height=200*i)
      plot(as.raster(img))
      dev.off()
    }
    
    # Set image size in inches (also need to set resolution in this case)
    for (i in 3:6) {
      jpeg(paste0("Inches",i,".jpeg"), width=i, height=i, unit="in", res=600)
      plot(as.raster(img))
      dev.off()
    }
    

    You can also save in other formats; png, bmp, tiff, pdf. ?jpeg will display help for saving in bitmap formats. ?pdf for help on saving as pdf.

    0 讨论(0)
  • 2020-12-25 15:24

    You can easily accomplish this with the help of the Bioconductor package EBImage, an image processing and analysis toolbox for R. To install the package use:

    source("http://bioconductor.org/biocLite.R")
    biocLite("EBImage")
    

    You can then use the functionality provided by EBImage to load and scale the image, as in the following example.

    library("EBImage")
    
    x <- readImage(system.file("images", "sample-color.png", package="EBImage"))
    
    # width and height of the original image
    dim(x)[1:2]
    
    # scale to a specific width and height
    y <- resize(x, w = 200, h = 100)
    
    # scale by 50%; the height is determined automatically so that
    # the aspect ratio is preserved
    y <- resize(x, dim(x)[1]/2)
    
    # show the scaled image
    display(y)
    
    # extract the pixel array
    z <- imageData(y)
    
    # or
    z <- as.array(y)
    

    For more examples on the functionality provided by EBImage see the the package vignette .

    0 讨论(0)
提交回复
热议问题