<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MetalPerformanceShaders",
  "identifier" : "/documentation/MetalPerformanceShaders/MPSImageHistogram",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Metal Performance Shaders"
    ],
    "preciseIdentifier" : "c:objc(cs)MPSImageHistogram"
  },
  "title" : "MPSImageHistogram"
}
-->

# MPSImageHistogram

A filter that computes the histogram of an image.

```
class MPSImageHistogram
```

## Overview

Typically, you use an [`MPSImageHistogram`](/documentation/MetalPerformanceShaders/MPSImageHistogram) filter to calculate an image’s histogram that is passed to a subsequent filter such as [`MPSImageHistogramEqualization`](/documentation/MetalPerformanceShaders/MPSImageHistogramEqualization) or [`MPSImageHistogramSpecification`](/documentation/MetalPerformanceShaders/MPSImageHistogramSpecification).

The following listing shows how you can create a histogram filter to calculate the histogram of the <doc://com.apple.documentation/documentation/Metal/MTLTexture>, `sourceTexture`. The filter is passed an instance of [`MPSImageHistogramInfo`](/documentation/MetalPerformanceShaders/MPSImageHistogramInfo) that specifies information to compute the histogram for the channels of an image. After encoding, `histogramInfoBuffer` contains the histogram information and can be used for further operations such as equalization or specification.

Listing 1. Creating a histogram filter

```swift
var histogramInfo = MPSImageHistogramInfo(
    numberOfHistogramEntries: 256,
    histogramForAlpha: false,
    minPixelValue: vector_float4(0,0,0,0),
    maxPixelValue: vector_float4(1,1,1,1))
     
let calculation = MPSImageHistogram(device: device,
                                    histogramInfo: &histogramInfo)
let bufferLength = calculation.histogramSize(forSourceFormat: sourceTexture.pixelFormat)
let histogramInfoBuffer = device.makeBuffer(length: bufferLength, 
                                            options: [.storageModePrivate])
     
calculation.encode(to: commandBuffer,
                   sourceTexture: sourceTexture,
                   histogram: histogramInfoBuffer,
                   histogramOffset: 0)
```

## Topics

### Initializers

[`-  initWithCoder:device:`](/documentation/MetalPerformanceShaders/MPSImageHistogram/init(coder:device:))

### Methods

[`-  initWithDevice:histogramInfo:`](/documentation/MetalPerformanceShaders/MPSImageHistogram/init(device:histogramInfo:))

Initializes a histogram with specific information.

[`MPSImageHistogramInfo`](/documentation/MetalPerformanceShaders/MPSImageHistogramInfo)

The information used to compute the histogram channels of an image.

[`-  encodeToCommandBuffer:sourceTexture:histogram:histogramOffset:`](/documentation/MetalPerformanceShaders/MPSImageHistogram/encode(to:sourceTexture:histogram:histogramOffset:))

Encodes the filter to a command buffer using a compute command encoder.

[`-  histogramSizeForSourceFormat:`](/documentation/MetalPerformanceShaders/MPSImageHistogram/histogramSize(forSourceFormat:))

The amount of space the histogram will take up in the output buffer.

### Properties

[`clipRectSource`](/documentation/MetalPerformanceShaders/MPSImageHistogram/clipRectSource)

The source rectangle to use when reading data.

[`zeroHistogram`](/documentation/MetalPerformanceShaders/MPSImageHistogram/zeroHistogram)

Determines whether to zero-initialize the histogram results.

[`histogramInfo`](/documentation/MetalPerformanceShaders/MPSImageHistogram/histogramInfo)

A structure describing the histogram content.

[`minPixelThresholdValue`](/documentation/MetalPerformanceShaders/MPSImageHistogram/minPixelThresholdValue)

## Relationships

### Conforms To

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`CVarArg`](/documentation/Swift/CVarArg)

### Inherits From

[`MPSKernel`](/documentation/MetalPerformanceShaders/MPSKernel)

---

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)