How to do it...

  1. First, read the image and then resize it to reduce the dimensions and then plot the image:
> img = readImage("bird.jpg")> img_resize = resizeImage(img, 350, 350, method = 'bilinear') > imageShow(img_resize) 

Here is the sample image:

  1. Now vectorize the image and check the image dimension:
> img_vector = apply(img_resize, 3, as.vector) # vectorize RGB> dim(img_vector)[1] 122500 3
  1. Next, we will use mini-batch-kmeans clustering on the image vector and then predict the vectorized image using the cluster centroid. Finally, we will apply the prediction and cluster centroid to generate the following new segmented image, shown ...

Get R Data Analysis Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.