O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required