R语言之读取图片的方式总结

本文总结了R语言中读取png和jpg格式图片的方法,包括使用png包的readPNG()函数及imager包的load.image()函数,结合rasterImage()和plot()展示图像。
摘要由CSDN通过智能技术生成

R语言之读取图片的方式总结


最近遇到的,加上之前使用的一些技巧,觉得还是有必要记录一下,便于后续查找~

读取png格式的图片

通过png包中的readPNG()函数读取png格式的图片,然后使用rasterImage()函数显示图像。

library(png)
imgpng <- readPNG("E:/data/1/1_dog.png")
r <- nrow(imgpng)/ncol(imgpng) 
plot(c(0,1),c(0,r),type = "n",xlab = "",ylab = "",asp=1)
rasterImage(imgpng,0,0,1,r)

生成结果如下:
在这里插入图片描述

读取jpg格式图片

使用imager包中的load.image()函数读取jpg格式图像,然后使用plot()函数将图像进行可视化。

library(imager)
imgjpg <- load.image("E:/data/1/2_dog.jpg")
#imdim <- dim(imgjpg)
plot(imgjpg,xlim = c(1,width(imgjpg)),ylim = c(height(imgjpg),1)) #ylim的值前后变换可颠倒图片

生成结果如下:
在这里插入图片描述
后续还会持续总结更新~

  • 6
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
R语言中,可以使用不同的包来读取JPG图像。其中,可以使用png包中的readPNG()函数来读取PNG格式的图像,然后使用rasterImage()函数来显示图像。另外,也可以使用imager包中的load.image()函数来读取JPG格式的图像,然后使用plot()函数将图像进行可视化。以下是两种方法的示例代码: 方法一:使用png包 ```R library(png) imgpng <- readPNG("path/to/image.png") r <- nrow(imgpng)/ncol(imgpng) plot(c(0,1), c(0,r), type = "n", xlab = "", ylab = "", asp = 1) rasterImage(imgpng, 0, 0, 1, r) ``` 方法二:使用imager包 ```R library(imager) imgjpg <- load.image("path/to/image.jpg") plot(imgjpg, xlim = c(1, width(imgjpg)), ylim = c(height(imgjpg), 1)) ``` 请注意,以上代码中的"path/to/image.png"和"path/to/image.jpg"需要替换为实际的图像文件路径。同时,确保安装了相应的包(png包和imager包)并加载了它们。 #### 引用[.reference_title] - *1* *2* [R语言读取图片方式总结](https://blog.csdn.net/weixin_45534890/article/details/108089035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [R语言 图片识别文字 PNG JPG图片转文字 OCR tesseract包](https://blog.csdn.net/weixin_46623488/article/details/120495200)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值