←Select platform

KMeansCommand Class

Summary
Performs image segmentation and color reduction using the K-means algorithm.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public class KMeansCommand : RasterCommand 
@interface LTKMeansCommand : LTRasterCommand 
public class KMeansCommand 
    extends RasterCommand 
public ref class KMeansCommand : public RasterCommand   
class KMeansCommand(RasterCommand): 
Remarks
  • K-Means is an algorithm for analyzing data. Each observation gets placed in the cluster having the nearest mean.
  • The number of clusters returned are less than or equal to the number of input clusters.
  • If the image contains fewer clusters than the number of input clusters, this command will return the image colors in the OutCenters property.
  • This command can only process entire images. It does not support regions.
  • This command converts image to 8-bit grayscale.
  • This command supports signed/unsigned images.
  • This command supports 12 and 16-bit grayscale and 48 and 64-bit color images. Support for 12 and 16-bit grayscale and 48 and 64-bit color images is available in the Document/Medical Imaging toolkits.

Kmeans Segmentation Function - Before

Kmeans Segmentation Function - Before

Kmeans Segmentation Function - After

Kmeans Segmentation Function - After

View additional platform support for this Kmeans Segmentation function.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing.Core; 
 
public void KMeansCommandExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.ThrowExceptionsOnInvalidImages = true; 
 
   //Load an image 
   RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "cannon.jpg")); 
 
   //Prepare the command 
   KMeansCommand command = new KMeansCommand(); 
 
   command.Clusters = 7; 
   command.Type = KMeansCommandFlags.KMeans_Random; 
 
   //Apply  
   command.Run(image); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.IOException; 
 
import org.junit.*; 
import org.junit.Test; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.core.*; 
 
 
public void kMeansCommandExample() { 
    final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
 
    RasterCodecs codecs = new RasterCodecs(); 
    codecs.setThrowExceptionsOnInvalidImages(true); 
 
    // Load an image 
    RasterImage image = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "cannon.jpg")); 
 
    // Prepare the command 
    KMeansCommand command = new KMeansCommand(); 
 
    command.setClusters(7); 
    command.setType(KMeansCommandFlags.KMEANS_RANDOM); 
    assertTrue(command.getClusters() == 7 && command.getType() == KMeansCommandFlags.KMEANS_RANDOM); 
 
    // Apply 
    command.run(image); 
    codecs.save(image, combine(LEAD_VARS_IMAGES_DIR, "cannonResult.jpg"), RasterImageFormat.JPEG, 24); 
 
    assertTrue(new File(combine(LEAD_VARS_IMAGES_DIR, "cannonResult.jpg")).exists()); 
    System.out.println("Command run and image exported to: " + combine(LEAD_VARS_IMAGES_DIR, "cannonResult.jpg")); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.3.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.ImageProcessing.Core Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.