简体   繁体   English

使用R中的基本图形绘制JPG图像

[英]Plot a JPG image using base graphics in R

I am searching for a simple way to plot a photographic JPEG image on a graphics device in R. 我正在寻找一种在R中的图形设备上绘制照片JPEG图像的简单方法。

For example the following using the raster package appears to ignore the colour attributes in the image. 例如,使用raster包的以下内容似乎忽略了图像中的颜色属性。 I want to reproduce the photograph in its original colours: 我想以原始颜色再现照片:

library(raster)
library(rgdal)

myJPG <- raster("colourfulPic.jpg")
plot(myJPG)  ## Recolours JPEG;

I have discovered that the package rimage has recently been archived and appears to no longer be recommended for use (see here ), if it, indeed, ever did what I need. 我发现包rimage最近已经存档,似乎不再推荐使用(见这里 ),如果它确实做到了我需要的东西。

Similarly the EBImage for BioConductor, which may also possibly work, is not built for 64 bit Windows, and unfortunately I need this architecture. 类似地, EBImage的EBImage(也可能有效)不是为64位Windows构建的,不幸的是我需要这种架构。

Please tell me I missing something very obvious in base graphics? 请告诉我,我错过了基本图形中非常明显的东西?


Here goes an updated solution, that relies only on the jpeg package and handles color and greyscale images ( packages used in other solutions are outdated and won't install with a recent R version ). 这是一个更新的解决方案,它仅依赖于jpeg包并处理颜色和灰度图像( 其他解决方案中使用的包已过时,不会安装最新的R版本 )。

The solution consists in this plot function: 解决方案包括此绘图功能:

plot_jpeg = function(path, add=FALSE)
{
  require('jpeg')
  jpg = readJPEG(path, native=T) # read the file
  res = dim(jpg)[2:1] # get the resolution, [x, y]
  if (!add) # initialize an empty plot area if add==FALSE
    plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]),asp=1,type='n',xaxs='i',yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='',bty='n')
  rasterImage(jpg,1,1,res[1],res[2])
}

This function can then be called with the path of the picture as argument, for example: 然后可以使用图片的路径作为参数调用此函数,例如:

plot_jpeg('~/nyancat.jpg')

To add the picture to an existing plot, use the switch add=TRUE - and be wary of axis limits! 要将图片添加到现有绘图中,请使用开关add=TRUE - 并警惕轴限制!

Using the imager package: 使用imager包:

library(imager)

image <- load.image(image_filename)
plot(image)

This works for a number of image formats including jpeg , gif , etc... 这适用于许多图像格式,包括jpeggif等...

如果要构建RMarkdown文档, knitr::include_graphics()是直接且简单的。

Instead of rimage , perhaps use the package ReadImages : 取而代之的rimage ,或许使用软件包ReadImages

library('ReadImages')
myjpg <- read.jpeg('E:/bigfoot.jpg')
plot(myjpg)

raster Has a built-in function for this called plotRGB raster有一个内置函数,名为plotRGB

library(raster)  
myJPG <- stack("colourfulPic.jpg")  
plotRGB(myJPG)  

Please note that ReadImages is now deprecated. 请注意,现在不推荐使用ReadImages Instead you could install the biOps package , which also contains a rich set of image manipulation methods. 相反,你可以安装biOps包 ,它也包含一组丰富的图像处理方法。 Use as follows: 使用方法如下:

library(jpeg)
library(biOps)

image <- readJPEG("test.jpg")

# image is of type imagedata (the red,green,blue channel images concatenated)

plot(image)

Edit: An object of type imagedata is accessed as image[Y,X,Channel] , btw 编辑: imagedata类型的对象作为image[Y,X,Channel] ,顺便说一句进行访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM