CocoaPods trunk is moving to be read-only. Read more on the blog, there are 7 months to go.

KTSpectrum 0.0.1

KTSpectrum 0.0.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Mar 2016
SPMSupports SPM

Maintained by Kyle Ryan.



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.

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:

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)