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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.