<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 11.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreImage",
  "identifier" : "/documentation/CoreImage/CIFilter-swift.class/kMeans()",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Core Image",
      "CoreImage"
    ],
    "preciseIdentifier" : "c:objc(cs)CIFilter(cm)KMeansFilter"
  },
  "title" : "kMeans()"
}
-->

# kMeans()

Applies the k-means algorithm to find the most common colors in an image.

```
class func kMeans() -> any CIFilter & CIKMeans
```

## Return Value

A one-dimensional [`CIImage`](/documentation/CoreImage/CIImage) containing the colors.

## Discussion

This filter uses the k-means clustering algorithm to find the most common colors in an input image. The result is a [`CIImage`](/documentation/CoreImage/CIImage) with `count` x 1 dimensions. Each `RGBA` pixel in the result image represents the center of a k-means cluster. The `RGB` components contain the color and the alpha component represents the weight of the color. You typically use the [`kMeans()`](/documentation/CoreImage/CIFilter-swift.class/kMeans()) filter in conjunction with the [`palettize()`](/documentation/CoreImage/CIFilter-swift.class/palettize()) filter to produce an image with a reduced number of colors.

- `inputImage`: A [`CIImage`](/documentation/CoreImage/CIImage) to process.
- `extent`: A <doc://com.apple.documentation/documentation/CoreFoundation/CGRect> specifying the area of the image to analyze.
- `means`: An optional [`CIImage`](/documentation/CoreImage/CIImage) containing a set of colors to use as seeds for the k-means clustering.
- `count`: The number of k-means color clusters that should be created. Maximum is `128`, and default is `8`.
- `passes`: The number of k-means passes that should run. Maximum is `20`, and default is `5`.
- `perceptual`: Whether the k-means color palette should use a perceptual color space.

> Tip:
> The colors in the result of the ``doc://com.apple.coreimage/documentation/CoreImage/CIFilter-swift.class/kMeans()`` filter have an alpha component that indicates the weight of the color. You should set this value one using ``doc://com.apple.coreimage/documentation/CoreImage/CIImage/settingAlphaOne(in:)`` before using the palette.

The following code example uses the [`kMeans()`](/documentation/CoreImage/CIFilter-swift.class/kMeans()) filter followed by the [`palettize()`](/documentation/CoreImage/CIFilter-swift.class/palettize()) filter to reduce the colors in the image to four:

```swift
func kMeans(inputImage: CIImage) -> CIImage {
    let filter = CIFilter.kMeans()
    filter.inputImage = inputImage
    filter.extent = inputImage.extent
    filter.count = 4
    filter.passes = 5
    return filter.outputImage!
}

func palettize(inputImage: CIImage, paletteImage: CIImage) -> CIImage {
    let palettize = CIFilter.palettize()
    palettize.inputImage = inputImage
    palettize.paletteImage = paletteImage
    return palettize.outputImage!
}

let palette = kMeans(inputImage: image)
let palettized = palettize(inputImage: image, palette.settingAlphaOne(in: palette.extent))
```

![Three images arranged horizontally. The image on the left is a closeup photograph of a cactus. The center image consists of squares arranged vertically showing the four main colors from the left image. The image on the right shows the image with the reduced colors.](images/com.apple.coreimage/media-4332587@2x.png)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)