CIKMeans filter - Example or documentation ?

iOS 13 introduced CIFilter "CIKMeans" which is used by the related CIPaletteCentroid  and CIPalettize filters.

Are there any examples of the use of CIKMeans? I am mystified by the CIKMeans error "Mean seeds should be passed as a K x 1 image.."

I am using a CIImage as the input for the inputMeans parm.

The inputMeans attribute has this in the parmDictionary
 
▿ 3 elements
 ▿ 0 : 2 elements
  - key : "CIAttributeDescription"
  - value : Specifies the color seeds to use for k-means clustering, either passed as an image or an array of colors.
 ▿ 1 : 2 elements
  - key : "CIAttributeClass"
  - value : CIImage
 ▿ 2 : 2 elements
  - key : "CIAttributeDisplayName"
  - value : Means"

What is a CIImage that qualifies as a "K x 1" image?

  • "What is a CIImage that qualifies as a "K x 1" image?"

    This filter uses an algorithm called K-means, which is the name of a classic method for determining the k dominant colors in an image, a.k.a. the "palette" of the image. (Look up "k-means clustering" on google for the details).

    Let's set k = 5 for example's sake. I've found this to be a good number of colors to capture the essential look of an image.

    Because you want the filter to give you the 5 most important colors in the image, it needs 5 "seed" colors to kick off its algorithm. These seeds are simply meant to be hints you supply in order to help the algorithm converge on the optimal answer faster—they don't have to be the "right" colors. (Determining the right colors is the algorithm's job, after all!)

    As an initial guess, why not just take the 5 pixel colors found in (say) the center and the corners of the image? Or black, red, green, yellow and blue?

    OK, but how do you supply these 5 hints to the CIFilter? An NSArray of five NSColor objects might be the most obvious way. Unfortunately, however, CIFilter doesn't like that kind of input, so instead it requests that you give it a 5-by-1 pixel image, where each pixel, reading right from left, is the corresponding color. Yes, it's a tiny, tiny image. You can generate it in code or in Photoshop or in Pixelmator.

    Caveat: I don't have MacOS 15 yet so I'm only speaking from my experience with processing images. I haven't had a chance to use CIKMeans yet. Hope this helps though.

Add a Comment

Replies

Replying in hopes to get some more interest in this topic. Am I the only one that feels like the documentation is still (after quite a bit of time) is just woefully lacking? Where's the notorious missing manual when you need it?
  • Yes, Peter, Apple's philosophy seems to be "WTFM" (c/f RTFM). I hope my reply to Will, above, is of some assistance.

Add a Comment