Latest 0.0.1
Homepage https://github.com/kylry/KTSpectrum
License MIT
Platforms ios 9.0, requires ARC
Authors

Coming soon.

Background

Recently, I have been interested in the ways computers can process images. One area is dominant color evaluation which has applications in adaptive UI and a few other cool areas. I first noticed how Apples Music’s background changed depending on the dominant color of the album you were listening to. With this, I became interested in the ways that a computer program could process and image to find the image’s dominant colors.

CocoaPod

KTSpectrum is availible as a Cocoapod which you can install by inserting the following into your podfile.

pod 'KTSpectrum'

How does evaluation work?

Taking a Pixel Sample

A random set of CGPoints are generated within the bounds of a UIImage’s dimensions. {(x1, y1), (x2, y2), … (xn, yn)} where n is a resonably large number. These CGPoints are then sent to an extension of UIImage which returns pixel info (R, G, B, A) of the UIImage at that CGPoint. A dictionary is used to store the pixel data with the CGPoint as the key for the key-value pairs. With the pixel info stored, the program sends this dictionary to a k-means clustering algorithm.

k-Means Clustering

k-Means clustering is a way partition n observations into k clusters. Each observation (n) gets associated to the cluster (k) with the nearest mean.

KTSpectrum

The easiest way to visualize the results of k-means clustering is with a Veronoi diagram. Every point in the x-y plane gets assigned to a centroid k. The algorithm stops iteration when the movement of the centroids is not large (d < 0.0001).

You can better visualize the steps of the algorithm with the following diagram:

KTSpectrum

Swift Implementation

The easiest way use KTSpectrum is with the following statements:

let image = UIImage(named: "image.jpg")
let colors = image.getSpectrum()

getSpectrum() will return the top 10 colors with their percentages. These values are the positions of the 10 centroids.

getSpectrum will return an array of Spectrum objects that look like.

Spectrum(centroidMeanColor: UIColor, percentage: Double)

Latest podspec

{
    "name": "KTSpectrum",
    "version": "0.0.1",
    "summary": "KTSpectrum is a UIImage extension that identifies dominant colors in an image using k-means clustering.",
    "description": "KTSpectrum is a UIImage extension that identifies dominant colors in an image using k-means clustering. Often, it is useful to be able to find the dominant color in a UIImage. This could be applied to UI that adapts to its background when displaying an image. It can also be used in object recognition and AI eventually.",
    "homepage": "https://github.com/kylry/KTSpectrum",
    "license": {
        "type": "MIT",
        "file": "FILE_LICENSE.txt"
    },
    "authors": {
        "Kyle Ryan": "[email protected]"
    },
    "social_media_url": "http://twitter.com/kylry",
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/kylry/KTSpectrum.git",
        "tag": "0.0.1"
    },
    "source_files": "KTSpectrum/**/*.{swift}",
    "requires_arc": true,
    "xcconfig": {
        "HEADER_SEARCH_PATHS": "$(SDKROOT)/usr/include/libxml2"
    }
}

Pin It on Pinterest

Share This