From d42f43fb59522294f85f61427b781629732a1e73 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 28 Jan 2021 15:17:27 -0500 Subject: [PATCH] Update kmeans.md Added an example of k-means on 2D images --- doc/source/kmeans.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/source/kmeans.md b/doc/source/kmeans.md index 1661a136..c7b4e95c 100644 --- a/doc/source/kmeans.md +++ b/doc/source/kmeans.md @@ -54,3 +54,29 @@ result = kmeans(features, 3); # run K-means for the 3 clusters scatter(iris.PetalLength, iris.PetalWidth, marker_z=result.assignments, color=:lightrainbow, legend=false) ``` + +```@example julia K-means clustering on images. +using TestImages, Clustering, ImageView + +# Loads a gray scale image. +img = testimage("camera") +# Convert into a normal Array +img = Array{Float64,2}(img) + +# Reshape that image into a vector. +Nx, Ny = size(img) +dat = reshape(img, 1, Nx*Ny) + +#= +Feed in the image vector, # segments, show iteration info +and set max iterations +=# +result = kmeans(dat, 3, display=:iter, maxiter=200) + +# Reshape the data back into its orginal form. +img_result = reshape(result.assignments,Nx,Ny) + +# View the newly segmented image. +imshow(img_result) + +```