R language advanced image processing

R language advanced image processing

Original tecdat.cn/?p=3431

 

The ImageMagick library has a large number of functions. This article briefly introduces the most important concepts for getting started.

installation 
magick

On Windows or OS-X, it is easiest to install software packages through CRAN.

install.packages( "magick" ) Copy code

The binary CRAN package works out of the box.

library(magick) Copy code
## Linking to ImageMagick 6.9.9.39 ## Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp ## Disabled features: fftw, ghostscript, x11 Copy code
str (magick :: magick_config ()) Copy the code
## 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 Copy code

Build from source

sudo apt-get install libmagick ++ - dev copy the code
sudo yum install ImageMagick-c ++ - devel duplicated code

To install from source on OS-X, you need

imagemagick@6
Homemade software.

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

The default imagemagick configuration in the homebrew software disables a series of functions.

Image IO

 

Read and write

You can use the file path with the image data, URL or original vector to read the image directly

image_read
. The
image_info
The function displays some meta-type data about the image, similar to ImageMagick 
identify
Command line utility.

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

 

Use us

image_write
Export images in any format to a file on disk or in memory.

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

 

Conversion format

Magick saves the image in memory in its original format. To be specified as the conversion format other

format
parameter
image_write
. You can also convert the image to other formats internally before applying the conversion. This may be useful if your original format is lossy.

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

 

On Linux, you use

image_display
Preview the image in the X11 window.

# X11 image_display(tiger) # Dependent system image_browse(tiger) Copy code

Another method is to convert the image into a raster object and draw it on the R graphics display.

Conversion

Here are a few examples to understand.

Cut and edit

Convert some functions to use a

geometry
Parameter, the parameter requires a special syntax of the form,
AxB+C+D
. Some examples where each element is optional:

  • image_crop(image, "100x150+50")
    : Crop
    width:100px
    And from the left
    height:150px
    begin
    +50px
  • image_scale(image, "200")
    : Adjust the width proportionally:
    200px
  • image_scale(image, "x200")
    : Adjust height proportionally:
    200px
  • image_fill(image, "blue", "+100+200")
    : Start filling from the blue dot
    x:100, y:200
  • image_border(frink, "red", "20x10")
    : Add a border of 20 pixels on the left + right and 10 px on the top + bottom
print(frink) Copy code
## format width height colorspace matte filesize density ## 1 PNG 220 445 sRGB TRUE 73494 72x72 Copy code

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

image_trim(frink) Copy code

image_crop(frink, "100x150+50" ) Copy code

image_scale (Frink, "300" ) # width: 300px Copy the code

 

image_scale (Frink, "X300" ) # height: 300px Copy the code

image_fill
We can fill in pixels
point
.
fuzz
The parameter allows to fill pixels with similar colors. Its value must be between 0 and 256^2.

Filters and effects

 

image_noise(frink) Copy code

Kernel convolution

image_convolve()
The function to apply kernel convolution on the image means to recalculate each pixel value using the weighted neighborhood defined in the kernel matrix. For example:

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 Copy code
## [,1] [,2] [,3] ## [1,] 0.00 0.25 0.00 ## [2,] 0.25 0.00 0.25 ## [3,] 0.00 0.25 0.00 Copy code

This kernel changes each pixel to the average of its horizontal and vertical adjacent pixels, which produces a slight blur effect in the right image below:

img <- image_resize(logo, "300x300" ) img_blurred <- image_convolve(img, kern) image_append ( C (IMG, img_blurred)) copying the code

Use standard kernel

Text annotation

Finally, it can be useful to output some text on the image:

image_annotate (Frink, "the I R & lt like!" , size = 70 , = Gravity "Southwest" , Color = "Green" ) copy the code

image_annotate (Frink, "of The Quick Brown Fox" , font = 'Times' , size = 30 ) copying the code

The fonts supported on most platforms include

"sans"
,
"mono"
,
"serif"
,
"Times"
,
"Helvetica"
,
"Trebuchet"
,
"Georgia"
,
"Palatino"
or
"Comic Sans"
.

Combined pipeline

Each function transformation of the image will not affect the original image.

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

Combination conversion:

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

use

magrittr
Pipe syntax makes it more readable

Image vector

The above example involves a single image. However, all functions in magick have been vectorized to support the use of layers, compositing or animation.

 

image_scale( "200x" ) %>% image_quantize( 128 ) length (earth) Copy code
## [1] 44Copy code

## 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 Copy code

 

Layer

We can stack layers together like in 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 Copy code

 

Combine

Attach simply place the frames next to each other:

Used for

stack = TRUE
Put them on top of each other:

 

Web page

When reading a PDF document, each page becomes an element of a vector.

## 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 Copy code

 

Animation

We can also make frames in animation instead of treating vector elements as layers.

Create a series of deformations

n
Image, gradually transforming one image into another.

If you import an existing GIF or video file, each frame will become a layer:

## 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 Copy code

Manipulate the individual frames and put them back into the animation:

## 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 Copy code

Animation can be saved as GIF of MPEG file:

Drawing and graphics

It produces a magick image object of this software package.

Graphics device

The

image_graph()
The function opens a similar to
png()
Or a new graphics device
x11()
. It returns the image object that will be written into the drawing.

We can easily post-process the graphics using conventional image manipulation.

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

 

Drawing equipment

Another way to use a graphics device is to use pixel coordinates to draw on an existing image.

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

 

by default,

image_draw()
Set all margins to 0, and use graphic coordinates to match the image size (in pixels) (width X height), where (0,0) is the upper left corner.

Animated graphics

The graphics device supports multiple frames and can easily create animated graphics. The following code shows how to implement this example from the gganimate package using the magick graphics device .

## 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 Copy code

To write it to a file, you only need to do the following:

 

OCR text extraction

The newest member of the package is to use OCR to extract text from images:

## format width height colorspace matte filesize density ## 1 PNG 640 480 sRGB TRUE 23 359 72x72 Copy code

Thank you very much for reading this article. If you have any questions, please leave a message below!


 

Most popular insights

1. Introduction to image processing in python using opencv

2. Partial least squares regression (plsr) and principal component regression (pcr) in matlab

3. Variational modal decomposition using vmd in matlab

4. Matlab uses hampel filter to remove outliers

5. Matlab uses empirical mode to decompose emd- to denoise the signal

6. Partial least squares regression (plsr) and principal component regression (pcr) in matlab

7. Matlab uses copula simulation to optimize market risk

8. Advanced image processing in r language

9. Matlab realizes the Markov switching arma-garch model estimation of mcmc