cv2r is an R wrapper to Pyhon OpenCV using reticulate. There is a few additions to simplify the integration in RStudio.
The very first time you may need to install OpenCV lib in your python environment.
library(reticulate) library(cv2r) install_opencv()
Than it is pretty simle to read and show an image.
img_url <- "https://upload.wikimedia.org/wikipedia/fr/4/4e/RStudio_Logo.png" my_image <- imread(img_url) # Very simple to plot images imshow(mat=my_image)
There is also a few modifications to simplify image pixel selection. You can use R matrix index on an numpy.ndarray variable.
# Lets show the alpha mask imshow(mat=my_image[,,4])
# To use advances matrix subseting you shall convert the matrix to R my_r_image <- reticulate::py_to_r(my_image) imshow(mat=my_r_image[50:300,200:900,c(4,2,2)])
If you are using RStudio 1.2 you can call plot from a python chunk
print(r.my_image.shape)
You can naturraly do your cv2 code in python
import cv2 blured_img = cv2.blur(r.my_image, (100,100) ) # We call the cv2r::imshow version to get the result in the markdown document r.imshow(mat=blured_img)
You can change the colorspace
# change color space cvtColor(my_image) <- "HSV" # change Hue my_image[,,1] <- my_image[,,1]*2+50 imshow(mat=my_image)
library(data.table) my_image <- imread(img_url) my_table <- as.data.table(my_image) summary(my_table)
So that you can do data science on pixels
hist(my_table[, R])
Then change pixels and revert to image
imshow(mat=my_table)
If you need to put in a 3D space
pairs(my_table[sample.int(size = 100, n = nrow(my_table)),.(R,G,B, A)])
Than you update values in 3D spaces
my_table[R < 100 & G > 50, R:=200] imshow(mat=my_table)
You can also remove points in any colorspace and plot the result with transparency
# remove alpha before converting to HSV my_hsv_image <- my_image[,,1:3] cvtColor(my_hsv_image) <- "HSV" my_hsv_table <- as.data.table(my_hsv_image) imshow(mat=my_hsv_table[V > 200 & V < 240,])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.