I am working on an R assignment about Sobel edge-detection. Unfortunately the video tutorial I was following uses R for every other task, but switches to python for image processing - I am guessing he did not find any useful R package for image convolution type work (this tutorial is from last year). I have tried EBImage and magick (this one seems new), but did not find much. This magick vignette talks aboutimage_convolve('Sobel')
(about half way down the page) but only for vertical edges, not horizontal. Can someone suggest some good material that I can use? I am fairly new to image processing.
Update:
I have managed to get as far as detecting vertical and horizontal edges separately using magick package (code pasted below), but do not know how to combine them to generate a single image
library(magick)
# get image
img <- image_read("https://www.r-project.org/logo/Rlogo.png")
print(image_info(img))
# define horizontal and vertical Sobel kernel
Shoriz <- matrix(c(1, 2, 1, 0, 0, 0, -1, -2, -1), nrow = 3)
Svert <- t(Shoriz)
# get horizontal and vertical edges
imgH <- image_convolve(img, Shoriz)
imgV <- image_convolve(img, Svert)
print(plot(as.raster(img))) # view original image
print(plot(as.raster(imgH))) # view horizontal edges
print(plot(as.raster(imgV))) # view vertical edges
From the tutorial, next I need to combineimgH
andimgV
by computing the Euclidean distance between these edges, butdist()
won't work with the image objects themselves. I need to get the data from these images, but do not know how. Something similar toimageData()
inEBImage
package would help, but cannot find it inmagick
.It hasimage_data()
function, but its output looks complicated.
Update(2):
I have (hopefully) got what I wanted withEBImage
package (code below). I'd still like to get it working withmagick
package, once I figure out how to get pixel data from edge images as described above, and also how to transform the final edge data back to image.
library(EBImage)
# get image
img <- readImage("https://www.r-project.org/logo/Rlogo.png")
print(img, short = T)
# define horizontal and vertical Sobel kernel
Shoriz <- matrix(c(1, 2, 1, 0, 0, 0, -1, -2, -1), nrow = 3)
Svert <- t(Shoriz)
# get horizontal and vertical edges
imgH <- filter2(img, Shoriz)
imgV <- filter2(img, Svert)
# combine edge pixel data to get overall edge data
hdata <- imageData(imgH)
vdata <- imageData(imgV)
edata <- sqrt(hdata^2 + vdata^2)
# transform edge data to image
imgE <- Image(edata, colormode = 2)
print(display(combine(img, imgH, imgV, imgE), method = "raster", all = T))
Thanks.