Image contours in R

I recently came across this short fun post on R-bloggers that demonstrated how to use the image.ContourDetector package (available on CRAN) to extract contours from an image. The image of the contours looked really cool so I thought I would try it out for myself!

As an example, I wanted to extract the contours from the image below:


Here is the code taken from the R-bloggers post (slightly modified):


img_path <- paste0("",
img <- magick::image_read(img_path)
mat <- magick::image_data(img, channels = "gray")
mat <- drop(as.integer(mat, transpose = TRUE))
contourlines <- image_contour_detector(mat)

And here is the result. Not bad for a little chunk of code!

Not bad, but can we do better? In particular, we can’t really see any of the facial features of the players.

It turns out that the image_contour_detector() function has a function option Q, which (according to the documentation) represents “numeric value with the pixel quantization step”. This is discussed in Section 5 of the paper introducing the method. My high-level understanding of the issue is that in digital image processing, the pixel values are usually quantized to integers while the algorithm was designed to work on pixels with real values. As a result, the algorithm can produce some artifacts. The paper gives the figure below as an example:

… I actually like the artifacts! The default value of the parameter is Q = 2. To get more contours, we could set Q = 0, i.e. change the second-last line above to

contourlines <- image_contour_detector(mat, Q = 0)

I think it’s a bit better! Here are the contours for 3 different Q values, placed side by side:

2 thoughts on “Image contours in R

    • I didn’t as I’m not familiar with this area. I was googling around for other packages and none showed up! So thanks for the references.

      It would certainly be interesting to compare the contours generated by other methods. In my experimentation so far, I’ve found it difficult to get image.ContourDetector to give me denser, more descriptive contours.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s