To perform image convolution in R, the image data must first be converted into a matrix format, with three layers for color images (RGB) and one layer for grayscale images. Each element in the matrix represents a value from 0 to 255, which is used in the convolution process.
In R, we can use 'magick' library to perform image convolution. The library provides functions for defining the kernel and performing the convolution operation on the image data. The output of the convolution is a modified version of the original image, which can be used for various image processing tasks.
For example, to perform a simple edge detection convolution, a kernel that detects edges can be defined and then convolved with the image data. The resulting output image will highlight the edges in the original image. Below image shows a convolution process with image matrix data. Here, after combining an 8x8 pixel input matrix with a 3x3 kernel, the output becomes 6x6 matrix data.
In this tutorial, we use 'magick' library to perform image convolution. First, we'll install the package with install.package() command then activate it by loading the package.
> install.packages("magick")
> library(magick)
We'll prepare target image file to use. Below code shows how to load image file.
> image=image_read("C:/tmp/20180812_141901.jpg")
> image
format width height colorspace matte filesize density
1 JPEG 4608 3456 sRGB FALSE 4396226 72x72
We change the size of the image to make it smaller.
> image=image_scale(image,"130x100")
> image
format width height colorspace matte filesize density
1 JPEG 130 98 sRGB FALSE 0 72x72
> plot(image)
Some of the effects by using specific kernel operators are shown below.
Edge detection
Edges in an image can be detected by the Sobel operator. We create a Sobel kernel matrix for vertical and horizontal edges.
> sobelH = matrix(c(1,2,1, 0,0,0, -1,-2,-1), nrow = 3, ncol = 3)
> sobelH
[,1] [,2] [,3]
[1,] 1 0 -1
[2,] 2 0 -2
[3,] 1 0 -1
> sobelV=t(sobelH)
> sobelV
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 0 0 0
[3,] -1 -2 -1
To apply the convolution process, we use a convolve() function of 'magick' package.
> image_convolve(image, sobelH) -> imgEdgeH
> plot(imgEdgeH)
> image_convolve(image, sobelV) -> imgEdgeV
> plot(as.raster(imgEdgeV))
> edge = matrix(c(0,1,0, 1,-4,1, 0,1,0), 3, 3)
> edge
[,1] [,2] [,3]
[1,] 0 1 0
[2,] 1 -4 1
[3,] 0 1 0
> image_convolve(image, edge) -> imgEdge
> plot(imgEdge)
> edge2 = matrix(c(-1,-1,-1, -1,8,-1, -1,-1,-1),3, 3)
> edge2
[,1] [,2] [,3]
[1,] -1 -1 -1
[2,] -1 8 -1
[3,] -1 -1 -1
> image_convolve(image, edge2) -> imgEgde2
> plot(imgEgde2)
Emboss
> embos = matrix(c(-2,0,0, 0,1,0, 0,0,2),3, 3)
> embos
[,1] [,2] [,3]
[1,] -2 0 0
[2,] 0 1 0
[3,] 0 0 2
> image_convolve(image, embos) -> imgEmbos
> plot(imgEmbos)
Sharpen
> sharpen = matrix(c(0,-1,0, -1,6,-1, 0,-1,0),3, 3)
> sharpen
[,1] [,2] [,3]
[1,] 0 -1 0
[2,] -1 6 -1
[3,] 0 -1 0
> image_convolve(bus, sharpen) -> imgSharpen
> plot(imgSharpen)
You can change the kernel element values and observe the changes in an image.
No comments:
Post a Comment