ImageMagick库具有大量功能。本文简要介绍了最重要的入门概念。

安装 magick

在的Windows或OS-X上,通过CRAN最容易安装软件包。

install.packages("magick")

二进制CRAN包开箱即用。

library(magick)
## Linking to ImageMagick 6.9.9.39
## Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp
## Disabled features: fftw, ghostscript, x11
str(magick::magick_config())
## List of 21
##  $ version           :Class 'numeric_version'  hidden list of 1
##   ..$ : int [1:4] 6 9 9 39
##  $ modules           : logi FALSE
##  $ cairo             : logi TRUE
##  $ fontconfig        : logi TRUE
##  $ freetype          : logi TRUE
##  $ fftw              : logi FALSE
##  $ ghostscript       : logi FALSE
##  $ jpeg              : logi TRUE
##  $ lcms              : logi TRUE
##  $ libopenjp2        : logi TRUE
##  $ lzma              : logi TRUE
##  $ pangocairo        : logi TRUE
##  $ pango             : logi TRUE
##  $ png               : logi TRUE
##  $ rsvg              : logi TRUE
##  $ tiff              : logi TRUE
##  $ webp              : logi TRUE
##  $ wmf               : logi FALSE
##  $ x11               : logi FALSE
##  $ xml               : logi TRUE
##  $ zero-configuration: logi TRUE

从源构建

sudo apt-get install libmagick++-dev
sudo yum install ImageMagick-c++-devel

要在OS-X上从源代码安装,您需要imagemagick@6自制软件。

brew reinstall imagemagick@6 --with-fontconfig --with-librsvg
brew link --force imagemagick@6

自制软件中的默认imagemagick配置会禁用一系列功能。

图像IO

 

读和写

可以使用带有图像数据的文件路径,URL或原始向量直接读取图像image_read。该image_info函数显示了一些关于图像的元型态数据,类似于ImageMagick的  identify命令行实用程序。


print(tiger)
##   format width height colorspace matte filesize density
## 1    PNG   400    400       sRGB  TRUE        0   72x72

 

使用我们image_write以任何格式将图像导出到磁盘上的文件或内存中。

image_write(tiger, path = "tiger.png", format = "png")

 

转换格式

Magick以原始格式将图像保存在内存中。要指定为转换格式其他的format参数image_write。在应用转换之前,您还可以在内部将图像转换为其他格式。如果您的原始格式有损,这可能很有用。

tiger_png <- image_convert(tiger, "png")
image_info(tiger_png)
##   format width height colorspace matte filesize density
## 1    PNG   400    400       sRGB  TRUE        0   72x72

 

在Linux的上,您使用image_display在X11窗口中预览图像。

# X11 
image_display(tiger)

# 依赖系统
image_browse(tiger)

另一种方法是将图像转换为光栅对象并将其绘制在R的图形显示上。

转换

下面举几个例子来了解。

剪切和编辑

转换一些函数采用一个geometry参数,该参数需要表单的特殊语法,AxB+C+D。其中每个元素都是可选的一些例子:

  • image_crop(image, "100x150+50")裁剪width:100px并从左侧height:150px开始+50px
  • image_scale(image, "200")按比例调整宽度:200px
  • image_scale(image, "x200")按比例调整高度:200px
  • image_fill(image, "blue", "+100+200")从蓝点开始填充x:100, y:200
  • image_border(frink, "red", "20x10")添加左侧+右侧20像素和顶部+底部10px的的边框
print(frink)
##   format width height colorspace matte filesize density
## 1    PNG   220    445       sRGB  TRUE    73494   72x72

R语言高级图像处理_编程开发

image_border(image_background(frink, "hotpink"), "#000080", "20x10")

R语言高级图像处理_编程开发_02

image_trim(frink)

R语言高级图像处理_编程开发_03

image_crop(frink, "100x150+50")

R语言高级图像处理_编程开发_04

image_scale(frink, "300") # width: 300px

 

image_scale(frink, "x300") # height: 300px

R语言高级图像处理_R语言_05

R语言高级图像处理_编程开发_06

R语言高级图像处理_R语言_07

R语言高级图像处理_R语言_08

R语言高级图像处理_R语言_09

R语言高级图像处理_编程开发_10

image_fill我们可以填补像素pointfuzz参数允许填充具有相似颜色的像素。它的值必须在0到256 ^ 2之间。

过滤器和效果

 

R语言高级图像处理_编程开发_11

image_noise(frink)

R语言高级图像处理_R语言_12

R语言高级图像处理_R语言_13

R语言高级图像处理_编程开发_14

内核卷积

image_convolve()函数在图像上应用内核卷积意味着使用内核矩阵中定义的加权邻域来重新计算每个像素值例如:

kern <- matrix(0, ncol = 3, nrow = 3)
kern[1, 2] <- 0.25
kern[2, c(1, 3)] <- 0.25
kern[3, 2] <- 0.25
kern
##      [,1] [,2] [,3]
## [1,] 0.00 0.25 0.00
## [2,] 0.25 0.00 0.25
## [3,] 0.00 0.25 0.00

此内核将每个像素更改为其水平和垂直相邻像素的平均值,这会在下面的右侧图像中产生轻微的模糊效果:

img <- image_resize(logo, "300x300")
img_blurred <- image_convolve(img, kern)
image_append(c(img, img_blurred))

R语言高级图像处理_R语言_15

使用标准内核

R语言高级图像处理_R语言_16

R语言高级图像处理_R语言_17

文本注释

最后,在图像上输出一些文本会很有用:

image_annotate(frink, "I like R!", size = 70, gravity = "southwest", color = "green")

R语言高级图像处理_编程开发_18

R语言高级图像处理_R语言_19

image_annotate(frink, "The quick brown fox", font = 'Times', size = 30)

R语言高级图像处理_R语言_20

支持的大多数平台上的字体包括"sans""mono""serif""Times""Helvetica""Trebuchet""Georgia""Palatino""Comic Sans"

结合管道

图像每个函数变换不会影响原始图像。

##   format width height colorspace matte filesize density
## 1    PNG   220    445       sRGB  TRUE    73494   72x72
##   format width height colorspace matte filesize density
## 1    PNG   100    202       sRGB  TRUE        0   72x72

组合转换:

##   format width height colorspace matte filesize density
## 1    PNG   465    240       sRGB  TRUE        0   72x72

R语言高级图像处理_R语言_21

使用magrittr管道语法使其更具可读性

R语言高级图像处理_R语言_22

图像矢量

以上示例涉及单个图像。然而,magick中的所有函数都已经过矢量化,以支持使用图层,合成或动画。

 

 image_scale("200x") %>%
  image_quantize(128)

length(earth)
## [1] 44

R语言高级图像处理_编程开发_23

##   format width height colorspace matte filesize density
## 1    GIF   200    200        RGB FALSE        0   72x72
## 2    GIF   200    200        RGB FALSE        0   72x72
## 3    GIF   200    200        RGB FALSE        0   72x72
## 4    GIF   200    200        RGB FALSE        0   72x72
## 5    GIF   200    200        RGB FALSE        0   72x72
## 6    GIF   200    200        RGB FALSE        0   72x72

 

图层

我们可以像在Photoshop中中一样将图层堆叠在一起:

##   format width height colorspace matte filesize density
## 1   JPEG   300    225       sRGB FALSE        0   72x72
## 2    PNG   300    232       sRGB  TRUE        0   72x72
## 3    PNG   148    300       sRGB  TRUE        0   72x72

 

R语言高级图像处理_编程开发_24

R语言高级图像处理_编程开发_25

结合

附加简单地将框架彼此相邻:

用于stack = TRUE将它们放在彼此的顶部:

R语言高级图像处理_R语言_26

 

网页

在阅读PDF文档时,每个页面都成为向量的元素。

##    format width height colorspace matte filesize density
## 1     PNG   612    792       sRGB  TRUE        0   72x72
## 2     PNG   612    792       sRGB  TRUE        0   72x72
## 3     PNG   612    792       sRGB  TRUE        0   72x72
## 4     PNG   612    792       sRGB  TRUE        0   72x72
## 5     PNG   612    792       sRGB  TRUE        0   72x72
## 6     PNG   612    792       sRGB  TRUE        0   72x72
## 7     PNG   612    792       sRGB  TRUE        0   72x72
## 8     PNG   612    792       sRGB  TRUE        0   72x72
## 9     PNG   612    792       sRGB  TRUE        0   72x72
## 10    PNG   612    792       sRGB  TRUE        0   72x72
## 11    PNG   612    792       sRGB  TRUE        0   72x72
## 12    PNG   612    792       sRGB  TRUE        0   72x72
## 13    PNG   612    792       sRGB  TRUE        0   72x72
## 14    PNG   612    792       sRGB  TRUE        0   72x72
## 15    PNG   612    792       sRGB  TRUE        0   72x72
## 16    PNG   612    792       sRGB  TRUE        0   72x72
## 17    PNG   612    792       sRGB  TRUE        0   72x72
## 18    PNG   612    792       sRGB  TRUE        0   72x72
## 19    PNG   612    792       sRGB  TRUE        0   72x72
## 20    PNG   612    792       sRGB  TRUE        0   72x72
## 21    PNG   612    792       sRGB  TRUE        0   72x72
## 22    PNG   612    792       sRGB  TRUE        0   72x72
## 23    PNG   612    792       sRGB  TRUE        0   72x72
## 24    PNG   612    792       sRGB  TRUE        0   72x72
## 25    PNG   612    792       sRGB  TRUE        0   72x72
## 26    PNG   612    792       sRGB  TRUE        0   72x72
## 27    PNG   612    792       sRGB  TRUE        0   72x72
## 28    PNG   612    792       sRGB  TRUE        0   72x72
## 29    PNG   612    792       sRGB  TRUE        0   72x72

 

动画

我们也可以在动画中制作帧,而不是将矢量元素视为图层。

R语言高级图像处理_编程开发_27

创建³³变形一系列n图像,逐渐将一个图像变换为另一个图像。

如果您读入现有的GIF或视频文件,则每个帧都会成为一个图层:

##   format width height colorspace matte filesize density
## 1    GIF   150    148       sRGB  TRUE        0   72x72
## 2    GIF   150    148       sRGB  TRUE        0   72x72
## 3    GIF   150    148       sRGB  TRUE        0   72x72
## 4    GIF   150    148       sRGB  TRUE        0   72x72
## 5    GIF   150    148       sRGB  TRUE        0   72x72
## 6    GIF   150    148       sRGB  TRUE        0   72x72
## 7    GIF   150    148       sRGB  TRUE        0   72x72
## 8    GIF   150    148       sRGB  TRUE        0   72x72

操纵各个帧并将它们放回动画中:

##   format width height colorspace matte filesize density
## 1    gif   200    155       sRGB  TRUE        0   72x72
## 2    gif   200    155       sRGB  TRUE        0   72x72
## 3    gif   200    155       sRGB  TRUE        0   72x72
## 4    gif   200    155       sRGB  TRUE        0   72x72
## 5    gif   200    155       sRGB  TRUE        0   72x72
## 6    gif   200    155       sRGB  TRUE        0   72x72
## 7    gif   200    155       sRGB  TRUE        0   72x72
## 8    gif   200    155       sRGB  TRUE        0   72x72

动画可以保存为MPEG文件的GIF:

绘图和图形

该软件包的它产生一个magick图像对象。

图形设备

image_graph()功能打开一个类似于png()或新的图形设备x11()。它返回将要写入绘图的图像对象。

我们可以使用常规图像操作轻松地对图形进行后处理。

##   format width height colorspace matte filesize density
## 1    PNG   400    400       sRGB  TRUE        0   72x72

 

绘图设备

使用图形设备的另一种方式是使用像素坐标在现有图像上绘制。

##   format width height colorspace matte filesize density
## 1    PNG   220    445       sRGB  TRUE        0   72x72

 

默认情况下,image_draw()将所有边距设置为0,并使用图形坐标匹配图像大小(以像素为单位)(宽度X高度),其中(0,0)是左上角。

动画图形

图形设备支持多个帧,可以轻松创建动画图形。下面的代码显示了如何使用magick图形设备从gganimate包中实现该示例。

##    format width height colorspace matte filesize density
## 1     gif   600    340       sRGB  TRUE        0   72x72
## 2     gif   600    340       sRGB  TRUE        0   72x72
## 3     gif   600    340       sRGB  TRUE        0   72x72
## 4     gif   600    340       sRGB  TRUE        0   72x72
## 5     gif   600    340       sRGB  TRUE        0   72x72
## 6     gif   600    340       sRGB  TRUE        0   72x72
## 7     gif   600    340       sRGB  TRUE        0   72x72
## 8     gif   600    340       sRGB  TRUE        0   72x72
## 9     gif   600    340       sRGB  TRUE        0   72x72
## 10    gif   600    340       sRGB  TRUE        0   72x72
## 11    gif   600    340       sRGB  TRUE        0   72x72
## 12    gif   600    340       sRGB  TRUE        0   72x72

R语言高级图像处理_编程开发_28

要将其写入文件,您只需执行以下操作:

 

OCR文本提取

该软件包的最新成员是使用OCR从图像中提取文本:

##   format width height colorspace matte filesize density
## 1    PNG   640    480       sRGB  TRUE    23359   72x72

R语言高级图像处理_R语言_29

非常感谢您阅读本文,有任何问题请在下面留言!


R语言高级图像处理_编程开发_30 

最受欢迎的见解

1.使用opencv在python中进行图像处理的简介

2.matlab中的偏最小二乘回归(plsr)和主成分回归(pcr)

3.matlab中使用vmd变分模态分解

4.matlab使用hampel滤波去除异常值

5.matlab使用经验模式分解emd-对信号进行去噪

6.matlab中的偏最小二乘回归(plsr)和主成分回归(pcr)

7.matlab使用copula仿真优化市场风险

8.r语言高级图像处理

9.matlab实现mcmc的马尔可夫切换arma-garch模型估计